2016-09-15 107 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