您可以創建位圖圖像渲染像素的一種形式:
// Assuming this code is within the form code-behind,
// so this is instance of Form class.
using (var bmp = new System.Drawing.Bitmap(this.Width, this.Height))
{
this.DrawToBitmap(bmp, new Rectangle(0, 0, this.Width, this.Height));
bmp.Save("formScreenshot.bmp"); //or change another format.
}
爲了保持乾淨,你可以創建擴展方法。例如:
public static class FormExtentions
{
public static System.Drawing.Bitmap TakeScreenshot(this Form form)
{
if (form == null)
throw new ArgumentNullException("form");
form.DrawToBitmap(bmp, new Rectangle(0, 0, form.Width, form.Height));
return bmp;
}
public static void SaveScreenshot(this Form form, string filename, System.Drawing.Imaging.ImageFormat format)
{
if (form == null)
throw new ArgumentNullException("form");
if (filename == null)
throw new ArgumentNullException("filename");
if (format == null)
throw new ArgumentNullException("format");
using (var bmp = form.TakeScreenshot())
{
bmp.Save(filename, format);
}
}
}
形式的代碼隱藏內部用法:
this.SaveScreenshot("formScreenshot.png",
System.Drawing.Imaging.ImageFormat.Png); //or other formats
注:DrawToBitmap
將只繪製屏幕上顯示內容。
編輯:而在OP影像png
你可以使用:bmp.Save("formScreenshot.png", System.Drawing.Imaging.ImageFormat.Png);
看起來像你打印您的表單元素的圖像。考慮到你的平均桌面屏幕至多100dpi,而你可能瞄準的打印機至少300dpi,你總是會得到糟糕的質量。這意味着不使用屏幕渲染打印源。 –
我該怎麼辦? – Elshan
@Elshan它是winforms嗎? – Leri