Click here to Skip to main content
13,197,211 members (55,803 online)
Rate this:
Please Sign up or sign in to 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.

Bitmap bmp;
using(Bitmap temp = System.Drawing.Image.FromStream(fuInsertImage1.PostedFile.InputStream))
bmp = resize_image(temp);
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);
     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;
    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 13-Feb-13 4:26am
Updated 13-Feb-13 7:09am
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?
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

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

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.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171020.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100