How to find reason for Generic GDI+ error when saving an image?

前端 未结 10 1504
心在旅途
心在旅途 2020-11-27 19:03

Having a code that works for ages when loading and storing images, I discovered that I have one single image that breaks this code:

const string i1Pa         


        
相关标签:
10条回答
  • 2020-11-27 19:18

    I found this question because I also faced the similar error and the file was actually created with zero length (if you don't see any file, first check the permissions to write into folder as other answers suggest). Although my code was slightly different (I use stream to read the image from memory, not from file), I think my answer may be helpful to anyone facing similar problem.

    It may looks counter-intuitive, but you can't really dispose memory stream until you finish with image.

    NOT WORKING:

    Image patternImage;
    
    using (var ms = new MemoryStream(patternBytes)) {
        patternImage = new Bitmap(ms);
    }
    
    patternImage.Save(patternFile, ImageFormat.Jpeg);
    

    Just don't dispose the stream until you done with image.

    WORKS:

    using (var ms = new MemoryStream(patternBytes)) {
        patternImage = new Bitmap(ms);
        patternImage.Save(patternFile, ImageFormat.Jpeg);
    }
    

    What is misleading:

    • Error message doesn't really tell you anything
    • You can see the image properties, like width and height, but can't save it
    0 讨论(0)
  • 2020-11-27 19:23

    my solution was to make, write temp content (File.WriteAllText) just before saving the file

    Here is the code:

    var i = Image.FromFile(i1Path);
    File.WriteAllText(i2Path, "empty");  // <---- magic goes here
    i.Save(i2Path, ImageFormat.Jpeg);
    

    Please try and let me know

    0 讨论(0)
  • 2020-11-27 19:23

    Open in the program

    const string i1Path = @"c:\my\i1.jpg";
    
    const string i2Path = @"c:\my\i2.jpg";
    
    var i = Image.FromFile(i1Path);
    
    i.Save(i2Path, ImageFormat.Jpeg);
    
    i.Dispose();
    
    0 讨论(0)
  • 2020-11-27 19:24

    While I still did not find out the reason what exactly caused the error when saving the image, I found a workaround to apply:

    const string i1Path = @"c:\my\i1.jpg";
    const string i2Path = @"c:\my\i2.jpg";
    
    var i = Image.FromFile(i1Path);
    
    var i2 = new Bitmap(i);
    i2.Save(i2Path, ImageFormat.Jpeg);
    

    I.e. by copying the image internally into a Bitmap instance and saving this image instead of the original image, the error disappeared.

    I'm assuming that by copying it, the erroneous parts the caused the original Save call to fail are being removed an/or normalized, thus enabling the save operation to succeed.

    saved image i2.jpg

    Interestingly, the so stored image has a smaller file on disk (16 kB) than its original source (26 kB).

    0 讨论(0)
  • 2020-11-27 19:24

    Solution is here, you must dispose image object to release the memory on the server. Try use using statement. Make sure destination directory on server exists too.

    0 讨论(0)
  • 2020-11-27 19:36

    First of all make sure, that the desired folder has Read/Write permissions. Changing the permissions solved this problem for me.

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