5

I am getting an exception in the following code:

(System.IO.MemoryStream) Stream stream = new MemoryStream(fcr.FileContents);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);

I wrapped it in a Try/Catch & the exception is thrown on the second line: Message = "Parameter is not valid."

I checked the contents of the variable stream. This is where the error is. Expanding the contents of stream, I find:

Read.TimeOut: 'stream.Read.TimeOut'threw an exception of type 
'System.InvalidOperationException'
Write.TimeOut: 'stream.Write.TimeOut' thew an exception of type 
'System.InvalidOperationException'

I have 2 questions:

  1. Why wasn't the first error picked up in the Try/Catch?
  2. How do I solve it?

    using (Stream stream = new MemoryStream(fcr.FileContents))
            {
            System.Drawing.Image img = System.Drawing.Image.FromStream(stream, true, true);
    

Continues to crash in here with Parameter is not valid. The object stream still has read & write TimeOut messages.

Steve Staple
  • 2,983
  • 9
  • 38
  • 73
  • where do you get the error? in which of the two lines? whats the exact error msg? btw you should use a using using a memory stream – WhileTrueSleep May 08 '17 at 11:47
  • The exception occurs in the second line of code shown here. The TimeOut errors were only shown when I hover over stream, in the first line of code. – Steve Staple May 08 '17 at 11:55
  • What is `fcr`, an image? What kind? – Timothy G. May 08 '17 at 11:57
  • fcr is a png image file – Steve Staple May 08 '17 at 11:58
  • neither are null. the image exists – Steve Staple May 08 '17 at 12:03
  • Maybe the stream is being disposed first. Like [this question](http://stackoverflow.com/questions/43069026/stream-readtimeout-threw-an-exception-of-type-system-invalidoperationexceptio/43069192). However, I don't see a using in your code. It's probably related to something like this at least. – Timothy G. May 08 '17 at 12:11
  • Putting the statement inside a using statement made no difference whatsoever. – Steve Staple May 08 '17 at 12:17
  • Make sure you set the offset of memory stream to zero after writing. The offset is automatically set to end of stream which may give this error. If you are reading from a file you need to use UTF8 encoding. So normally I use a binary reader to read from the memory stream an image. or use any stream type the you can set the encoding to UTF8. – jdweng May 08 '17 at 12:30
  • thanks jdweng. How do I I do that? i.e set the offset of memory stream to zero - and use UTF8 encoding? – Steve Staple May 08 '17 at 13:17
  • I have tried this with a jpg image file, and it works. Do you still think I need to set the offset of memory stream to zero after writing & use UTF8 encoding? – Steve Staple May 08 '17 at 14:40
  • What is the type of the variable `fcr`? – Ofir Winegarten May 09 '17 at 15:36

1 Answers1

6

You need to change the position before using it.

using (Stream stream = new MemoryStream(fcr.FileContents))
{
    stream.Position = 0;
    System.Drawing.Image img = System.Drawing.Image.FromStream(stream, true, true);
}
Just a learner
  • 26,690
  • 50
  • 155
  • 234