【问题标题】:How do I solve this error in Stream.ReadTimeOut error?如何解决 Stream.ReadTimeOut 错误中的此错误?
【发布时间】:2017-10-06 09:41:54
【问题描述】:

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

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

我将它包装在 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'

我有两个问题:

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

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

在这里继续崩溃,参数无效。 对象流仍有读写 TimeOut 消息。

【问题讨论】:

  • 你在哪里得到错误?在哪两条线?什么是确切的错误消息?顺便说一句,您应该使用使用内存流
  • 异常发生在此处显示的第二行代码中。只有当我将鼠标悬停在流上时,才会在第一行代码中显示 TimeOut 错误。
  • 什么是fcr,图片?什么样的?
  • fcr 是 png 图片文件
  • 两者都不为空。图片存在

标签: c# asp.net-mvc


【解决方案1】:

您需要在使用前更改位置。

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

【讨论】:

  • 这是我的问题。您能否指出解释这一点的文档?
  • @Akaisteph7 我不知道这是否记录在案。我通过下载.Net Framework源代码并调试发现问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-12
  • 2012-02-14
  • 2015-04-12
  • 2016-06-21
  • 2020-11-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多