1

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.
Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
aw48
  • 19
  • 1
  • 1
  • 5
  • at which call in this method the exception is thrown? Can you post the whole Exception and Stacktrace? – BoeseB Jan 27 '15 at 14:02
  • Did you convert your byte array to any string format before writing it out on the config file ? – Mahesh Jan 27 '15 at 14:16
  • @BoeseB at line bmp.save(..)the actual message is generic gdi+ error – aw48 Jan 27 '15 at 14:22
  • @coder of code : there's no conversion and exception occurs when creating byte[] – aw48 Jan 27 '15 at 14:26
  • @Tim thanks for editung my post – aw48 Jan 27 '15 at 14:29
  • @aw48: if you save the bitmap as rawbytes into a textfile i would suggest you convert it to a Base64 string to store it instead of using the raw bytes. Storing the raw bytes is error prone. Look at System.Convert.ToBase64String and System.Convert.FromBase64String for this – BoeseB Jan 27 '15 at 14:49

2 Answers2

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

Community
  • 1
  • 1
BoeseB
  • 695
  • 4
  • 17
  • but why does it work once ? just tried ImageConverter instead of memorystream and it crashes too. – aw48 Jan 27 '15 at 14:56
  • my guess is the first time you read the image from a imagefile. On the second start you read it from your config file. When you read it from your config file it could be that some resources are not freed correctly and so the save method fails. Or by storing and loading the bytearray as rawbytes in a textfile, there are errors in the loaded bytearray, that cause the save method to fail but don't cause errors while displaying. All just guessing... – BoeseB Jan 27 '15 at 15:01
  • you saved my project !!! after changing back to the original bmp.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp) your suggestion the copy works. – aw48 Jan 27 '15 at 15:17
0

I faced this issue and it took a while to understand that, the object that uploaded the image did not release its located memory and lock it! and the garbage collection didn't dispose of that object, therefore the API could not stream the image. somehow it works for the first time then it will raise the error.

Solution: I used HttpPostFileBase to upload the image, and after it has done I set it to null

and that's it!

Ahmed Abd Elmoniem
  • 157
  • 1
  • 4
  • 12