使用 Image.fromPath 读取 PDF 文件时出现内存不足异常

Out of memory exception reading a PDF file with Image.fromPath

我正在尝试从本地路径将 PDF 文件读入 System.Drawing.Image 结构。

我使用了 Image.fromPath(string fileName) - 它抛出了一个异常:

Out of memory.

我阅读了文档,我了解到在以下两种情况之一中可能会抛出此异常 -

The file does not have a valid image format.

-或-

GDI+ does not support the pixel format of the file.

我的问题是 - 它是哪一个,尽管如此我如何读取图像?

注意: 最终,图像变成了System.IO.Stream - 如果你能将PDF转换成这种结构,它会帮助我。

这个问题之前有人回答过,here.

我想做的是从本地路径获取流,为此我什至不必使用 System.Drawing - 它甚至不在图像中包含 PDF,而是 System.IO-

内存流可以由一个文件中的所有字节组成。我所要做的就是给出本地路径,从文件中读取数据,然后 return 将其作为内存流,如下所示:

var pdfContent = new MemoryStream(System.IO.File.ReadAllBytes(imageLocation));
pdfContent.Position = 0; // ensuring the stream is reading from the start.
return pdfContent;