本文介绍了是否可以直接从GDI +位图生成BitBlt?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

GetHBitmap较慢,因为除了BitBlt副本之外,它还制作了整个映像的新副本,并且速度比BitBlt副本慢,并且必须处理给定的HBITMAP.图片很大.

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.

是否有一种方法可以指向BitBlt以使用原始GDI +图像的像素数据?

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

编辑:我可以找到GDI +位图像素数据在内存中的位置的指针.我可以创建一个指向GDI +位图像素数据的HBITMAP以避免多余的复制吗,以及从中复制BitBlt吗?

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?

推荐答案

搜索几天后,突然发现答案一直盯着我!我正在从指向字节数组的指针创建GDI +位图.然后尝试使用相同的指针创建一个HBITMAP.但是我可以很容易地先创建HBITMAP,然后使用其中的指针来创建GDI +位图.

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.

它就像一种魅力!您可以随意混合使用GDI和GDI +操作.该图像同时是普通GDI和GDI +.您可以使用完全相同的像素数据中的BitBlt来代替DrawImage!

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!

代码如下:

// 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);

这篇关于是否可以直接从GDI +位图生成BitBlt?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 14:28