【发布时间】:2014-04-23 08:49:04
【问题描述】:
是否可以从当前表单创建图像?例如 onLoad 在根文件夹中创建表单当前状态的图片?将用户看到的内容存储为图片。
【问题讨论】:
是否可以从当前表单创建图像?例如 onLoad 在根文件夹中创建表单当前状态的图片?将用户看到的内容存储为图片。
【问题讨论】:
试试这个,
using (var bmp = new Bitmap(this.Width, this.Height))
{
this.DrawToBitmap(bmp, new Rectangle(0, 0, bmp.Width, bmp.Height));
bmp.Save(@"c:\temp\screenshot.png");
}
这将使用表单的宽度和高度(this.Width,this.Height,如果您使用当前表单将屏幕写入磁盘)并将其绘制到位图!
http://msdn.microsoft.com/en-us/library/system.windows.forms.control.drawtobitmap.aspx
【讨论】:
这将返回一个没有边框、滚动条、标题等的“FormShot”。要保存它,只需使用 Bitmap.Save 方法。
public static Bitmap TakeWindowScreenshot(Form window)
{
var b = new Bitmap(window.Width, window.Height);
this.DrawToBitmap(b, new Rectangle(0, 0, window.Width, window.Height));
Point p = window.PointToScreen(Point.Empty);
Bitmap target = new Bitmap( window.ClientSize.Width, window.ClientSize.Height);
using (Graphics g = Graphics.FromImage(target))
{
g.DrawImage(b, 0, 0,
new Rectangle(p.X - window.Location.X, p.Y - window.Location.Y,
target.Width, target.Height),
GraphicsUnit.Pixel);
}
b.Dispose();
return target;
}
【讨论】: