如何在给定的位图句柄上绘图(C++ / Win32)?

发布于 2024-09-07 08:41:03 字数 192 浏览 5 评论 0原文

我正在编写一个非托管 Win32 C++ 函数,它获取位图的句柄,并且我需要在它上绘图。

我的问题是,要绘制,我需要获取设备上下文,但是当我执行 GetDC (NULL) 时,它为我提供了 WINDOW 的设备上下文! GetDC()的参数是一个窗口句柄(HWND),但是我没有窗口;只是一个位图句柄。

我怎样才能在这个位图上绘图?谢谢!

I'm writing an unmanaged Win32 C++ function that gets a handle to a bitmap, and I need to draw on it.

My problem is that to draw I need to get a device context, but when I do GetDC (NULL), it gives me a device context for the WINDOW! The parameter for GetDC () is a window handle (HWND), but I don't have a window; just a bitmap handle.

How can I draw on this bitmap? Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

琴流音 2024-09-14 08:41:03

除了帕维尔的回答之外,“与屏幕兼容”也总是困扰着我,但是,由于 CreateCompatibleDC(NULL) 普遍用于此目的,我认为它是正确的。

我认为“兼容”的事情仅与DDB相关(DC设置为在当前屏幕的正确DDB类型上写入),但不影响DIB上的读/写。

因此,为了安全起见,如果您需要处理的位图不仅仅是暂时出现在屏幕上,请始终使用 DIB 而不是 DDB,如今性能差异可以忽略不计。请参阅 此处了解有关 DIB 和 DDB 的更多信息。

In addition to Pavel's answer, the "compatible with the screen" always bugged me too, but, since CreateCompatibleDC(NULL) is universally used for that purpose, I assume it is correct.

I think that the "compatible" thing is related just to DDB (the DC is set up to write on the correct DDB type for the current screen), but does not affect read/writes on DIBs.

So, to be safe, always use DIBs and not DDBs if you need to work on bitmaps that doesn't just have to go temporarily onscreen, nowadays the difference in performance is negligible. See here for more info about DIBs and DDBs.

匿名的好友 2024-09-14 08:41:03

CreateCompatibleDC() 和 SelectObject() 将您的位图放入其中。

然而,并非每个位图都可以选择到任何 DC 中。
您可能需要使用内存 DC 的映射模式和其他选项。

CreateCompatibleDC() and SelectObject() your bitmap into it.

However, not every bitmap can be selected into any DC.
You might have to play with mapping mode and other options of memory DCs.

情绪 2024-09-14 08:41:03

在位图上绘图的基本 win32 范例是,将位图选择到设备上下文上,之后,该设备上下文上的所有绘图操作都存储在位图中。然后,您可以使用各种“blit”操作之一(例如 StretchBlt)将其传输到显示表面,这只是窗口客户端区域的设备上下文。

其他人提供了更好的细节,这只是高级视图。

The basic win32 paradigm for drawing on a bitmap is that you select the bitmap onto a device context, after which, all drawing operations on that device context are stored in the bitmap. You then use one of the various 'blit' operations (e.g. StretchBlt) to transfer this to a display surface, which is just the device context of a window client area.

Others have provided better detail, this is just the high-level view.

清风挽心 2024-09-14 08:41:03

嗯,这有点超出框架..我想..但我确实知道 Graphics 可以 返回 HDC,并且 GraphicsBitmap 作为其 ctor位图又可以是从 HBITMAP 和 HPALETTE 创建。这里唯一的问题是我不知道 HPALETTE 参数是否可以为 NULL。

Graphics* g;
Bitmap* bitmap;
HBITMAP _bitmap;   // <- this one is yours

bitmap = Bitmap::FromHBITMAP(_bitmap, NULL);
g = new Graphics(bitmap);

HDC hdc = g->GetHDC();

// when done, call g->ReleaseHDC(hdc);

但是,我强烈建议您也将 HDC 作为您的函数的参数。我认为没有人会拥有 BITMAP 而没有 DC。
如果您在查找 HBITMAP 的 HDC 时遇到这些问题,那么其他人也会遇到这些问题。

Well, this is a bit outside the box.. I guess.. But I do know that Graphics can return a HDC, and Graphics take a Bitmap as an argument to its ctor . A Bitmap in turn can be created from a HBITMAP and a HPALETTE. The only problem here is that I do not know if the HPALETTE argument can be NULL.

Graphics* g;
Bitmap* bitmap;
HBITMAP _bitmap;   // <- this one is yours

bitmap = Bitmap::FromHBITMAP(_bitmap, NULL);
g = new Graphics(bitmap);

HDC hdc = g->GetHDC();

// when done, call g->ReleaseHDC(hdc);

However, I would urge you to receive the HDC as an argument to your function as well.. I do not think that anyone will have a BITMAP and NOT have the DC to it.
If you're having these issues with finding a HDC to a HBITMAP, so will everyone else.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文