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

前端 未结 30 1256
再見小時候
再見小時候 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 06:59

    I found that if one of the parent folders where I was saving the file had a trailing space then GDI+ would throw the generic exception.

    In other words, if I tried to save to "C:\Documents and Settings\myusername\Local Settings\Temp\ABC DEF M1 Trended Values \Images\picture.png" then it threw the generic exception.

    My folder name was being generated from a file name that happened to have a trailing space so it was easy to .Trim() that and move on.

    0 讨论(0)
  • 2020-11-22 06:59

    SOLVED - I had this exact problem. The fix, for me, was to up the disk quota for IUSR on the IIS server. In this instance, we have a catalog app with images of items and such. The upload quota for the "Anonymous Web User" was set to 100MB, which is the default for this particular hosting company's IIS servers. I upped it to 400MB and was able to upload images without error.

    This might not be your issue, but if it is, it's an easy fix.

    0 讨论(0)
  • 2020-11-22 06:59

    Just to throw another possible solution on the pile, I'll mention the case I ran into with this error message. The method Bitmap.Save would throw this exception when saving an bitmap I had transformed and was displaying. I discovered it would not throw the exception if the statement had a breakpoint on it, nor would it if the Bitmap.Save was preceeded by Thread.Sleep(500) so I suppose there is some sort of resource contention going on.

    Simply copying the image to a new Bitmap object was enough to prevent this exception from appearing:

    new Bitmap(oldbitmap).Save(filename);
    
    0 讨论(0)
  • 2020-11-22 07:00

    if your code is as follows then also this error occurs

    private Image GetImage(byte[] byteArray)
    {
       using (var stream = new MemoryStream(byteArray))
       {
           return Image.FromStream(stream);
        }
    }
    

    The correct one is

    private Image GetImage(byte[] byteArray)
    {
       var stream = new MemoryStream(byteArray))
       return Image.FromStream(stream);        
    }
    

    This may be because we are returning from the using block

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-22 07:02

    If you are getting that error , then I can say that your application doesn't have a write permission on some directory.

    For example, if you are trying to save the Image from the memory stream to the file system , you may get that error.

    Please if you are using XP, make sure to add write permission for the aspnet account on that folder.

    If you are using windows server (2003,2008) or Vista, make sure that add write permission for the Network service account.

    Hope it help some one.

    0 讨论(0)
提交回复
热议问题