Win32 Loadimage生成错误1812和1813

时间:2015-08-06 14:31:54

标签: c++ c winapi runtime-error loadimage

我在编写扫雷游戏时遇到了这个问题。我使用位图来表示数字,地雷和空白。我想我已经在资源文件

中正确注册了它们
IDI_0                   BITMAP                  "D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\empty.bmp"
IDI_1                   BITMAP                  "D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\1.bmp"
IDI_2                   BITMAP                  "D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\2.bmp"
IDI_3                   BITMAP                  "D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\3.bmp"

和头文件

#define IDI_0                           200
#define IDI_1                           201
#define IDI_2                           202
#define IDI_3                           203

我按照这样加载它们

h0 = (HBITMAP)LoadImage(hInst, MAKEINTRESOURCE(IDI_0), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
h1 = (HBITMAP)LoadImage(hInst, MAKEINTRESOURCE(IDI_1), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
h2 = (HBITMAP)LoadImage(hInst, MAKEINTRESOURCE(IDI_2), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);
h3 = (HBITMAP)LoadImage(hInst, MAKEINTRESOURCE(IDI_3), IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION);

我还使用resourcehacker检查了exe文件并找到了所有位图。

我不明白的是,当我运行游戏时,有时候(~50%)会弹出

Error 1812: The specified image file did not contain a resource section. 

Error 1813: The specified resource type cannot be found.

但是如果我从像这样的文件中加载它们

h0 = (HBITMAP)LoadImage(NULL, L"D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\empty.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
h1 = (HBITMAP)LoadImage(NULL, L"D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\1.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
h2 = (HBITMAP)LoadImage(NULL, L"D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\2.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
h3 = (HBITMAP)LoadImage(NULL, L"D:\\User\\Mark\\Documents\\C++\\win32\\MineSweeper\\MineSweeper\\3.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);

一切正常。

任何想法或建议都将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

我个人考虑使用GDI +来加载图片。您仍然可以从磁盘文件或应用程序的资源部分加载。它还为您提供了访问Windows本机支持的所有图像格式(BMP,GIF,JPEG,PNG,TIFF,Exif,WMF和EMF)的优势

要使用它,您只需先初始化GDI +,然后在程序退出前执行关机。

这是我用来从磁盘文件加载的函数:

// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
HBITMAP mLoadImageFile(wchar_t *filename)
{
    HBITMAP result = NULL;
    Bitmap bitmap(filename, false);
    bitmap.GetHBITMAP(0, &result);
    return result;
}

如果我们有一个包含以下内容的资源文件:

IDR_PNG1           RT_PNG            ".\\girl.png"
IDR_JPG1           RT_JPG            ".\\rssLogo.jpg"

然后我们可以这样加载每个图像:

HBITMAP png = loadImgResource(IDR_PNG1, L"RT_PNG");
HBITMAP jpg = loadImgResource(IDR_JPG1, L"RT_JPG");

我用来从资源加载的两个函数。您注意到我在第二个函数中的GetModuleHandle(0)中进行了硬编码 - 如果您想从当前模块以外的任何模块(dll,exe)读取,请更改此项。

// BMP, GIF, JPEG, PNG, TIFF, Exif, WMF, and EMF
HBITMAP loadImgResource(wchar_t *pName, wchar_t *pType, HMODULE hInst)
{
    Gdiplus::Bitmap *m_pBitmap;
    HBITMAP result=NULL;

    HRSRC hResource = FindResource(hInst, pName, pType);
    if (!hResource)
        return NULL;

    DWORD imgSize = SizeofResource(hInst, hResource);
    if (!imgSize)
        return NULL;

    const void *pResourceData = LockResource(LoadResource(hInst, hResource));
    if (!pResourceData)
        return NULL;

    HANDLE m_hBuffer = GlobalAlloc(GMEM_MOVEABLE, imgSize);
    if (m_hBuffer)
    {
        void* pBuffer = GlobalLock(m_hBuffer);
        if (pBuffer)
        {
            CopyMemory(pBuffer, pResourceData, imgSize);
            IStream* pStream = NULL;
            if (CreateStreamOnHGlobal(m_hBuffer, FALSE, &pStream) == S_OK)
            {
                m_pBitmap = Gdiplus::Bitmap::FromStream(pStream);
                pStream->Release();
                if (m_pBitmap)
                {
                    if (m_pBitmap->GetLastStatus() == Gdiplus::Ok)
                    {
                        m_pBitmap->GetHBITMAP(0, &result);
                        delete m_pBitmap;
                    }
                }
            }
            GlobalUnlock(m_hBuffer);
        }
        GlobalFree(m_hBuffer);
    }
    return result;
}

HBITMAP loadImgResource(WORD resNum, LPWSTR pType)
{
    return loadImgResource(MAKEINTRESOURCE(resNum), pType, GetModuleHandle(0));//hInst);
}