2

我需要创建 HBITMAP。

问题就在这里。我在内存中有 bmp 文件的内容。

如果位图作为资源,我知道如何创建 HBITMAP。但由于它在内存中,我不知道该怎么做。

我这样做(如果在资源中):链接

    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是限制位图内容的内存。

4

1 回答 1

2

首先,让我们删除一个小问题:

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 兼容。(通常是屏幕、窗口或绘画 DC)。

您可以通过包含初始数据的缓冲区创建初始化位图,或使用CreateBitmapGetBitmapBits获取位图保存的数据。

无论如何,这些是位图数据的本地副本,而不是 GDI 绘制的“实时位图”。

还要注意,这些数据的内部结构取决于位图需要具有的格式(在多少个平面上每个像素有多少位,有无调色板),并且为了避免 Blit 过程中的性能损失,它必须与您的屏幕设置使用的格式一致。

这不一定与保存到“bmp”文件中的位图相同。

于 2013-04-19T11:39:08.167 回答