0

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.

A. Abramov
  • 1,823
  • 17
  • 45
  • 2
    The answer is right in your link: PDF is not in the list of supported formats! Reading PDF will take an external library (shh, we can't talk about it here..) How to display PDF is one of the afaik unanswered questions :-( – TaW Jan 10 '16 at 17:29
  • @TaW how would you read a PDF into an Image then? Is it possible? If it isn't, can you convert it into a `System.IO.Stream`? – A. Abramov Jan 10 '16 at 17:30
  • Well, that depends on what you want to achieve; any file can be read into a (binary) stream, but that does not mean that anybody can convert it to an image; which a pdf simply is not.. – TaW Jan 10 '16 at 17:33
  • @TaW Thank you very much, you helped me reach the solution myself, which's actually taking the `System.Drawing` out of the picture. :) – A. Abramov Jan 10 '16 at 17:41

1 Answers1

1

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;
Community
  • 1
  • 1
A. Abramov
  • 1,823
  • 17
  • 45