Dav*_*vid 3 c# graphics windows-mobile bitblt
我曾经使用 BitBlt 将屏幕截图保存到图像文件(.Net Compact Framework V3.5、Windows Mobile 2003 及更高版本)。工作得很好。现在我想在表单上绘制位图。我可以使用this.CreateGraphics().DrawImage(mybitmap, 0, 0),但我想知道它是否可以像以前一样与 BitBlt 一起使用并且只是交换参数。所以我写道:
[DllImport("coredll.dll")]
public static extern int BitBlt(IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, uint dwRop);
Run Code Online (Sandbox Code Playgroud)
(再往下走:)
IntPtr hb = mybitmap.GetHbitmap();
BitBlt(this.Handle, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
Run Code Online (Sandbox Code Playgroud)
但形式保持纯白色。这是为什么?我犯的错误在哪里?谢谢你的意见。干杯,大卫
小智 5
this.Handle是一个Window 句柄而不是一个设备上下文。
替换this.Handle为this.CreateGraphics().GetHdc()
当然,您需要销毁图形对象等...
IntPtr hb = mybitmap.GetHbitmap();
using (Graphics gfx = this.CreateGraphics())
{
BitBlt(gfx.GetHdc(), 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
}
Run Code Online (Sandbox Code Playgroud)
另外hbis a Bitmap Handlenot adevice context所以上面的代码片段仍然不起作用。您需要从位图创建设备上下文:
using (Bitmap myBitmap = new Bitmap("c:\test.bmp"))
{
using (Graphics gfxBitmap = Graphics.FromImage(myBitmap))
{
using (Graphics gfxForm = this.CreateGraphics())
{
IntPtr hdcForm = gfxForm.GetHdc();
IntPtr hdcBitmap = gfxBitmap.GetHdc();
BitBlt(hdcForm, 0, 0, myBitmap.Width, myBitmap.Height, hdcBitmap, 0, 0, 0x00CC0020);
gfxForm.ReleaseHdc(hdcForm);
gfxBitmap.ReleaseHdc(hdcBitmap);
}
}
}
Run Code Online (Sandbox Code Playgroud)