为什么这个特定的图片会导致我的应用程序崩溃并显示 OutOfMemoryException?

8

为什么这张图片会导致我的应用程序崩溃并出现OutOfMemoryException?

在此输入图片描述

我创建了一个非常简单的WPF Windows应用程序,只有一个简单的窗口:

<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>
  • 无论是 x86 还是 x64 平台都没有关系。
  • 如果我将图像文件大小调整一个像素,它就可以工作。
  • 如果我增加几个像素并保存图像,它仍然不起作用。
  • 只有在使用 “Fant” 和 “HighQuality” 位图缩放模式时才会出现问题。

这是一个问题的原因在于,我不想每次都需要重新调整从创建这些图像的人那里获得的图片。我正在使用 Paint.Net 来处理这些图像。

谢谢。

我的事件查看器给出了以下信息:

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()

我还记得在早期的.Net中,一些图像API在加载格式不正确的图像时会抛出OOM异常。这并不是致命的进程崩溃OOM,只是一个“嘿,这个图像有一个错误的大小条目”OOM。也许这就是这里发生的事情。只需尝试直接加载图像,看看是否可以正常工作或抛出异常。 - JaredPar
在项目中尝试使用该图像,虽然有些困难但请努力。 - paparazzo
2
当我与上面相同的XAML添加它时,这对我来说并没有爆炸。代码后台中是否有任何内容?您的图形驱动程序是否更新? - Matt
这里也没有问题。 - Mark Feldman
我更新了我的图形驱动程序,但问题仍然存在。我认为这可能与Fant缩放模式有关。 - Jordan
显示剩余3条评论
1个回答

1
据我所知,您的问题似乎对应于WPF中已知但目前未经确认的错误,请参阅Microsoft Connect。有趣的是,该问题已被标记为重复,但我无法找到其他问题。如果它只是内部问题,我也不会感到惊讶...

解决方法

如果您的问题确实与上述错误相关,则有一个简单的解决方法:将基于调色板的PNG8转换为非基于调色板的PNG24。这可以使用Photoshop或GIMP等软件完成。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接