4

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?

Daniel Schierbeck
  • 1,942
  • 2
  • 17
  • 24
  • 1
    I'd suggest the http://imageresizing.net/ library - it handles memory usage properly, and frees you from manually avoiding [the 28+ pitfalls](http://nathanaeljones.com/163/20-image-resizing-pitfalls/) that are the reason MS warns against System.Drawing usage in ASP.NET apps. – Lilith River Nov 11 '11 at 12:51
  • possible duplicate of [Resizing an image in asp.net without losing the image quality](http://stackoverflow.com/questions/2319983/resizing-an-image-in-asp-net-without-losing-the-image-quality) – Simone Carletti Nov 17 '11 at 09:28

5 Answers5

4

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
  • 12,086
  • 1
  • 33
  • 55
  • 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
  • 1
    There are [28 other pitfalls](http://nathanaeljones.com/163/20-image-resizing-pitfalls/) you should check your code for, though - this probably isn't the only one you ran into. – Lilith River Jun 22 '11 at 23:47
3

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.

slugster
  • 49,403
  • 14
  • 95
  • 145
Muhammad Adnan
  • 1,373
  • 1
  • 13
  • 20
1

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
  • 118,037
  • 53
  • 300
  • 385
1

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
  • 5,993
  • 4
  • 30
  • 39
0

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
  • 13,110
  • 20
  • 78
  • 138