问题
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