C# 将windows窗体捕获到图像中

C# 将windows窗体捕获到图像中,c#,capture,C#,Capture,我知道这个问题以前有人回答过。但没有一个答案是正确的。 如何捕获windows窗体的映像并保存它。我用这个: Bitmap bmp = new Bitmap(this.Width, this.Height); this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size)); bmp.Save(@"C://Desktop//sample.png",ImageFormat.Png); 但是得到错误: GDI中发生一般性错误+ 我也读过这个错

我知道这个问题以前有人回答过。但没有一个答案是正确的。 如何捕获windows窗体的映像并保存它。我用这个:

Bitmap bmp = new Bitmap(this.Width, this.Height);
this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
bmp.Save(@"C://Desktop//sample.png",ImageFormat.Png);
但是得到错误:

GDI中发生一般性错误+


我也读过这个错误,但是没有一个建议对我有用!请帮助

您的路径格式错误。应该是:

bmp.Save("C:\\Desktop\\sample.png",ImageFormat.Png);
更重要的是-检查文件夹是否存在

问题出在bmp中。Save@C://Desktop//sample.png,ImageFormat.png

首先它必须是@C:\Desktop\sample.png,您不需要用它来转义任何内容

其次,确保路径正确,并且您有写入权限

第三,正如Sayse所指出的,处理位图

using(Bitmap bmp = new Bitmap(this.Width, this.Height))
{
    this.DrawToBitmap(bmp, new Rectangle(Point.Empty, bmp.Size));
    bmp.Save(@"C:\Desktop\sample.png",ImageFormat.Png); // make sure path exists!
}

你确定你没有忘记处理位图吗?编辑:效果很好。非常感谢。请忽略以下内容,谢谢。事实上,这首先是我的问题。如何处理位图?通过使用bmp.Dispose?当我在保存之前使用此选项时,会出现错误。当我在保存后使用它时,我会得到相同的GDI+错误