将 LPVOID 位图指针转换为 QPixmap
Convert LPVOID bitmap pointer to QPixmap
我正在尝试将资源文件夹中的 BMP 图像加载到 QPixmap 对象中。但是,即使将这些字节重写为新文件可以正确复制原始文件,我也无法读取这些字节。这是我的加载方法:
QPixmap* GUIMain::loadImage(int name) {
// Resource loading, works fine
HRSRC rc = FindResource(NULL, MAKEINTRESOURCE(name), RT_BITMAP);
if (rc == NULL) {
printf("INVALID RESOURCE ADDRESS (%i)\n", name);
return new QPixmap();
}
HGLOBAL rcData = LoadResource(NULL, rc);
LPVOID data = LockResource(rcData);
DWORD data_size = SizeofResource(NULL, rc);
// Rewrite file to new file, works fine
ofstream output("E:\" + to_string(name) + ".bmp", std::ios::binary);
BITMAPFILEHEADER bfh = { 'MB', 54 + data_size, 0, 0, 54 };
output.write((char*)&bfh, sizeof(bfh));
output.write((char*)data, data_size);
output.close();
// Need to return, can't get bytes working
return new QPixmap(/*?*/);
}
使用 resource.h
文件中的定义调用此方法。
我曾尝试使用 stringstream
与 ofstream
进行相同的调用,然后将该流用作 QPixmap
的源,但该流没有产生相同的输出。
以下是我的 resource.h
文件的相关部分:
#define IDB_BITMAP1 101
#define IDB_BITMAP2 102
这是我的 Resource.rc
文件:
IDB_BITMAP1 BITMAP "E:\Downloads\onIcon.bmp"
IDB_BITMAP2 BITMAP "E:\Downloads\offIcon.bmp"
我知道我应该使用 Qt 工具进行资源管理,但我没有这样做的能力。
您可以使用 QPixmap::loadFromData(...) 从 bmp 格式的字节创建 QPixmap,但您也不需要在 .rc 文件中将资源声明为 "BITMAP"。
位图资源旨在与 LoadBitmap(...) or LoadImage(...) and are stored in the .exe with the bitmap header stripped off. (Raymond Chen discusses this here 一起使用)由于您没有使用 LoadBitmap,因此请将资源类型设置为任意二进制数据,例如
IDB_BITMAP1 RCDATA "E:\Downloads\onIcon.bmp"
然后按如下方式实现您的图像加载例程:
QPixmap* GUIMain::loadImage(int name) {
// ...
HGLOBAL rcData = LoadResource(NULL, rc);
LPVOID data = LockResource(rcData);
DWORD data_size = SizeofResource(NULL, rc);
QPixmap* pm = new QPixmap();
pm->loadFromData( static_cast<uchar*>(data), data_size, "bmp");
return pm;
}
我正在尝试将资源文件夹中的 BMP 图像加载到 QPixmap 对象中。但是,即使将这些字节重写为新文件可以正确复制原始文件,我也无法读取这些字节。这是我的加载方法:
QPixmap* GUIMain::loadImage(int name) {
// Resource loading, works fine
HRSRC rc = FindResource(NULL, MAKEINTRESOURCE(name), RT_BITMAP);
if (rc == NULL) {
printf("INVALID RESOURCE ADDRESS (%i)\n", name);
return new QPixmap();
}
HGLOBAL rcData = LoadResource(NULL, rc);
LPVOID data = LockResource(rcData);
DWORD data_size = SizeofResource(NULL, rc);
// Rewrite file to new file, works fine
ofstream output("E:\" + to_string(name) + ".bmp", std::ios::binary);
BITMAPFILEHEADER bfh = { 'MB', 54 + data_size, 0, 0, 54 };
output.write((char*)&bfh, sizeof(bfh));
output.write((char*)data, data_size);
output.close();
// Need to return, can't get bytes working
return new QPixmap(/*?*/);
}
使用 resource.h
文件中的定义调用此方法。
我曾尝试使用 stringstream
与 ofstream
进行相同的调用,然后将该流用作 QPixmap
的源,但该流没有产生相同的输出。
以下是我的 resource.h
文件的相关部分:
#define IDB_BITMAP1 101
#define IDB_BITMAP2 102
这是我的 Resource.rc
文件:
IDB_BITMAP1 BITMAP "E:\Downloads\onIcon.bmp"
IDB_BITMAP2 BITMAP "E:\Downloads\offIcon.bmp"
我知道我应该使用 Qt 工具进行资源管理,但我没有这样做的能力。
您可以使用 QPixmap::loadFromData(...) 从 bmp 格式的字节创建 QPixmap,但您也不需要在 .rc 文件中将资源声明为 "BITMAP"。
位图资源旨在与 LoadBitmap(...) or LoadImage(...) and are stored in the .exe with the bitmap header stripped off. (Raymond Chen discusses this here 一起使用)由于您没有使用 LoadBitmap,因此请将资源类型设置为任意二进制数据,例如
IDB_BITMAP1 RCDATA "E:\Downloads\onIcon.bmp"
然后按如下方式实现您的图像加载例程:
QPixmap* GUIMain::loadImage(int name) {
// ...
HGLOBAL rcData = LoadResource(NULL, rc);
LPVOID data = LockResource(rcData);
DWORD data_size = SizeofResource(NULL, rc);
QPixmap* pm = new QPixmap();
pm->loadFromData( static_cast<uchar*>(data), data_size, "bmp");
return pm;
}