Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c# Out of Memory Exception with System.Drawing.Image

I wanna create a pdf with 2 images. One of the image is a text and the other is a watermark to draw on top of the first one. Well when I load the first image everything is ok but then I try to load the watermark image and get the "Out of Memory" exception. I've got memory (printed the memory usage was like 20MB) and can open the image in my computer (I'm using one I took from google just to test until I don't get the real one).

Image I'm using to test this feature

The code where I get the exception is this one:

      using (System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile(sOriginalPath, true))
      {
        using (System.Drawing.Image imgLogo = System.Drawing.Image.FromFile(sLogoPath, true)) //This is where it throws the exception
        {
          using (Graphics gra = Graphics.FromImage(imgOriginal))
          {
            Bitmap bmLogo = new Bitmap(imgLogo);
            int nWidth = bmLogo.Size.Width;
            int nHeight = bmLogo.Size.Height;
            int nLeft = (imgOriginal.Width / 2) - (nWidth / 2);
            int nTop = (imgOriginal.Height / 2) - (nHeight / 2);
            gra.DrawImage(bmLogo, nLeft, nTop, nWidth, nHeight);
          }
          return imgOriginal;
        }
      }

I've seen the other questions like mine but:

  • It doesn't seem memory problem
  • It doesn't seem image problem

Can you help me? Thanks :)

like image 635
patricia Avatar asked Nov 17 '25 11:11

patricia


1 Answers

Issue

You are building an object

  using (System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile(sOriginalPath, true))

Then you are returning it...but it is already disposed of...you need to not dispose of the object by unwrapping it with a using...whatever consumes this will need to dispose of the object.

Other Issue

bitmap is also a memory leak and needs to be wrapped with a using or dispose called implicitly.


Final Function Example

public System.Drawing.Image GetImage(string sOriginalPath, string sLogoPath)
{
  System.Drawing.Image imgOriginal = System.Drawing.Image.FromFile(sOriginalPath, true);
  using (System.Drawing.Image imgLogo = System.Drawing.Image.FromFile(sLogoPath, true)) //This is where it throws the exception
  {
    using (Graphics gra = Graphics.FromImage(imgOriginal))
    {
      using(Bitmap bmLogo = new Bitmap(imgLogo)) 
      {
        int nWidth = bmLogo.Size.Width;
        int nHeight = bmLogo.Size.Height;
        int nLeft = (imgOriginal.Width / 2) - (nWidth / 2);
        int nTop = (imgOriginal.Height / 2) - (nHeight / 2);
        gra.DrawImage(bmLogo, nLeft, nTop, nWidth, nHeight);
      }
    }
  }
  return imgOriginal;
}

Example Console App Demo

I've tested the below and it worked as expected.

using System.Drawing;

namespace SO_Test
{
    class Program
    {
        static void Main(string[] args)
        {
            using(Image newImage = GetImage("C:\\Users\\username\\Pictures\\image.png", "C:\\Users\\username\\Pictures\\watermark.jpg"))
            {
                newImage.Save("C:\\Users\\username\\Pictures\\newImage.png");
            }
        }

        static Image GetImage(string sOriginalPath, string sLogoPath)
        {
            Image imgOriginal = Image.FromFile(sOriginalPath, true);
            using (Image imgLogo = Image.FromFile(sLogoPath, true)) //This is where it throws the exception
            {
                using (Graphics gra = Graphics.FromImage(imgOriginal))
                {
                    using (Bitmap bmLogo = new Bitmap(imgLogo))
                    {
                        int nWidth = bmLogo.Size.Width;
                        int nHeight = bmLogo.Size.Height;
                        int nLeft = (imgOriginal.Width/2) - (nWidth/2);
                        int nTop = (imgOriginal.Height/2) - (nHeight/2);
                        gra.DrawImage(bmLogo, nLeft, nTop, nWidth, nHeight);
                    }
                }
            }
            return imgOriginal;
        }
    }
}
like image 163
abc123 Avatar answered Nov 20 '25 02:11

abc123



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!