0

In our application we have a problem with an ExternalException in GDI+. The application is distributed and runs on over 100 computers successfully and we cannot reproduce it in our test environment. However one single computer crashes with the mentioned exception.

The Bitmap was previously loaded via a resx file and the BitmapImage is used in a WPF part of the application just for displaying.

Here's the corresponding code:

private static BitmapImage ConvertToImage(Bitmap bitmap)
      {
         var bitmapImage = new BitmapImage();
         using (var memory = new MemoryStream())
         {
            bitmap.Save(memory, ImageFormat.Png);
            memory.Position = 0;

            bitmapImage.BeginInit();
            bitmapImage.StreamSource = memory;
            bitmapImage.CacheOption = BitmapCacheOption.OnLoad;
            bitmapImage.EndInit();
         }

         return bitmapImage;
      } 

The application crashes when calling

bitmap.Save

The computer has Windows 7 x64 installed with .NET 4.0.

Any hints on what this could be?

EDIT 1:

I didn't find out so far what's causing the exception. We found out that the exception only happens with some specific images. But all images are in the PNG format.

For now we will stick to the solution of using CreateBitmapSourceFromHBitmap which is working for us. CreateBitmapSourceFromHBitmap - Stackoverflow post

Community
  • 1
  • 1
faceman
  • 1,318
  • 11
  • 20
  • Just out of curiosity, why are you combining GDI+ with WPF? – Mike Dinescu Nov 12 '13 at 16:17
  • The main part of the application is Winforms. There's a small part which is WPF and uses the same Resources. – faceman Nov 12 '13 at 16:24
  • 1
    My only guess could be that the bitmap is too large and/or the available memory is too small. – BeemerGuy Nov 12 '13 at 16:29
  • 1
    The thing is that I've run into "generic" errors from GDI+ before, and almost each time it's for a different reason; so, even if you can recreate the error, you may not have tapped into the source of the error on that one machine. – BeemerGuy Nov 12 '13 at 16:31
  • The image size is around 32*32 pixel. -> Very small. Do you know any methods to find out more about the error? – faceman Nov 13 '13 at 07:16
  • Just for anyone seeing this later.. if it crashes on `bitmap.Save` then the image object is _already corrupted_ at that moment. It is completely unrelated to the shown code. – Nyerguds Feb 14 '18 at 20:46

0 Answers0