我正在尝试使用BitBlt获取图形区域并将它们存储在位图中。但是在这里,我做了一些更容易理解我的问题的事情:
Bitmap sourceBitmap = new Bitmap(64, 64, PixelFormat.Format32bppRgb);
Graphics sourceGraphics = Graphics.FromImage(sourceBitmap);
Bitmap destBitmap = new Bitmap(64, 64, PixelFormat.Format32bppRgb);
Graphics destGraphics = Graphics.FromImage(destBitmap);
sourceGraphics.FillRectangle(new SolidBrush(Color.Red), new Rectangle(0, 0, 30, 30));
sourceGraphics.FillRectangle(new SolidBrush(Color.Green), new Rectangle(30, 30, 30, 30));
destGraphics.FillRectangle(new SolidBrush(Color.Blue), new Rectangle(0, 0, 30, 30));
destGraphics.FillRectangle(new SolidBrush(Color.Yellow), new Rectangle(30, 30, 30, 30));
IntPtr destDC = destGraphics.GetHdc();
IntPtr destHB = destBitmap.GetHbitmap();
IntPtr old = SelectObject(destDC, destHB);
IntPtr sourceDC = sourceGraphics.GetHdc();
IntPtr sourceHB = sourceBitmap.GetHbitmap();
old = SelectObject(sourceDC, sourceHB);
int success = BitBlt(
destDC, 0, 0, 64, 64, sourceDC, 0, 0, 0x00CC0020
);
为什么在 BitBlt 之后,我的 destBitmap 包含蓝色/黄色矩形(目标中的初始位图),而不是应该从源位图中删除的红色/绿色矩形?
导入是这样完成的:
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
private static extern int BitBlt(
IntPtr hdcDest, // handle to destination DC (device context)
int nXDest, // x-coord of destination upper-left corner
int nYDest, // y-coord of destination upper-left corner
int nWidth, // width of destination rectangle
int nHeight, // height of destination rectangle
IntPtr hdcSrc, // handle to source DC
int nXSrc, // x-coordinate of source upper-left corner
int nYSrc, // y-coordinate of source upper-left corner
System.Int32 dwRop // raster operation code
);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
public static extern IntPtr SelectObject(IntPtr hdc, IntPtr obj);
[System.Runtime.InteropServices.DllImportAttribute("gdi32.dll")]
public static extern void DeleteObject(IntPtr obj);
最后清理代码和位图流以查看位图内容:
DeleteObject(destHB);
DeleteObject(sourceHB);
destGraphics.ReleaseHdc();
sourceGraphics.ReleaseHdc();
string path = "c:/tmp/dest.png";
destBitmap.Save(path);