是否可以直接从GDI+位图直接进行bitblt?
题
是否可以在不使用GethBitMap的情况下使用BITBLT直接从GDI+位图中复制?
GethBitmap很慢,因为除了比Bitblt副本较慢,并且必须处理给定的HBITMAP。图像很大。
有没有办法指向BITBLT使用原始GDI+图像的像素数据?
编辑: 我可以在内存中获取一个指向GDI+位图像素数据的指针。我可以创建一个指向GDI+位图像素数据的HBITMAP以避免额外的副本,并从中避免使用BitBlt?
解决方案
在搜寻了几天之后,我突然打动了我的答案一直在盯着我!我正在创建一个从指针到字节数组的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);
不隶属于 StackOverflow