我正在尝试使用以下代码将webform保存为图像:
using System.Drawing.Printing;
using System.Drawing;
using System.Drawing.Imaging;
protected void btntopdf_Click(object sender, EventArgs e)
{
Capture("D://ScreenShot.jpg");//path to Save Captured files
}
public static void Capture(string CapturedFilePath)
{
Bitmap bitmap = new Bitmap
(System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width, System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height);
Graphics graphics = Graphics.FromImage(bitmap as System.Drawing.Image);
graphics.CopyFromScreen(25, 25, 25, 25, bitmap.Size);
bitmap.Save(CapturedFilePath, ImageFormat.Jpeg);//ERROR
bitmap.Dispose();
graphics.Dispose();
}
我正在A generic error occurred in GDI+.
栈跟踪
[ExternalException (0x80004005): A generic error occurred in GDI+.]
System.Drawing.Image.Save(String filename, ImageCodecInfo encoder, EncoderParameters encoderParams) +474386
System.Drawing.Image.Save(String filename, ImageFormat format) +69
UNO.EmployeePerformanceCreate.Capture(String CapturedFilePath) in D:\Project\UNO\UNO\EmployeePerformanceCreate.aspx.cs:2258
UNO.EmployeePerformanceCreate.btntopdf_Click(Object sender, EventArgs e) in D:\Project\UNO\UNO\EmployeePerformanceCreate.aspx.cs:2208
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628114
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
答案 0 :(得分:1)
不是直接将Bitmap保存到路径,而是尝试如下:
protected void Page_Load(object sender, EventArgs e)
{
Bitmap bmpScreenshot = Screenshot();
Bitmap bm;
byte[] ss = ImageToByte(bmpScreenshot);
using (var ms = new MemoryStream(ss))
{
bm = new Bitmap(ms);
bm.Save("D:\\Test\\testimage.jpeg", ImageFormat.Jpeg);
}
}
public static byte[] ImageToByte(System.Drawing.Image img)
{
ImageConverter converter = new ImageConverter();
return (byte[])converter.ConvertTo(img, typeof(byte[]));
}
private Bitmap Screenshot()
{
Bitmap bmpScreenshot =
new Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height);
using (var g = Graphics.FromImage(bmpScreenshot))
{
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
return bmpScreenshot;
}
}