tags:

views:

892

answers:

5

We're having problems with an ASP.NET application which allows users to upload, and crop images. The images are all scaled to fixed sizes afterwards. We basically run out of memory when a large file is processed; it seems that the handling of JPEG is rather inefficient -- we're using System.Drawing.BitMap. Do you have any general advice, and perhaps some pointers to a more efficient image handling library? What experiences do you have?

A: 

Do you perhaps have a stack trace to look at?

I have also done some image editing after a user has uploaded an image. The only problems that I ran into were restrictions on file upload size on the browsers and timeouts. But nothing related to .Net's libraries.

Something else to consider. If you are doing multiple images or have some dangerous looping somewhere then are you making sure to flush() and dispose() things.

uriDium
+3  A: 

I had the same problem, the solution was to use System.Drawing.Graphics to do the transformations and dispose every bitmap object as soon as I was finished with it. Here's a sample from my library (resizing) :

    public Bitmap ApplyTo(Bitmap bitmap)
    {
        using (bitmap)
        {
            Bitmap newBitmap = new Bitmap(bitmap, CalculateNewSize(bitmap));

            using (Graphics graphics = Graphics.FromImage(newBitmap))
            {
                graphics.SmoothingMode =
                    SmoothingMode.None;
                graphics.InterpolationMode =
                    InterpolationMode.HighQualityBicubic;
                graphics.CompositingQuality =
                    CompositingQuality.HighQuality;

                graphics.DrawImage(
                    bitmap,
                    new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));
            }

            return newBitmap;
        }
    }
Diadistis
I do image cropping and scaling similar to this. The key point is to place your code in 'using' statements, which then auto-disposes of the resources once you leave the score of the using block. Otherwise, u'll have the image data not getting disposed == wasting memory.
Pure.Krome
+1 for disposing. I see our customers forgetting/not knowing to do this all the time.
Kev
Thank you, this apparently solved our problem!
Daniel Schierbeck
+1  A: 

A couple of thoughts spring to mind -

  1. What size of images do you allow your users to upload and can you impose restrictions on this?

  2. When you're using the System.Drawing.Bitmap class, are you remembering to dispose of it correctly? We found one of the primary causes of System.OutOfMemoryException exceptions on our shared hosting platform was users not disposing of Bitmap objects correctly.

Kev

Kev
+1  A: 

There was an older bug with .net that all images would default to 32 bits per pixel - at this size you can exhaust your memory pretty fast. Please use PixelFormat structure to make sure this is not the case for your problem.

This link might help: http://msdn.microsoft.com/en-us/library/aa479306.aspx

Sesh
+1  A: 

There is an excellent article on this on CodeProject. Really really nice one. Will try find the link (used it about 4 years back).

UPDATE:

Not the one I was looking for, but looks good too.

http://www.codeproject.com/KB/custom-controls/ImageCroppingControl.aspx

leppie