Click here to Skip to main content
11,565,382 members (37,844 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# GDI+ ImageProcessing
Hello everyone. I'm creating a little manipulation image software.
The problem is, when the process is ended, the memory doesn't unload, and later the program crashes.

        public Image WatermarkImages(Image ImagetoWatermark, Image Watermark)
        {
            Bitmap imgBack = new Bitmap(ImagetoWatermark);
            Bitmap imgWaterMark = new Bitmap(Watermark);
            Graphics imgBackGraph = Graphics.FromImage(imgBack);
            Point PointImagePos = WatermarkImagePosition(imgBack.Height, imgBack.Width, imgWaterMark.Height, imgWaterMark.Width);
            Rectangle WaterMarkRectangle = new Rectangle(PointImagePos, new Size(imgWaterMark.Width, imgWaterMark.Height));
            imgBackGraph.DrawImage(imgWaterMark, WaterMarkRectangle, 0, 0, imgWaterMark.Width, imgWaterMark.Height, GraphicsUnit.Pixel);
            Image ImgtoReturn = imgBack;
            return ImgtoReturn;
        }
 
        private void WatermarkImages
        {
            for (int i = 0; i < listBox1.Items.Count; i++)
            {                
                FileInfo filename = new FileInfo(listBox1.Items[i].ToString());
                string ImageFileName = filename.Name;
                Image Img = WaterImage.WatermarkImages(Image.FromFile(filename.FullName), Image.FromFile(WatermarkImagePath));
                Img.Save(folderBrowserDialog1.SelectedPath + "\\" + ImageFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                Img.Dispose();
            }
            
        }

How I can repair this?
Thanks
Posted 6-May-12 12:31pm
TANicox1.3K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Hi,

Try this coding.
I've just updated your code, with proper dispose methods.
Try n update your feedback.

public Image WatermarkImages(Image ImagetoWatermark, Image Watermark)
        {
            Bitmap imgBack = new Bitmap(ImagetoWatermark);
            //No need to create watermark image again
            //Bitmap imgWaterMark = new Bitmap(Watermark);
            //Again use using to create and dispose graphics object
            using (Graphics imgBackGraph = Graphics.FromImage(imgBack))
            {
                Point PointImagePos = WatermarkImagePosition(imgBack.Height, imgBack.Width, imgWaterMark.Height, imgWaterMark.Width);
                Rectangle WaterMarkRectangle = new Rectangle(PointImagePos, new Size(imgWaterMark.Width, imgWaterMark.Height));
                imgBackGraph.DrawImage(imgWaterMark, WaterMarkRectangle, 0, 0, imgWaterMark.Width, imgWaterMark.Height, GraphicsUnit.Pixel);
            }
            // No need to create a handle again. Just return it   
            //Image ImgtoReturn = imgBack;
            return imgBack;
 
        }
 
        private void WatermarkImages()
        {
            //No need to recreate image from file again and again as you are using the same image as watermark
            Image WMImage = Image.FromFile(WatermarkImagePath);
 
            for (int i = 0; i < listBox1.Items.Count; i++)
            {
                FileInfo filename = new FileInfo(listBox1.Items[i].ToString());
                string ImageFileName = filename.Name;
                //using will definetly dispose off. so no need to explicitly call dispose
                using (Image Img)
                {
                    Img = WaterImage.WatermarkImages(Image.FromFile(filename.FullName), WMImage);
                    Img.Save(folderBrowserDialog1.SelectedPath + "\\" + ImageFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                    Img.Dispose();
                }
            }
 
        }
  Permalink  
Comments
TANicox at 8-May-12 21:10pm
   
I tried the code, but the problem persist... When I call the WatermarkImages event, the memory doesn't unload

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS


Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | Last Updated 7 May 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100