ReadTimeout exception with MemoryStream

别说谁变了你拦得住时间么 提交于 2019-12-25 01:18:52

问题


The following method throws this exception

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

This is the method:

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:

  • 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.

回答1:


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.

Other Post discussing this Problem



来源:https://stackoverflow.com/questions/28172110/readtimeout-exception-with-memorystream

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!