5

I am writing a code to resize JPG images in C#. My code takes around 6 seconds to resize 20 JPG images. I am wondering if there is any faster way of doing this in C#? Any suggestion to improve this is appreciated!

Here is my code now:

Bitmap bmpOrig, bmpDest, bmpOrigCopy;
foreach (string strJPGImagePath in strarrFileList)
{
bmpOrig = new Bitmap(strJPGImagePath);
bmpOrigCopy = new Bitmap(bmpOrig);
bmpOrig.Dispose();
File.Delete(strJPGImagePath);

bmpDest = new Bitmap(bmpOrigCopy, new Size(100, 200));
bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);

bmpOrigCopy.Dispose();
bmpDest.Dispose();
}

Thanks to @Guffa for his solution. I moved the dispose() out of foreach loop. The updated and fast code is:

        Bitmap bmpDest = new Bitmap(1, 1);
        foreach (string strJPGImagePath in strarrFileList)
        {
            using (Bitmap bmpOrig = new Bitmap(strJPGImagePath))
            { 
                bmpDest = new Bitmap(bmpOrig, new Size(100, 200)); 
            }
            bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
        }
        bmpDest.Dispose();
4
  • 1
    First: I think the working codes should be posted in the code review forum; here codereview.stackexchange.com . second: why are you disposing and cleaning the arrays inside the foreah loop? May be you should reuse the exisiting object and clean everything after the loop finish Commented Jul 15, 2014 at 0:54
  • Sure - I did that, but it showed me out of memory exception! Commented Jul 15, 2014 at 1:16
  • how about @Guffa solution? Commented Jul 15, 2014 at 1:17
  • @stackunderflow: It doesn't work to reuse the bitmaps when they are created that way. You would need to draw the loaded bitmap onto an existing bitmap if you want to reuse it, and all the loaded bitmaps still have to be disposed. Commented Jul 15, 2014 at 11:51

1 Answer 1

6

Instead of copying the bitmaps in two steps, make it one step. That way you reduce the memory usage quite a bit as you don't have two copies of the oringal image in memory at once.

foreach (string strJPGImagePath in strarrFileList) {
  Bitmap bmpDest;
  using(Bitmap bmpOrig = new Bitmap(strJPGImagePath)) {
    bmpDest = new Bitmap(bmpOrig, new Size(100, 200));
  }
  bmpDest.Save(strJPGImagePath, jgpEncoder, myEncoderParameters);
  bmpDest.Dispose();
}
Sign up to request clarification or add additional context in comments.

6 Comments

WOW, this is great making the code more than two times faster (2.4 sec)! I did a small change and it is around 100 msec even faster (2.3 sec). I edited my main post to include the edited and updated code. Thank you again! :)
@M0HS3N: You should not move the dispose out of the loop, that will leave all bitmaps that you create undisposed, except the last one.
Pardon me @Guffa, one point i didnt understand, if bmpDist is going to be used inside a loop , why should we dispose it and recreate it each time. We can reuse it reassign it and then dispose it after the loop, is it related to value type andref ttype issue?
@stackunderflow: You could reuse it, but that's not what you are doing. When you do bmpDest = new Bitmap(...) you don't put the image in an existing object, you create a new object and the reference to that object replaces any existing reference in the variable. If you wanted to reuse the bitmap, then you would need to create one before the loop, and use a Graphics object to draw on that bitmap in the loop.
@stackunderflow: Yes, that is bad practice. Objects that are disposed is very easy to take care of for the GC, but if you don't dispose the object it is a resource expensive process. It has to be finalized before it can be collected, surviving one or more collections, which usually means that it is moved to the next heap generation, which will move the entire object from one memory area to another.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.