[英]Take a ScreenShot of my Control - Winform
我想对Winform上的控件进行截图。 我使用此功能-在此站点中找到:
public void GetImage()
{
var bm = new Bitmap(display1.Width, display1.Height);
display1.DrawToBitmap(bm, display1.ClientRectangle);
bm.Save(@"c:\whatever.gif", ImageFormat.Gif);
}
但是,当我使用它时,在System.Drawing.dll中出现有关'System.Runtime.InteropServices.ExternalException'的错误:GDI +中发生了一般性错误。
你有什么想法吗? 谢谢 !
- - - - - - - - - - - 编辑 - - - - - - - - - - - - - -
好了,我改变了我的职能,像这样:
public void GetImage()
{
var bm = new Bitmap(display1.Width, display1.Height);
display1.DrawToBitmap(bm, display1.ClientRectangle);
var dlg = new SaveFileDialog { DefaultExt = "png", Filter = "Png Files|*.png" };
var res = dlg.ShowDialog();
if (res == DialogResult.OK) bm.Save(dlg.FileName, ImageFormat.Png);
}
它是可行的,但我现在有一张空白图片:/
请尝试以下操作:假设display1是一个Rectangle,它限制了您要捕获的显示区域。
public void GetImage()
{
Rectangle display1 = this.Bounds; // winforms control bounds
// for full screen use "= Screen.GetBounds(Point.Empty);
var bm = new Bitmap(display1.Width, display1.Height);
//display1.DrawToBitmap(bm, display1.ClientRectangle);
Graphics g = Graphics.FromImage(bm);
g.CopyFromScreen(new Point(display1.Left, display1.Top), Point.Empty, display1.Size);
var dlg = new SaveFileDialog { DefaultExt = "png", Filter = "Png Files|*.png" };
var res = dlg.ShowDialog();
if (res == DialogResult.OK) bm.Save(dlg.FileName, ImageFormat.Png);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.