SiteExperts.com Logo Home | Community | Developer's Paradise
User Groups | Site Tools | Site Information | Search
 Main Menu
 Forums
SiteExperts.com Forums
All Discussions

SiteExperts Feedback
The Lounge
Dynamic HTML
Site Design/ Critiques
HTML and CSS
XML Technologies
The Wireless Internet
Internet Explorer
Microsoft .NET
The Server
Technical Support

Sponsored Links

User Groups : Forums : SiteExperts : Microsoft .NET :

Previous DiscussionNext Discussion
 Possible memory management problem

Ok, here's the scoop:

I'm doing some work for a local High School (American) Football team.

Anyway, I wrote an app that resizes images to 15% of their actual size.

Sometimes, the CD I receive for my part will have well over 200 photos on it (the most recent had 291).

The problem is, the app seems to suck up all of my memory, takes quite a while, but only does a little over 80 photos the first time. I think it's a memory problem.

Here is the relevant code to the resizing:

private bool resizeImages()
{
double percentResize = 0.0;
System.Drawing.Image.GetThumbnailImageAbort dummyCallBack;
System.Drawing.Image thumbnailImage;
System.Drawing.Image fullSizeImage;

double width = 0.0;
double height = 0.0;
int resizedWidth = 0;
int resizedHeight = 0;
string thePath = "";
string resizedPath = "";

thePath = Path.GetFullPath(txtImagePath.Text);

resizedPath = Path.GetFullPath(txtResizedPath.Text);

DirectoryInfo di = new DirectoryInfo(thePath);
FileInfo[] theFiles = di.GetFiles("*.jpg");

foreach (FileInfo fi in theFiles)
{
try
{
percentResize = 0.15;

fullSizeImage = Image.FromFile(thePath + "\\" + fi.Name.ToString());

width = (double)fullSizeImage.Width * (double)percentResize;
height = (double)fullSizeImage.Height * (double)percentResize;

resizedWidth = (int)width;
resizedHeight = (int)height;

dummyCallBack = new Image.GetThumbnailImageAbort(thumbnailCallback);

thumbnailImage = fullSizeImage.GetThumbnailImage(resizedWidth, resizedHeight, thumbnailCallback, IntPtr.Zero);

//thumbnailImage.RotateFlip(RotateFlipType.Rotate180FlipNone);
//thumbnailImage.RotateFlip(RotateFlipType.Rotate180FlipNone);

thumbnailImage.Save(resizedPath + "\\" + fi.Name.Replace(fi.Extension, "").Replace(".", "") + "_res.jpg", ImageFormat.Jpeg);
GC.Collect();
}
catch (Exception ex)
{
lblError.Text = ex.Message.ToString();
return false;
}

fullSizeImage = null;
thumbnailImage = null;

GC.Collect();
GC.Collect(0, GCCollectionMode.Forced);
}
return true;
}

It seems like it might be a memory problem, but I don't know.

Any thoughts on this?

My system is WinXP with 1GB of RAM and a 3 GHz processor (I believe).

Started By Monte on Nov 12, 2008 at 8:15:23 AM

8 Response(s) | Reply

Earlier Replies | Replies 5 to 8 of 8 | Later Replies
Goto Page: 2 1
brian on Nov 12, 2008 at 9:15:55 AM (# 5)

You have to dispose of image objects...gc.collect does not do that. The thumbnailImage and also the fullSizeImage  needs to be disposed at the end of each loop iteration otherwise the image data is orphaned.

I do have to say however that the thumbnail is not the best way to resize. The best way is actually to use a drawImage with the interpolation mode set to HighQualityBicubic - you get brilliant quality out if this. it also will then work with 256-color images which the thumbnail doesn't seem to work right with.

so instead, you have

  • new Bitmap with the size of the output
  • new Graphics, linked to the bitmap
  • the image that contains the file
  • then just copy that image into the new image
  • then dispose of the image and the graphics object
  • save your sized image
  • dispose of that too

it works brilliantly. I use it for reducing images within web-based apps.


Monte on Nov 12, 2008 at 2:43:50 PM (# 6)
This message has been edited.

Thanks a TON! I'll give this a shot and let you know the results. I think the drawImage will be a future iteration. For now, I am going to try disposing the image objects first.


Monte on Nov 13, 2008 at 12:20:47 PM (# 7)

Disposing of the image objects did speed it up.

I also made it a multi-threaded app, so that it didn't freeze up the UI.

Now on to the improvements...


Monte on Nov 17, 2008 at 8:36:28 AM (# 8)

One other note:

I learned a few things from this project.

I had to create a delegate to call the thread from, otherwise, my HTML template was being generated twice.

I also took out a couple of the thread delays that I had put in.


Earlier Replies | Replies 5 to 8 of 8 | Later Replies
Goto Page: 2 1

To respond to a discussion, you must first logon.

If you are not registered, please register yourself to become a member of the SiteExperts.community.

User Name
Password
Copyright 1997-2004 InsideDHTML.com, LLC. All rights reserved.