Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDI+ exception when saving image in PNG format

An ASP.NET application on my server starts throwing GDI+ exception after running for several days. After I restart the server, all works fine for a couple of days and then suddenly this exception occurs again. After the first time it occurs, it occurs every time I try to save a PNG image, until I restart again.

When I try to save the image in JPEG, it works fine.

When I run this code from my project, it fails:

var path = @"C:\Project\images\logo.png";
var image = Image.FromFile(path);

using (var ms = new MemoryStream())
{
    image.Save(ms, ImageFormat.Png);     // Fails here on GDI+ exception.
    //image.Save(ms, ImageFormat.Jpeg);  // JPEG works somehow
}

Again: When I restart remote desktop and run this code, it works for a couple of days and at some moment suddenly starts to fail over and over.

I tried:

  1. To make a console application with the same code and run it in the RDP where the project is. It worked fine!

  2. Lots of different variations of codes that were suggested in more then 10 articles I read on this topic.

  3. GCI.Collect() – no help.

  4. Checked all the folders that has write permissions (maybe there is something with the IIS?).

  5. More.

I think that it should be some configuration that suddenly changes due to something and I can’t understand what can it be.

like image 829
Misha Zaslavsky Avatar asked Aug 31 '16 21:08

Misha Zaslavsky


2 Answers

After I restart the server all works fine for a couple of days and then suddenly this exception occurs and after the first time it occurs it will occur everytime I try to save the PNG image, until I will restart again.

Sounds like a memory leak to me. What .NET version is this compiled to? What server OS this running on?

You can start by enclosing your image in a using block:

var path = @"C:\Project\images\logo.png";
using (Image image = Image.FromFile(path))
{
    using (var ms = new MemoryStream())
    {
        image.Save(ms, ImageFormat.Png);
    }
}

This link I believe is relevant to your case.

like image 140
ZagNut Avatar answered Oct 20 '22 23:10

ZagNut


It throw GDI+ error because one of your object not disposed, so better dispose used object.

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

GDI+ limits the height of an image to 65534

Always favor using the using statement. Which never forgets to dispose an object, even if the code throws an exception.

var path = @"C:\Project\images\logo.png";
using (Image image = Image.FromFile(path))
{
      using (var ms = new MemoryStream())
      {
            image.Save(ms, ImageFormat.Png); //fails here on GDI+ exception.
            //image.Save(ms, ImageFormat.Jpeg); //Jpeg Works somehow
      }
}

The FromFile method locks the file, so use the Image.FromStream() method for reading the image:

byte[] bytes = System.IO.File.ReadAllBytes(filename);
System.IO.MemoryStream ms = new System.IO.MemoryStream(bytes);
using (var ms = new MemoryStream())
{
      image.Save(ms, ImageFormat.Png); //fails here on GDI+ exception.
      //image.Save(ms, ImageFormat.Jpeg); //Jpeg Works somehow
}
like image 30
Sandip - Frontend Developer Avatar answered Oct 20 '22 23:10

Sandip - Frontend Developer