C# image resizing OutOfMemory error

Associate
Joined
3 Jan 2006
Posts
522
Location
The Undercroft
I'm developing a single user image gallery using C# .NET for my photos and just general coding practice and i've made a control which uploads up to 5 images and resizes them to 4 different sizes. For the most part its working fine except it often throws an out of memory error when it reaches the 4th or 5th image resizing stage.
I wont post all the code but there's 5 fileupload controls on the form and the code goes through them one at a time, saves the original image to the server, extracts EXIF info, then resizes the original image 4 times - Thumb, small, medium and large and saves each one.

Large resize code, does similar thing for each size:
Code:
.
.
 Bitmap largeImg = new Bitmap((Int32)largeNewWidth, (Int32)largeNewHeight);
            Graphics largeGfx = Graphics.FromImage(largeImg);
            largeGfx.FillRectangle(sb, 0, 0, largeImg.Width, largeImg.Height); //sb is a white solid brush
            largeGfx.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
            largeGfx.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
            largeGfx.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
            largeGfx.DrawImage(originalimg, 0, 0, largeImg.Width, largeImg.Height);
            try
            {
                if (!Directory.Exists(largeSavePath))
                {
                    string strFolderToCreate = largeSavePath;
                    if (largeSavePath.EndsWith(@"\"))
                        strFolderToCreate = largeSavePath.Substring(0, (strFolderToCreate.Length - 1));
                    Directory.CreateDirectory(strFolderToCreate);
                }
                largeImg.Save(largeSavePath + "//" + FileName, originalimg.RawFormat);
                Literal1.Text += "<br /><img src='Photos/Large/" + FileName + "' alt='' border='0' /><br />";
            }
            catch (Exception ex)
            {
                lblMessage.Text += " Large image not saved " + ex.Message.ToString();
            }
            finally
            {
                largeImg.Dispose();
            }
.
.
            sb.Dispose();
            originalimg.Dispose();
The images can be up to 5MB each so I expected it to take a fair amount of memory but I think I am freeing up resources properly after each resize (.Dispose()) but I suspect its doing garbage disposal too slow? If this is the case how do you force garbage collection before each resize or make the control wait for collection before moving onto the next image resize?
Is there a more efficient way to do this or increase memory limit?

Any help and suggestions appreciated!
 
Last edited:
I've put largeGfx.Dispose(); after largeImg.dispose(); but its still getting outofmemory errors :(

Edit: its sometimes throwing outofmemory after the first image resize so I'm guessing its just not being allocated enough memory to handle the raw bitmap during the high quality image resize. System memory usage doesnt reach 100% at all so is there someway to see how much memory the application uses and maybe increase the max available to it?

Edit2: after more googling it looks like its a image.fromfile bug not releasing resources..i'll try .fromstream.
 
Last edited:
Worked it out at last!
The code that created the thumbnail for each image only worked for landscape images not portrait :rolleyes:
Nothing to do with memory directly which is what got me so confused :o
 
Good to hear you've got it worked - and a thanks for the details of the problem. :)
 
Back
Top Bottom