Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I solve this error in Stream.ReadTimeOut error?

Tags:

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.

like image 965
Steve Staple Avatar asked May 08 '17 11:05

Steve Staple


1 Answers

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);
}
like image 83
Just a learner Avatar answered Sep 22 '22 10:09

Just a learner