Graphics.DrawImage OutofmemoryException
Dear All,
I am trying to resize a large JPEG file (10550 * 8500 pixels and 31MB size on disk) but everytime I am getting Outofmemory exception error. Following is my code:
Code:
public Bitmap userResizeImage(string strPath, Image imImage)
{
int intHeight = 0;
int intWidth = 0;
Bitmap bmpBitmap = default(Bitmap);
Graphics gGraphics = default(Graphics);
try
{
if (imImage.Height < 7000 & imImage.Width < 7000)
{
bmpBitmap = new Bitmap(strPath);
}
else
{
if (imImage.Height > 7000 & imImage.Width > 7000)
{
intHeight = 7000;
decimal dblRatio = 7000 / Convert.ToDecimal(imImage.Height);
intWidth = Convert.ToInt16(dblRatio * imImage.Width);
if (intWidth > 7000)
{
dblRatio = 7000 / Convert.ToDecimal(intWidth);
intHeight = Convert.ToInt16(dblRatio * intHeight);
intWidth = 7000;
}
}
else if (imImage.Height > 7000 & imImage.Width < 7000)
{
decimal dblRatio = 7000 / Convert.ToDecimal(imImage.Height);
intHeight = 7000;
intWidth = Convert.ToInt16(dblRatio * imImage.Width);
}
else if (imImage.Height < 7000 & imImage.Width > 7000)
{
decimal dblRatio = 7000 / Convert.ToDecimal(imImage.Width);
intWidth = 7000;
intHeight = Convert.ToInt16(dblRatio * imImage.Height);
}
gGraphics = Graphics.FromHwnd(IntPtr.Zero);
bmpBitmap = new Bitmap(intWidth, intHeight, gGraphics);
gGraphics.Dispose();
bmpBitmap.SetResolution(imImage.HorizontalResolution, imImage.VerticalResolution);
using (gGraphics = Graphics.FromImage((Image)bmpBitmap))
{
gGraphics.Clear(Color.White);
gGraphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
gGraphics.CompositingQuality = CompositingQuality.HighQuality;
gGraphics.SmoothingMode = SmoothingMode.HighQuality;
gGraphics.DrawImage(imImage, new Rectangle(0, 0, intWidth, intHeight), 0, 0, imImage.Width, imImage.Height, GraphicsUnit.Pixel);
}
return bmpBitmap;
}
return bmpBitmap;
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
return null;
}
finally
{
gGraphics.Dispose();
}
}
Could anyone please help me out to resolve the issue?
Thanks in advance.
Kaushal Sharma
Re: Graphics.DrawImage OutofmemoryException
Although as a jpeg your image is 31MB, in memory it will be in bitmap form and assuming 24bit colour will require ~256MB to store (10550 * 8500 * 24) / (8 * 1024 * 1024). This could well be too much if your system is a little short on RAM. An image of that size cannot be displayed all at once, perhaps you could tell us a bit more about what you are doing.
Re: Graphics.DrawImage OutofmemoryException
Milk,
Thanks for your reply, Its true that being the JPEG, size of images is too large. These are some inspection files which has been merged by the client using photoshop. I am working on an application which reads these images and show in a image control (with zoom and pan functionalities) but before assigning those images to the control, I am getting error on resizing.. I have spent almost a day in finding the solution but helpless.
Any ideas...
Regards,
Kaushal
Re: Graphics.DrawImage OutofmemoryException
Not sure but it seems GDI+ can throw an out of memory exception for a number of reasons, it might not be just the shear size.
Have you tested the code with a smaller image?
Have you tested the code with a different image of the same size (source might be corrupt in some way)?
Are you disposing of all created GDI objects when finished with?
Re: Graphics.DrawImage OutofmemoryException
Carve the image in to chunks, load the chunks one at a time in to a separate object and resize the chunks. Then, stitch them back together.
Re: Graphics.DrawImage OutofmemoryException
Thanks all for your replies.
I have tested it with smaller images and works fine for example 12mb jpeg (6500 * 5500). I am disposing all gdi objects
Re: Graphics.DrawImage OutofmemoryException
Well unless anyone else has any bright ideas I would be thinking about implementing Lord_Rat's solution. Although it would seem slightly ironic if you end up effectively splitting the image up into what the client merged originally.