Win32 Loadimage 生成错误 1812 和 1813
Win32 Loadimage generates error 1812 and 1813
我在写扫雷游戏的时候遇到了这个问题。我将位图用于数字、地雷和空白。我想我已经在资源文件中正确注册了它们
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);
一切正常。
如有任何想法或建议,我们将不胜感激。谢谢!
我个人会考虑使用 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);
}
我在写扫雷游戏的时候遇到了这个问题。我将位图用于数字、地雷和空白。我想我已经在资源文件中正确注册了它们
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);
一切正常。
如有任何想法或建议,我们将不胜感激。谢谢!
我个人会考虑使用 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);
}