Click here to Skip to main content
15,881,248 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
I have an asp:FileUpload (fuInsertImage1) that I'm allowing a user to upload an image to be resized and saved to a server. I grab the image using an InputStream and store it in a Bitmap. I then pass the bitmap into a Resize_Image method. This is for a local news company so the images are high quality, usually 15Mb or larger in size. See code below.

C#
Bitmap bmp;
using(Bitmap temp = System.Drawing.Image.FromStream(fuInsertImage1.PostedFile.InputStream))
bmp = resize_image(temp);
bmp.Save(directory);
bmp.Dispose();

protected Bitmap resize_image(Bitmap img)
{
 //determines if image width is larger that 1024 pixels
 //if yes, resizes image to 1024 width and keeps aspect ratio for height.
 int width, height;
 float percentage = 1; //This is to keep the aspect ratio of the image
 width = img.Width;
 height = img.Height;

 if (width > 1024)
  {
   percentage = (float)1024 / width;
   Bitmap bmp = new Bitmap((int)Math.Round(width * percentage, 0), (int)Math.Round(height * percentage, 0));
   Graphics graphic = Graphics.FromImage((System.Drawing.Image)bmp);

    try
    {
     graphic.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
     graphic.DrawImage(img, 0, 0, (int)Math.Round(width * percentage), (int)Math.Round(height * percentage)); //This is where the exception is thrown.  

     return bmp;
    }
     
    catch (Exception ex)
    {
     return null;
    }

    finally
    {
     bmp.Dispose();
     graphic.Dispose();
    }
   }

   else
   {
    return img;
   }
  }


Can anyone help me figure out why this exception is being thrown or point me in another, more efficient, direction. I have 4GB of Ram in my dev environment also, so I don't think it's a physical memory issue. Any help would be greatly appreciated.
Posted
Updated 13-Feb-13 7:09am
v2
Comments
Sergey Alexandrovich Kryukov 13-Feb-13 13:44pm    
Well, we don't know how much memory is already used. Is is a 32-bit system? or 32-bit application? in this case, applications got only 2GB of memory, and we don't know how much is available.
Anyway, in what line the exception is thrown?
—SA
TRK3 13-Feb-13 14:25pm    
Is the "15Mb" image a jpeg file? If so, that's an extremely high quality photo that is absolutely huge. You probably don't have enough memory in a 32-bit system to fully decompress a file like that.

If your dev system is a 32-bit system, see if you can try the same thing on 64-bit system. Then you'll know if it really is an absolute memory issue, or if you are doing something horribly wrong.

If it is an absolute memory issue and a 64-bit machine solves it, then it is probably easier (and cheaper, believe it or not) to just get a 64-bit machine than it is to try to write a more memory efficient algorithm -- assuming the program is for dedicated use by one customer.

1 solution

Without knowing anything about the image in question, like it's dimensions in pixels, it's pretty hard to nail down.

Some operations in GDI will throw an OutOfMemory exception, even thought he problem doesn't have anything to do with running out of RAM. For example, no object in .NET, no matter how much memory is in the machine, can be larger than 2GB is size. An app leaking handles can cause an app to throw an OutOfMemoryException, even though there's tons of memory available. This can be caused when various GDI objects are not released properly when you're done with them, such as allocating a new Brush or Pen or Graphics, or ..., object and not calling Dispose on them when you're done. Do this enough and your app will not only crash itself, but possibly even Windows.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900