2016-09-15 108 views
0

我尝试从imgres32.dll加载图像。我试图做这样的:C#LoadImage返回错误1813

加载DLL:

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

传递句柄到我的功能它执行的ressource加载:

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

功能如下:

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); 
} 

无论哪个参数我进去,我总是得到错误代码1813的意思

在映像文件中找不到指定的资源类型。

当我打开Visual Studio中的文件,我看到一个名为Icon包含图像的ID 1002文件夹。

enter image description here

当我点击它,它让我看到几个位图图像包含的,在不同的分辨率,包含一个与16 x 16分辨率。但是,当我打电话

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

无论这个没有任何其他的参数组合不工作,我总是得到错误1813

IMAGE_BITMAP是一个常数INT设置为0像记录here,同样有IMAGE_ICONIMAGE_CURSOR但他们没有工作。

帮助非常感谢。谢谢。

回答

1

您应该在资源ID前添加#。这样称呼:

GetImageResource(dll_h, "#1002"); 
+0

没错。或者,您可以通过将unsigned int传递给本机LoadImage方法而不是字符串来模仿MAKEINTRESOURCE宏:'[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);'然后你可以使用整数1002。 – Yosh