我想加载图像流,以便可以使用PDFsharp将其转换为pdf。
以下代码在我的test / console-application中工作,但在我使用ASP.NET Framework 4.6.1运行的项目中没有。这是因为我使用 System.Drawing.Image 。
不支持在Windows或ASP.NET服务中使用System.Drawing.Imaging命名空间中的类。尝试在其中一种应用程序类型中使用这些类可能会产生意外问题,例如服务性能下降和运行时异常。
来源:Microsoft
我们如何解决这个问题?我的偏好是把一切都记在心里。
我在控制台应用程序中的工作代码:
PdfDocument pdfDoc = new PdfDocument();
pdfDoc.Pages.Add(new PdfPage());
MemoryStream MemStream = new MemoryStream(inputStream); // Inputstream as byte[]
Image imgSource = Image.FromStream(MemStream); // This will fail
XImage imgX = XImage.FromGdiPlusImage(imgSource);
XGraphics xgr = XGraphics.FromPdfPage(pdfDoc.Pages[0]);
xgr.DrawImage(imgX, 0, 0);
MemoryStream outputStream = new MemoryStream();
pdfDoc.Save(outputStream, false);
byte[] dataConverted = outputStream.ToArray();
pdfDoc.Close();
return dataConverted;
答案 0 :(得分:2)
只需将信息流传递给PDFsharp:
public static XImage FromStream(Stream stream)
使用PDFsharp 1.50,此方法是公开的。我认为版本1.3x并不公开。
使用PDFsharp的WPF构建 - GDI构建在内部使用Image.FromStream
。