是否可以将 BitBlt 直接连接到 GDI+位图?

发布于 2024-10-10 02:42:45 字数 700 浏览 0 评论 0原文

我正在尝试从 HBITMAP 到 GDI+ 位图的 BitBlt。我尝试了这个,但没有任何反应:

Bitmap Buffer = New Bitmap(608, 392)
Graphics BufferGraphics = Graphics.FromImage(Buffer);
IntPtr hBufferDC = BufferGraphics.GetHdc();

...

BitBlt(hBufferDC, x, y, width, height, hInputDC, 0, 0, SRCCOPY);

编辑: 显然,如果我获取它然后将其与 BitBlt 一起使用,hDC 将无法工作。我需要确保 hDC 仍然有效。这就是解决方案:

Bitmap Buffer = New Bitmap(608, 392)
Graphics BufferGraphics = Graphics.FromImage(Buffer);

...

IntPtr hBufferDC = BufferGraphics.GetHdc();
BitBlt(hBufferDC, x, y, width, height, hInputDC, 0, 0, SRCCOPY);
BufferGraphics.ReleaseHdc(hBufferDC);

有谁知道为什么需要进行此更改?为什么使用第一个示例中较早获得的 hDC 可能不起作用?

I am trying to BitBlt from an HBITMAP to a GDI+ bitmap. I tried this, but nothing happens:

Bitmap Buffer = New Bitmap(608, 392)
Graphics BufferGraphics = Graphics.FromImage(Buffer);
IntPtr hBufferDC = BufferGraphics.GetHdc();

...

BitBlt(hBufferDC, x, y, width, height, hInputDC, 0, 0, SRCCOPY);

EDIT: Apparently the hDC doesn't work if I acquire it and then much later use it with BitBlt. I needed to make sure the hDC was still valid. This is the solution:

Bitmap Buffer = New Bitmap(608, 392)
Graphics BufferGraphics = Graphics.FromImage(Buffer);

...

IntPtr hBufferDC = BufferGraphics.GetHdc();
BitBlt(hBufferDC, x, y, width, height, hInputDC, 0, 0, SRCCOPY);
BufferGraphics.ReleaseHdc(hBufferDC);

Does anyone know why this change is necessary? Why might it not work to use an hDC that was gotten earlier as in the first example?

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

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

发布评论

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

评论(1

安穩 2024-10-17 02:42:45

请查看 pinvoke.net 上页面末尾的示例。对 CreateCompatibleDC 和 SelectObject 的额外调用可能会使您的示例正常工作。

或者,您可以考虑使用 Graphics.DrawImageUns Called,它允许您仅在 .Net 端实现代码,并且仍然提供相当好的性能。

更新(由于更新的问题)
我不知道为什么 hDC 一段时间后变得无效,但根据 MSDN 您成对调用 GetHdc 和 ReleaseHdc,并对它们之间的 GDI+ 代码的调用进行分组:“对 GetHdc 和 ReleaseHdc 方法的调用必须成对出现。在 GetHdc 和 ReleaseHdc 方法对的范围内,您通常只进行调用到 GDI 函数。”

因此,根据文档,您在第二个示例中所做的方式就是正确的方法,您不应该缓存和重用 GetHdc 方法中的值。

Check the sample at the end of this page on pinvoke.net. The additional calls to CreateCompatibleDC and SelectObject might make your sample work.

Alternatively, you can consider using Graphics.DrawImageUnscalled which would allow you to implement your code only on the .Net side and would still offer a pretty good performance.

Update (Due to updated question)
I don't know exactly why the hDC becomes invalid after a while, but according to MSDN you call GetHdc and ReleaseHdc in pairs and group the calls to GDI+ code between them: "Calls to the GetHdc and ReleaseHdc methods must appear in pairs. During the scope of a GetHdc and ReleaseHdc method pair, you usually make only calls to GDI functions."

So according to the documentation, the way you did in your second sample is the way to go and you shouldn't cache and reuse values from the GetHdc method.

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