C# LoadImage returns 错误 1813

C# LoadImage returns error 1813

我尝试从 imgres32.dll 加载图像。我正在尝试这样做:

加载 dll:

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

将句柄传递给我执行资源加载的函数:

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意思

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

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

当我点击它时,它会显示几张包含不同分辨率的位图图像,其中一张分辨率为 16 x 16。但是当我打电话给

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

这和任何其他参数组合都不起作用,我总是得到错误 1813

IMAGE_BITMAP 是一个常量 int 设置为 0,如记录的 here,与 IMAGE_ICONIMAGE_CURSOR 相同,但其中 none有效。

非常感谢您的帮助。谢谢。

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

GetImageResource(dll_h, "#1002");