6

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

GetHBitmap 很慢,因为它制作了整个图像的新副本,除了 BitBlt 副本之外并且比 BitBlt 副本慢,并且必须释放给定的 HBITMAP。图像很大。

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

编辑: 我可以获得指向 GDI+ 位图像素数据在内存中的位置的指针。我可以创建一个指向 GDI+ 位图像素数据的 HBITMAP 以避免额外的副本,以及由此产生的 BitBlt 吗?

4

1 回答 1

8

找了几天,突然想到答案一直盯着我看!我正在从指向字节数组的指针创建 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);
于 2011-01-04T20:15:50.880 回答