8

Why is this particular image causing my application to crash with an OutOfMemoryException?

enter image description here

I created a really simple WPF Windows Application with a simple window:

<Window x:Class="DeleteMe.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <Image Source="C:\Path\To\The\Image\MyImage.png" RenderOptions.BitmapScalingMode="Fant"></Image>
    </Grid>
</Window>
  • It doesn't matter if it is x86 or x64.
  • If I resize the image file even by 1 pixel it works.
  • If I add a few pixels to the image and save it, it still doesn't work.
  • The problem occurs only with Fant and HighQuality bitmap scaling modes.

The reason this is a problem is that I don't want to have to resize every image we get from the person that is creating these images. I am using Paint.Net to work with the images as is said person.

Thanks.

My event viewer gives me the following:

Application: DeleteMe.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.OutOfMemoryException
Stack:
   at System.Windows.Media.Composition.DUCE+Channel.SyncFlush()
   at System.Windows.Interop.HwndTarget.UpdateWindowSettings(Boolean, System.Nullable`1
<ChannelSet>)
   at System.Windows.Interop.HwndTarget.UpdateWindowPos(IntPtr)
   at System.Windows.Interop.HwndTarget.HandleMessage(MS.Internal.Interop.WindowMessage, IntPtr, IntPtr)
   at System.Windows.Interop.HwndSource.HwndTargetFilterMessage(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndWrapper.WndProc(IntPtr, Int32, IntPtr, IntPtr, Boolean ByRef)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(System.Object)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(System.Delegate, System.Object, Int32)
   at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(System.Object, System.Delegate, System.Object, Int32, System.Delegate)
   at System.Windows.Threading.Dispatcher.LegacyInvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.HwndSubclass.DefWndProcWrapper(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr, Int32, IntPtr, IntPtr)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(System.Windows.Interop.MSG ByRef)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.PushFrame(System.Windows.Threading.DispatcherFrame)
   at System.Windows.Threading.Dispatcher.Run()
   at System.Windows.Application.RunDispatcher(System.Object)
   at System.Windows.Application.RunInternal(System.Windows.Window)
   at System.Windows.Application.Run(System.Windows.Window)
   at System.Windows.Application.Run()
   at DeleteMe.App.Main()
Jordan
  • 9,642
  • 10
  • 71
  • 141
  • I remember way back in early .Net some of the image APIs would throw OOM when loading badly formatted images. It wasn't a fatal process crash OOM, just a "hey this image has a bad size entry" OOM. Perhaps that is what is happening here. Just try loading the image directly and see if that works or throws – JaredPar Jan 30 '14 at 21:03
  • A reach but try the image in the project – paparazzo Jan 30 '14 at 21:46
  • 2
    This isn't blowing up for me when I add it with the same XAML as above. Is there anything at all in the code-behind? Are your graphics drivers up-to-date? – Matt Jan 30 '14 at 21:48
  • No problem here either. – Mark Feldman Jan 30 '14 at 22:14
  • It doesn't work on my coworker's computer either. He is running Windows 7. – Jordan Jan 31 '14 at 13:46
  • Updated my graphics drivers, it still happens. I think it has something to do with the Fant scaling mode. – Jordan Jan 31 '14 at 14:23
  • I've tried it on two other computers that are running Windows 8.1 and both of them have the problem. It seems that the problem is with 8.1. – Jordan Jan 31 '14 at 14:30
  • I can confirm that the problem exists on Windows 8.1 only. We were not able to reproduce it on other OS. – theDmi Jun 04 '14 at 08:51

1 Answers1

1

As far as I can tell your problem corresponds to a known but currently unconfirmed bug in WPF, see Microsoft Connect. Funnily, the issue has been marked as duplicate, I'm however unable to find the other issue. I wouldn't be surprised if it was internal only...

Workaround

If your problem really is connected to the bug above, then there is an easy workaround: Just convert your palette-based PNG8 to a non-palette-based PNG24. This can be done e.g. using Photoshop or GIMP.

theDmi
  • 17,546
  • 6
  • 71
  • 138