是否可以直接从 GDI 进行 BitBlt?位图?

发布于 2024-10-10 19:14:41 字数 295 浏览 9 评论 0原文

是否可以使用 BitBlt 直接从 GDI+ 位图中复制而不使用 GetHBitmap?

GetHBitmap 很慢,因为除了 BitBlt 副本之外,它还创建整个图像的新副本,并且比 BitBlt 副本慢,并且必须处理给定的 HBITMAP。图像很大。

有没有办法让BitBlt使用原始GDI+图像的像素数据?

编辑:我可以获得指向 GDI+ 位图像素数据在内存中的位置的指针。我可以创建一个指向 GDI+ 位图像素数据的 HBITMAP 以避免额外的复制,并从中创建 BitBlt 吗?

Is it possible to use BitBlt to copy directly out of a GDI+ bitmap without using GetHBitmap?

GetHBitmap is slow because it makes a new copy of the whole image, in addition to and slower than the BitBlt copy, and the given HBITMAP must be disposed. The image is large.

Is there a way to point BitBlt to use the pixel data of the original GDI+ image?

EDIT: I can get a pointer to where the GDI+ bitmap pixel data is in the memory. Can I create an HBITMAP that points to the GDI+ bitmap pixel data to avoid the extra copy, and BitBlt from that?

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

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

发布评论

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

评论(1

小嗷兮 2024-10-17 19:14:41

经过几天的寻找,我突然意识到答案一直就在我面前!我正在从指向字节数组的指针创建 GDI+ 位图。然后尝试使用相同的指针创建 HBITMAP。但我可以先轻松创建 HBITMAP,然后使用其中的指针来创建 GDI+ 位图。

它就像一个魅力!您可以随意混合 GDI 和 GDI+ 操作。该图像同时是普通 GDI 和 GDI+。您可以从完全相同的像素数据中进行 BitBlt,而不是使用 DrawImage!

这是代码:

// Create the HBITMAP
BITMAPINFO binfo = new BITMAPINFO();
binfo.biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFO));
binfo.biWidth = width;
binfo.biHeight = height;
binfo.biBitCount = (ushort)Image.GetPixelFormatSize(pixelFormat);
binfo.biPlanes = 1;
binfo.biCompression = 0;

hDC = CreateCompatibleDC(IntPtr.Zero);

IntPtr pointer;
hBitmap = CreateDIBSection(hDC, ref binfo, 0, out pointer, IntPtr.Zero, 0);

// Create the GDI+ bitmap using the pointer returned from CreateDIBSection
gdiBitmap = new Bitmap(width, height, width * binfo.biBitCount >> 3, pixelFormat, pointer);

After searching for days, it suddenly hit me that the answer had been staring me in the face all the time! I was creating a GDI+ bitmap from a pointer to a byte array. Then trying to create an HBITMAP using the same pointer. But I could just as easily create the HBITMAP first and use the pointer from that to create the GDI+ bitmap.

It works like a charm! You can mix GDI and GDI+ operations however you like. The image is both plain GDI and GDI+ at once. Instead of using DrawImage, you can BitBlt from the exact same pixel data!

Here's the code:

// Create the HBITMAP
BITMAPINFO binfo = new BITMAPINFO();
binfo.biSize = (uint)Marshal.SizeOf(typeof(BITMAPINFO));
binfo.biWidth = width;
binfo.biHeight = height;
binfo.biBitCount = (ushort)Image.GetPixelFormatSize(pixelFormat);
binfo.biPlanes = 1;
binfo.biCompression = 0;

hDC = CreateCompatibleDC(IntPtr.Zero);

IntPtr pointer;
hBitmap = CreateDIBSection(hDC, ref binfo, 0, out pointer, IntPtr.Zero, 0);

// Create the GDI+ bitmap using the pointer returned from CreateDIBSection
gdiBitmap = new Bitmap(width, height, width * binfo.biBitCount >> 3, pixelFormat, pointer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文