Resolve 'Out of memory' exception when calling DrawImage

11,199

I'm sure you've figured this out by now, but in case you haven't we hit the same issue. It's a bug in GDI+ and this works as a workaround. Unfortunately I don't know why but with these sorts of issues I tend to care more that it's fixed and less why it's fixed. ;)

image = image.GetThumbnailImage(image.Width, image.Height, null, IntPtr.Zero);

Apparently that call adjusts the PixelFormat and puts the image in a format that the imagelist's usage of DrawImage can handle.

From: https://siderite.dev/blog/outofmemoryexception-in.html

Share:
11,199
Jack Juiceson
Author by

Jack Juiceson

Updated on June 14, 2022

Comments

  • Jack Juiceson
    Jack Juiceson almost 2 years

    About one percent of our users experience sudden crash while using our application. The logs show below exception, the only thing in common that I've seen so far is that, they all have XP SP3.

    Thanks in advance

    Out of memory.
    
       at System.Drawing.Graphics.CheckErrorStatus(Int32 status)
       at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs, DrawImageAbort callback, IntPtr callbackData)
       at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr, DrawImageAbort callback)
       at System.Drawing.Graphics.DrawImage(Image image, Rectangle destRect, Int32 srcX, Int32 srcY, Int32 srcWidth, Int32 srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)
       at System.Windows.Forms.ControlPaint.DrawBackgroundImage(Graphics g, Image backgroundImage, Color backColor, ImageLayout backgroundImageLayout, Rectangle bounds, Rectangle clipRect, Point scrollOffset, RightToLeft rightToLeft)
       at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
       at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
       at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
       at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
       at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion)
       at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
       at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
       at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
       at System.Windows.Forms.ScrollableControl.OnPaintBackground(PaintEventArgs e)
       at System.Windows.Forms.Control.PaintTransparentBackground(PaintEventArgs e, Rectangle rectangle, Region transparentRegion)
       at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle, Color backColor, Point scrollOffset)
       at System.Windows.Forms.Control.PaintBackground(PaintEventArgs e, Rectangle rectangle)
       at System.Windows.Forms.Control.OnPaintBackground(PaintEventArgs pevent)
       at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
       at System.Windows.Forms.Control.WmPaint(Message& m)
       at System.Windows.Forms.Control.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
    
    Operation System Information
    ----------------------------
    Name = Windows XP
    Edition = Home
    Service Pack = Service Pack 3
    Version = 5.1.2600.196608
    Bits = 32
    
  • Jack Juiceson
    Jack Juiceson almost 13 years
    actually I was unable to solve it, as it was happening once a month.. But I will definitely try this out, and mark it as solution if it won't happen again. Thank you
  • Lilith River
    Lilith River over 11 years
    GetThumbnailImage uses the embedded exif data (or if it's missing bilinear scaling) Pretty low image quality. We need a better solution.
  • nevelis
    nevelis almost 10 years
    Unfortunately this didn't work in my case - GetThumbnailImage() now throws the OutOfMemoryException. I am using DrawImage() with the ImageAttributes class variant.