更新时间:2023-12-06 12:37:58
显示窗体之前,窗体和控件不在的 创建
状态。要强制窗体和控件要创建它足以调用内部的 CreateControl(布尔fIgnoreVisible)
窗体的方法:
Before showing the form, the form and its controls are not in Created
state. To force the form and its controls to be created it's enough to call internal CreateControl(bool fIgnoreVisible)
method of your form:
var f = new Form1();
var createControl = f.GetType().GetMethod("CreateControl",
BindingFlags.Instance | BindingFlags.NonPublic);
createControl.Invoke(f, new object[] { true });
var bm = new Bitmap(f.Width, f.Height);
f.DrawToBitmap(bm, new Rectangle(0, 0, bm.Width, bm.Height));
bm.Save(@"d:\bm.bmp");
同时删除您的表单中有代码加载
事件处理程序,并把它们在窗体的构造函数。
Also remove codes which you have in your form Load
event handler, and put them in constructor of your form.
注意
还有其他解决方法的问题:
There are also other workarounds for the problem:
位置
设置为( - 32000,-32000)
并设置中StartPosition
是手动
然后显示
和隐藏
的形式绘制位图前。透明度
设置为的形式0
然后显示
和隐藏
绘制位图前的形式。Location
to (-32000, -32000)
and set StartPosition
to be Manual
then Show
and Hide
the form before drawing to bitmap.Opacity
set to 0
and then Show
and Hide
the form before drawing to bitmap.