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

前端 未结 30 1378
再見小時候
再見小時候 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:21
    • I had this issue on a test server but not on the live server.
    • I was writing the image to a stream, so it wasn't a permission issue.
    • I'd been directly deploying some of the .dll's to the test server.
    • Deploying the entire solution fixed the issue, so it was probably a weird compilation mismatch
    0 讨论(0)
  • 2020-11-22 07:22

    I'll add this cause of the error as well in hopes it helps some future internet traveler. :)

    GDI+ limits the maximum height of an image to 65500

    We do some basic image resizing, but in resizing we try to maintain aspect ratio. We have a QA guy who's a little too good at this job; he decided to test this with a ONE pixel wide photo that was 480 pixels tall. When the image was scaled to meet our dimensions, the height was north of 68,000 pixels and our app exploded with A generic error occurred in GDI+.

    You can verify this yourself with test:

      int width = 480;
      var height = UInt16.MaxValue - 36; //succeeds at 65499, 65500
      try
      {
        while(true)
        {
          var image = new Bitmap(width, height);
          using(MemoryStream ms = new MemoryStream())
          {
            //error will throw from here
            image.Save(ms, ImageFormat.Jpeg);
          }
          height += 1;
        }
      }
      catch(Exception ex)
      {
        //explodes at 65501 with "A generic error occurred in GDI+."
      }
    

    It's too bad there's not a friendly .net ArgumentException thrown in the constructor of Bitmap.

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

    Same problem I was facing. But in my case, I was trying to save file in C drive and it was not accessible. So I tried it to save in D drive which was fully accessible and I succeeded.

    So first check your folders in which you are trying to save. You must have all (read and write) rights for that particular folder.

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

    in my case, path was wrong

    just use this

    String path = Server.MapPath("~/last_img.png");//Path
    
    0 讨论(0)
  • 2020-11-22 07:25

    I also get this error because i'm trying to save images with the same name of previous saved images.

    Make sure that you don't save images with duplicate name.

    Use for thar for example a 'Random' function (How does C#'s random number generator work?) or for example generate a Guid (http://betterexplained.com/articles/the-quick-guide-to-guids/)

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

    OK I seem to have found the cause just by sheer luck and its nothing wrong with that particular method, it's further back up the call stack.

    Earlier I resize the image and as part of that method I return the resized object as follows. I have inserted two calls to the above method and a direct save to a file.

    // At this point the new bitmap has no MimeType
    // Need to output to memory stream
    using (var m = new MemoryStream())
    {
           dst.Save(m, format);
    
           var img = Image.FromStream(m);
    
           //TEST
           img.Save("C:\\test.jpg");
           var bytes = PhotoEditor.ConvertImageToByteArray(img);
    
    
           return img;
     }
    

    It appears that the memory stream that the object was created on has to be open at the time the object is saved. I am not sure why this is. Is anyone able to enlighten me and how I can get around this.

    I only return from a stream because after using the resize code similar to this the destination file has an unknown mime type (img.RawFormat.Guid) and Id like the Mime type to be correct on all image objects as it makes it hard write generic handling code otherwise.

    EDIT

    This didn't come up in my initial search but here's the answer from Jon Skeet

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