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)
{
int width, height;
float percentage = 1;
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));
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.