【发布时间】:2015-09-12 03:45:36
【问题描述】:
我的第一篇文章就到这里了,
我有一个包含大约 30 个控件(标签、文本框等)的 UserControl。 现在我想做一个“截图”。 所以我使用了 UserControl 中的“DrawToBitmap”方法。
这里有一些示例代码
//this is the UserControl with the about 30 controls
var sampleusercontrol = new SampleUserControl();
var bmp = new Bitmap(sampleusercontrol.Width, sampleusercontrol.Height);
sampleusercontrol.DrawToBitmap(bmp, sampleusercontrol.Bounds);
如果我运行此代码,它会一直返回给我一个黑色图像。 我不知道为什么。请帮忙!
编辑:
忘了说UserControl是WinForms UserControl
【问题讨论】:
-
我也遇到了同样的问题。对我来说,解决方案是在创建位图之前执行 - sampleusercontrol.Arrange(0, 0, sampleusercontrol.Width, sampleusercontrol.height)。我不知道为什么会这样,但它确实......
-
DrawToBitmap只需发送WM_PRINT消息。Arrange是 wpf 函数,为什么不是 do it in the right way ? -
抱歉我忘了说我说的usercontrol,是一个winforms usercontrol
-
Bounds 只是错误的矩形,请改用 sampleusercontrol.DisplayRectangle。
-
添加到 Hans 的评论中:
Bounds位于控件的Location处,因此除非它位于屏幕原点,否则您将错过大部分或全部图像。根据控件是否滚动,您可以为可见部分选择ClientRectangle,或为包括不可见部分在内的全部内容选择DisplayRectangle。
标签: c# winforms user-controls controls drawtobitmap