Results 1 to 7 of 7

Thread: Graphics.DrawImage OutofmemoryException

  1. #1

    Thread Starter
    Member kashi_rock's Avatar
    Join Date
    Apr 2006
    Location
    Delhi (India)
    Posts
    55

    Question 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

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

  3. #3

    Thread Starter
    Member kashi_rock's Avatar
    Join Date
    Apr 2006
    Location
    Delhi (India)
    Posts
    55

    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

  4. #4
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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?
    W o t . S i g

  5. #5
    C# Aficionado Lord_Rat's Avatar
    Join Date
    Sep 2001
    Location
    Cave
    Posts
    2,497

    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.
    Need to re-register ASP.NET?
    C:\WINNT\Microsoft.NET\Framework\v#VERSIONNUMBER#\aspnet_regiis -i

    (Edit #VERSIONNUMBER# as needed - do a DIR if you don't know)

  6. #6

    Thread Starter
    Member kashi_rock's Avatar
    Join Date
    Apr 2006
    Location
    Delhi (India)
    Posts
    55

    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

  7. #7
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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.
    W o t . S i g

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width