简体   繁体   English

将窗体捕获到图像中

[英]Capturing windows form into image

I know this question has been answered before. 我知道这个问题已经回答过了。 But none of the answers are correct. 但是,没有一个答案是正确的。 How can I capture the image of a windows form and save it. 如何捕获Windows窗体的图像并将其保存。 I use this: 我用这个:

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);

but get the error: 但得到错误:

A generic error occurred in GDI+ GDI +中发生一般错误

I have also read about this error but none of the suggestions work for me! 我还阅读了有关此错误的信息,但没有任何建议对我有用! Please help 请帮忙

The problem is in bmp.Save(@"C://Desktop//sample.png",ImageFormat.Png); 问题出在bmp.Save(@"C://Desktop//sample.png",ImageFormat.Png); .

First it must be @"C:\\Desktop\\sample.png", you don't need to escape anything with verbatim string . 首先,它必须是@“ C:\\ Desktop \\ sample.png”,您不需要使用逐字字符串转义任何内容。

Second make sure the path is correct and you have permission to write to. 其次,请确保路径正确,并且您具有写入权限。

Third as Sayse point out dispose the bitmap. 正如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!
}

Your path is in wrong format. 您的路径格式错误。 Should be: 应该:

bmp.Save("C:\\Desktop\\sample.png",ImageFormat.Png);

What is more - check if the folder exists 更重要的是-检查文件夹是否存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM