如何解决 Stream.ReadTimeOut 错误中的此错误?

Ste*_*ple 5 c# asp.net-mvc

我在以下代码中遇到异常:

(System.IO.MemoryStream) Stream stream = new MemoryStream(fcr.FileContents);
System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
Run Code Online (Sandbox Code Playgroud)

我将它包装在 Try/Catch 中,并且在第二行抛出异常:Message = "Parameter is not valid."

我检查了变量的内容stream。这就是错误所在。展开 的内容stream,我发现:

Read.TimeOut: 'stream.Read.TimeOut'threw an exception of type 
'System.InvalidOperationException'
Write.TimeOut: 'stream.Write.TimeOut' thew an exception of type 
'System.InvalidOperationException'
Run Code Online (Sandbox Code Playgroud)

我有两个问题:

  1. 为什么在 Try/Catch 中没有发现第一个错误?
  2. 我该如何解决?

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

在这里继续崩溃,参数无效。对象流仍然具有读取和写入超时消息。

Jus*_*ner 6

使用前需要改变位置。

using (Stream stream = new MemoryStream(fcr.FileContents))
{
    stream.Position = 0;
    System.Drawing.Image img = System.Drawing.Image.FromStream(stream, true, true);
}
Run Code Online (Sandbox Code Playgroud)