byte[] 到 XImage 失败并出现一般 GDI+ 错误

byte[] to XImage Fails with Generic GDI+ Error

我正在使用 PDFsharp 在 PDF 中插入图像。该图像作为数据库中的二进制对象出现。要使用 PDFsharp 插入它,我需要将它作为 XImage。这是我目前正在尝试的:

XImage sealXImage;
using (var ms = new MemoryStream(seal.SealerImage))
// seal.SealerImage is the byte[], I verified this does actually have data in it
{
    Image sealImage = Image.FromStream(ms);
    sealXImage = XImage.FromGdiPlusImage(sealImage);
}
 XGraphics gfx = XGraphics.FromPdfPage(pdfToSeal.Pages[pageNumber]);
 gfx.DrawImage(sealXImage, 425, 475, 150, 100); // generic GDI+ error on this line

所以它一直运行到最后一行,抛出 GDI+ 错误:

System.Runtime.InteropServices.ExternalException: A generic error occurred in GDI+.

我做错了什么,我该怎么做才能解决这个问题?提前致谢。

我怀疑问题出在 using:当您调用 DrawImage() 时,MemoryStream 不再存在。如果我是正确的,在 using 块中调用 DrawImage 应该可以解决问题。

您也可以调用 XImage.FromStream(),但我认为如果不更改 using 范围,您会遇到同样的问题。