Take the 2-minute tour ×

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?

share|improve this question
I'd suggest the imageresizing.net library - it handles memory usage properly, and frees you from manually avoiding the 28+ pitfalls that are the reason MS warns against System.Drawing usage in ASP.NET apps. –  Nathanael Jones Nov 11 '11 at 12:51

6 Answers 6

up vote 3 down vote accepted

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 =
                graphics.InterpolationMode =
                graphics.CompositingQuality =

                    new Rectangle(0, 0, newBitmap.Width, newBitmap.Height));

            return newBitmap;
share|improve this answer
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 Mar 19 '09 at 13:04
+1 for disposing. I see our customers forgetting/not knowing to do this all the time. –  Kev Mar 19 '09 at 13:05
Thank you, this apparently solved our problem! –  Daniel Schierbeck Mar 19 '09 at 13:40
There are 28 other pitfalls you should check your code for, though - this probably isn't the only one you ran into. –  Nathanael Jones Jun 22 '11 at 23:47

I found imageresizer and its great. and good API. Works Great. Downloaded from Visual studio 2010 Extension Manager: http://nuget.org/.

Easy Steps to download API in VS-2010:

1). Install Extension http://nuget.org/.

enter image description here

3). Find and Install ImageResizing
enter image description here

4).Then Code: (I m using here cropping. you can use any) Documentation on imageresizing.net

string uploadFolder = Server.MapPath(Request.ApplicationPath + "images/");
FileUpload1.SaveAs(uploadFolder + FileUpload1.FileName);

//The resizing settings can specify any of 30 commands.. See http://imageresizing.net for details.
ResizeSettings resizeCropSettings = new ResizeSettings("width=200&height=200&format=jpg&crop=auto");

//Generate a filename (GUIDs are safest).
string fileName = Path.Combine(uploadFolder, System.Guid.NewGuid().ToString());

//Let the image builder add the correct extension based on the output file type (which may differ).
fileName = ImageBuilder.Current.Build(uploadFolder + FileUpload1.FileName, fileName, resizeCropSettings, false, true);

Try!!! its very awsumm and easy to use. thanks.

share|improve this answer

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.


share|improve this answer

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

share|improve this answer

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


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


share|improve this answer

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.

share|improve this answer

Your Answer


