【问题标题】:Drawing bitmap from memory从内存中绘制位图
【发布时间】:2013-04-19 11:20:03
【问题描述】:

我需要创建 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 是约束位图内容的内存。

【问题讨论】:

  • CreateDIBitmap 是您所需要的,但它是一个复杂的函数。寻找一些示例代码。
  • 你能给我举个例子吗?我真的搜索了很多......
  • @c.adhityaa 没有找到任何我可以推荐的样品。正如我所说,这是一个复杂的功能,并且很大程度上取决于您的“内存资源”的格式。我想您只需要通过 MSDN 工作即可。当您有一些实际代码时,我相信这里的人会帮助您解决任何问题。请记住花时间仔细准确地描述您的问题,这不是您这次所做的事情,这就是您得到三个不同答案的原因。
  • 抱歉,当时很着急...现在改写问题会更好... :)

标签: c++ c winapi bitmap bmp


【解决方案1】:

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

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”文件中的位图相同。

【讨论】:

  • 我尝试了 CreateBitmap... 但我不明白一些参数...(例如 cplane、lpvBits)... 那么,您能否提供一些代码来演示这一点?跨度>
  • GetBitmapBits 被上面cmets 中提到的GetDIBits 取代,对parms 有更好的解释。该代码看起来像一个经典的双缓冲示例。 :)
猜你喜欢
  • 2011-10-10
  • 2013-02-03
  • 2012-06-06
  • 1970-01-01
  • 2011-06-02
  • 2013-06-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多