1

I try to load an image from imgres32.dll. I'm trying to do it like this:

Load the dll:

dll_h = LoadLibrary(@"C:\Windows\System32\imgres32.dll");

Pass the handle to my function which does the ressource loading:

Bitmap b = GetImageResource(dll_h, "1002");

The function looks like this:

static Bitmap GetImageResource(IntPtr handle, string resourceId)
{
    IntPtr img_ptr = NativeMethods.LoadImage(handle, resourceId, IMAGE_BITMAP, 0, 0, 0);

    if (img_ptr == IntPtr.Zero)
        throw new System.ComponentModel.Win32Exception((int)NativeMethods.GetLastError());

    return Image.FromHbitmap(img_ptr);
}

No matter which parameters I enter, I always get error code 1813 meaning

The specified resource type cannot be found in the image file.

When I open the file in Visual Studio, I see a folder called Icon containing an Image with id 1002.

enter image description here

When I click it, it shows me several Bitmap images contained, in different resolutions, containing one with resolution 16 x 16. But when I call

LoadImage(handle, resourceId, IMAGE_BITMAP, 16, 16, 0);

Neither this not any other parameter combination does work, I always get error 1813.

IMAGE_BITMAP is a constant int set to 0 like documented here, same with IMAGE_ICON and IMAGE_CURSOR but none of them works.

Help is very much appreciated. Thanks.

Tom Doodler
  • 1,471
  • 2
  • 15
  • 41

1 Answers1

1

You should prefix the resource Id with #. Call it this way:

GetImageResource(dll_h, "#1002"); 
Sergey L
  • 1,402
  • 1
  • 9
  • 11
  • That's right. Or you could imitate the MAKEINTRESOURCE macro by passing an unsigned int to the native LoadImage method instead of a string: `[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr LoadImage(IntPtr hinst, uint uId, uint uType, int cxDesired, int cyDesired, uint fuLoad);` Then you can just use the integer 1002. – Yosh Sep 15 '16 at 10:32