我需要创建 HBITMAP。
问题就出在这里。我内存中有 bmp 文件的内容。
如果位图作为资源,我知道如何创建 HBITMAP。 但由于它在内存中,我不知道该怎么做。
我这样做(如果在资源中):Link
hDC = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_EXERCISING));
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hDC);
// Select the new bitmap
SelectObject(MemDCExercising, bmpExercising);
// Copy the bits from the memory DC into the current dc
BitBlt(hDC, 10, 10, 450, 400, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
DeleteDC(MemDCExercising);
DeleteObject(bmpExercising);
EndPaint(hWnd, &Ps);
如果它是内存资源,请指导我如何操作。
以某种方式将 char img[10000]
更改为资源?
这里,img
是限制位图内容的内存。
最佳答案
首先,让我们消除一些无辜的陷阱:
hDC = BeginPaint(hWnd, &Ps);
// Load the bitmap from the resource
bmpExercising = LoadBitmap(hInst, MAKEINTRESOURCE(IDB_EXERCISING));
// Create a memory device compatible with the above DC variable
MemDCExercising = CreateCompatibleDC(hDC);
// Select the new bitmap
HOBJECT oldbmp = SelectObject(MemDCExercising, bmpExercising); //<<<<save it for later ...
// Copy the bits from the memory DC into the current dc
BitBlt(hDC, 10, 10, 450, 400, MemDCExercising, 0, 0, SRCCOPY);
// Restore the old bitmap
SelectObject(MemDCExercising, oldbmp); //<<<... DeleteDC will leak memory if it holds a resource
DeleteDC(MemDCExercising);
DeleteObject(bmpExercising);
EndPaint(hWnd, &Ps);
现在,HBITMAP(从概念上讲)是一个指向内部结构的指针,该结构持有一个指向您无法访问的 GDI 内存空间的“指针”(实际上更像是一个流)。
“内存位图”在您的程序中并不表示为属于您的程序的内存缓冲区,而是表示为...通过 CreateCompatibleBitmap 获得的 HBITMAP ,其中 HDC 参数 id 位图必须兼容的 DC。 (通常是屏幕、 window 或绘画 DC)。
您可以创建一个初始化的位图,传递包含初始数据的缓冲区,或者使用 CreateBitmap 获取位图保存的数据。或GetBitmapBits .
无论如何,这些都是位图数据的本地拷贝,而不是 GDI 绘制的“实时位图”。
另请注意,这些数据的内部结构取决于位图需要的格式(多少个平面上每个像素有多少位,有或没有调色板),以避免 Blit 过程中的性能损失,它必须与您的屏幕设置使用的格式一致。
这不一定与保存到“bmp”文件中的位图相同。
关于c++ - 从内存中绘制位图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16103417/