Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#2.0 C#3.0 C# C#4.0
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);
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 13-Feb-13 5:26am
Jesse88135
Edited 13-Feb-13 8:09am
v2
Comments
Sergey Alexandrovich Kryukov at 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 at 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
good
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.
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 329
2 CPallini 270
3 Afzaal Ahmad Zeeshan 204
4 DamithSL 194
0 OriginalGriff 5,515
1 DamithSL 4,451
2 Maciej Los 3,902
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 13 Feb 2013
Copyright © CodeProject, 1999-2014
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