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.

4
  • 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 :-( Commented Jan 10, 2016 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? Commented Jan 10, 2016 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.. Commented Jan 10, 2016 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. :) Commented Jan 10, 2016 at 17:41

1 Answer 1

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;
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.