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:
Could anyone please help me out to resolve the issue?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(); } }
Thanks in advance.
Kaushal Sharma




Reply With Quote
