MemoryStream 的 ReadTimeout 异常 [英] ReadTimeout exception with MemoryStream

查看:18
本文介绍了MemoryStream 的 ReadTimeout 异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面的方法抛出这个异常

The following method throws this exception

System.IO.Stream)(ms)).ReadTimeout 抛出类型异常System.InvalidOperationException'

System.IO.Stream)(ms)).ReadTimeout threw an exception of type System.InvalidOperationException'

这是方法:

private static byte[] ImageToByteArraybyMemoryStream(Bitmap bmp)
{
    using (MemoryStream ms = new MemoryStream()) {
        bmp.Save(ms, bmp.RawFormat);        
        return ms.ToArray();
    }
}

但是,此错误不会一直发生.我将尝试简短地解释会发生什么:

however, this error doesn't occur all the time. I'll try to explain in short what happens:

  • 我从文件加载位图,显示它并将其存储在Dictionary
  • 当应用程序关闭时,我将位图作为 byte[] 写入应用程序配置文件(并且没有例外)
  • 在应用程序启动时,我从配置文件加载位图并显示它
  • 当用户更改应用程序数据(如调整大小或移动位图)时,我会像从文件加载位图时那样重写配置文件,然后发生此异常.
  • I load a bitmap from a file, display it and store it in Dictionary<int,Bitmap>
  • When application is closed, i write bitmap to apps config-file as byte[] (and there's no exception)
  • On start of app i load bitmap from config-file and display it
  • When user changes application-data (like resizing or moving the bitmap) i rewrite the config-file the same way as i did when bitmap was loaded from file and this exception occurs.

推荐答案

我刚刚搜索了通用 gdi+ 错误",其他人也遇到了 bmp.Save 方法的问题.他们的解决方法是从您要保存的位图创建一个新位图,然后保存此副本.

i just googled "generic gdi+ error" others also got a problem with the bmp.Save Method. the workaround for them is to create a new Bitmap from the one you wanna save and then save this copy.

...
Bitmap copy = new Bitmap(bmp); 
copy.Save(ms, copy.RawfFormat);
...

也许这也适用于你,我不能告诉你这个错误的原因,它被认为是其他网站上的错误.

Maybe this also works for you, i can not tell you the reason for this error, its reffered to as a bug on other sites.

讨论这个问题的其他帖子

这篇关于MemoryStream 的 ReadTimeout 异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆