C# Bitblit 从位图到控件(紧凑框架)
我曾经使用过 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);
并进一步向下:)
IntPtr hb = mybitmap.GetHbitmap();
BitBlt(this.Handle, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
但表格保持纯白色。这是为什么?我犯的错误在哪里? 感谢您的意见。干杯,大卫
I used once BitBlt to save a screenshot to an image file (.Net Compact Framework V3.5, Windows Mobile 2003 and later). Worked fine. Now I want to draw a bitmap to a form. I could use this.CreateGraphics().DrawImage(mybitmap, 0, 0)
, but I was wondering if it would work with BitBlt like before and just swap the params. So I wrote:
[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);
(and further down:)
IntPtr hb = mybitmap.GetHbitmap();
BitBlt(this.Handle, 0, 0, mybitmap.Width, mybitmap.Height, hb, 0, 0, 0x00CC0020);
But the form stays plain white. Why is that? Where is the error I commited?
Thanks for your opinions. Cheers, David
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
this.Handle
是一个窗口句柄,而不是设备上下文。将
this.Handle
替换为this.CreateGraphics().GetHdc()
当然,您需要销毁图形对象等...
此外
hb< /code> 是一个
位图句柄
,而不是一个设备上下文
,因此上面的代码片段仍然不起作用。您需要从位图创建设备上下文:this.Handle
is a Window handle not a device context.Replace
this.Handle
withthis.CreateGraphics().GetHdc()
Of course you'll need to destroy the graphics object etc...
In addition
hb
is aBitmap Handle
not adevice context
so the above snippet still won't work. You'll need to create a device context from the bitmap:你的意思是这样的吗?
仅供参考,这是直接来自 SDF。
编辑:这段代码中并不是很清楚,但是 BitBlt 中的 hDC 是目标位图(您希望在其中绘制)的 HDC。
You mean something along these lines?
FYI, this is right out of the SDF.
EDIT: It's not real clear in this snippet, but hDC in the BitBlt is the HDC of the target bitmap (into which you wish to paint).
您确定 this.Handle 引用有效的设备上下文吗?您是否尝试过检查 BitBlt 函数的返回值?
请尝试以下操作:
Are you sure that
this.Handle
refers to a valid device context? Have you tried checking the return value of theBitBlt
function?Try the following: