【发布时间】:2010-05-05 21:21:16
【问题描述】:
这是我将 pdf 转换为 png 图像的函数,它会引发错误 这一行——> stream.WriteTo(Response.OutputStream);有什么不对吗??
protected void CreatePngFromPdf()
{
try
{
string PDFLocation = string.Format(@"\\XXXX\{0}\{1}\{2}.pdf", Yr, Loc.Substring(0, 4), Loc.Substring(4, 4));
Utilities.WebPDF.PDF WebPDF = new DocuvaultMVC.Utilities.WebPDF.PDF();
WebPDF.Credentials = new NetworkCredential(@"xyz", "xyz");
byte[] png = WebPDF.StreamPdfPageAsPngResize(PDFLocation,PageNumber, 612, 792);
MemoryStream ms = new MemoryStream(png);
MemoryStream stream = new MemoryStream();
int newWidth = 612;
int newHeight = 792;
System.Drawing.Image newImg = System.Drawing.Image.FromStream(ms);
Bitmap temp = new Bitmap(newWidth, newHeight, newImg.PixelFormat);
Graphics newImage = Graphics.FromImage(temp);
newImage.DrawImage(newImg, 0, 0, newWidth, newHeight);
newImg.Dispose();
temp.Save(stream, ImageFormat.Png);
stream.WriteTo(Response.OutputStream);
temp.Dispose();
stream.Dispose();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
【问题讨论】:
-
哦,天哪,如果抛出异常,这将泄漏句柄 :-) 在处理流和 GDI+ 对象等一次性资源时,您真的应该考虑使用
using。 -
Pinu,有关异常的一些详细信息会有所帮助。还有调用 this 的上下文。 (ASPX还是ASHX,哪个事件?)代码本身基本没问题,除了@Darin做的注释。
-
@Darin,我不明白你,你能给我一个例子,说明我如何使用它吗?
-
@Pinu,你得到了什么确切的例外?
DocuvaultMVC.Utilities.WebPDF是什么? -
这是对我正在使用的网络服务的引用