A generic error occurred in GDI+, JPEG Image to MemoryStream

前端 未结 30 1441
再見小時候
再見小時候 2020-11-22 06:47

This seems to be a bit of an infamous error all over the web. So much so that I have been unable to find an answer to my problem as my scenario doesn\'t fit. An exception ge

30条回答
  •  [愿得一人]
    2020-11-22 07:01

    This article explains in detail what exactly happens: Bitmap and Image constructor dependencies

    In short, for a lifetime of an Image constructed from a stream, the stream must not be destroyed.

    So, instead of

    using (var strm = new ... )  {
        myImage = Image.FromStream(strm);
    }
    

    try this

    Stream imageStream;
    ...
    
        imageStream = new ...;
        myImage = Image.FromStream(strm);
    

    and close imageStream at the form close or web page close.

提交回复
热议问题