A. Abramov
A. Abramov

Reputation: 1859

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

I'm trying to read into a System.Drawing.Image structure a PDF file from a local path.

I used Image.fromPath(string fileName) - and it thrown me an exception:

Out of memory.

I read the documentation, and I understood that that this exception can be thrown in one of two cases -

The file does not have a valid image format.

-or-

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

My question is - which one is it, and how can I read the image despite this?

Note: Eventually, the image becomes a System.IO.Stream - if you can convert the PDF into this structure, it will help me just as much.

Upvotes: 0

Views: 1131

Answers (1)

A. Abramov
A. Abramov

Reputation: 1859

This question was answered before, here.

What I wanted to do is to get a stream from a local path, and for that I dont even have to use System.Drawing - which doesnt even include PDF's in the images, but instead, System.IO -

A memory stream can be constructed by all the bytes in a file. All I had to do is give the local path, read the data from the file, and return it as a memory stream, like so:

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

Upvotes: 1

Related Questions