Implementing Bitmap.FromResource() - Acquiring hinstance.
-
public static Bitmap FromResource( IntPtr hinstance, string bitmapName )
I see that many others have had trouble getting Bitmap.FromResource() to work for them, and I've found conflicting or wrong information as to how to acquire the handle. Some examples call out to unmanaged functions. I'd like to avoid that if possible. I have a VS2005 assembly DLL to which I've added many bitmaps as embedded resources (via Project.Properties...Add Files...). I assume that much is OK. Is there an entirely "managed" way to get the handle of the assembly to pass to the hinstance argument of FromResource()? If not, what is the recommended process?
-
public static Bitmap FromResource( IntPtr hinstance, string bitmapName )
I see that many others have had trouble getting Bitmap.FromResource() to work for them, and I've found conflicting or wrong information as to how to acquire the handle. Some examples call out to unmanaged functions. I'd like to avoid that if possible. I have a VS2005 assembly DLL to which I've added many bitmaps as embedded resources (via Project.Properties...Add Files...). I assume that much is OK. Is there an entirely "managed" way to get the handle of the assembly to pass to the hinstance argument of FromResource()? If not, what is the recommended process?
For instance...
using System.Diagnostics; myBitmap = Bitmap.FromResource( Process.GetCurrentProcess( ).Handle, @"MyBitmapResourceName" );
This call fails, reporting that the "Parameter is not valid."
-
public static Bitmap FromResource( IntPtr hinstance, string bitmapName )
I see that many others have had trouble getting Bitmap.FromResource() to work for them, and I've found conflicting or wrong information as to how to acquire the handle. Some examples call out to unmanaged functions. I'd like to avoid that if possible. I have a VS2005 assembly DLL to which I've added many bitmaps as embedded resources (via Project.Properties...Add Files...). I assume that much is OK. Is there an entirely "managed" way to get the handle of the assembly to pass to the hinstance argument of FromResource()? If not, what is the recommended process?
This approach appears [?] to get a legitimate handle. The value is usually just over 1,000 -- 1,008, 1,032, etc. I've checked MyResourceName over and over again for case. I've tried all uppercase (which doesn't match the resource name), and I've tried odd syntax that I've seen in other examples (which resort to native C++ to get a handle) such as prefixing MyResourceName with "#" or including the file extension (which in my experience with resource files should be an error)... but still, all I get is a "Parameter is not valid" error:
IntPtr hInstance = Process.GetCurrentProcess().Handle; if ( (IntPtr)0 != hInstance ) myBitmap = Bitmap.FromResource( hInstance, "MyResourceName" );
Process.GetCurrentProcess().Handle is as close as I've come to succeding, in that the value is possibly a legitimate handle/IntPtr value. Does anybody know how to do this?