WPF应用程序由于System.IO.FileFormatException()无法启动

9

我已经使用.NET 4.0编写了一个WPF应用程序。

我已经在以下操作系统上安装并成功运行了该应用程序:

  • Windows XP SP2
  • Vista
  • Window 7 (32 bit)
  • Windows 7 (64 bit)

在一台安装了Windows XP SP3的机器上安装该应用程序后,该应用程序无法启动。我检查了事件查看器的应用程序日志,并发现了以下错误:

Application: ApplicationName.exe
Framework Version: v4.0.30319
Description: The process was terminated due to an unhandled exception.
Exception Info: System.IO.FileFormatException
Stack:
   at System.Windows.Media.Imaging.BitmapFrameDecode.EnsureThumbnail()
   at System.Windows.Media.Imaging.BitmapFrameDecode.get_Thumbnail()
   at MS.Internal.AppModel.IconHelper.GetBestMatch(System.Collections.ObjectModel.ReadOnlyCollection`1<System.Windows.Media.Imaging.BitmapFrame>, System.Windows.Size)
   at MS.Internal.AppModel.IconHelper.CreateIconHandleFromImageSource(System.Windows.Media.ImageSource, System.Windows.Size)
   at MS.Internal.AppModel.IconHelper.GetIconHandlesFromImageSource(System.Windows.Media.ImageSource, IconHandle ByRef, IconHandle ByRef)
   at System.Windows.Window.UpdateIcon()
   at System.Windows.Window.SetupInitialState(Double, Double, Double, Double)
   at System.Windows.Window.CreateSourceWindow(Boolean)
   at System.Windows.Window.CreateSourceWindowDuringShow()
   at System.Windows.Window.SafeCreateWindowDuringShow()
   at System.Windows.Window.ShowHelper(System.Object)
   at System.Windows.Window.Show()
   at ApplicationName.App.LoadMainWindow()
   at ApplicationName.App.OnStartup(System.Windows.StartupEventArgs)
   at System.Windows.Application.<.ctor>b__1(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.DispatcherOperation.InvokeImpl()
   at System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(System.Object)
   at System.Threading.ExecutionContext.runTryCode(System.Object)
   at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode, CleanupCode, System.Object)
   at System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object, Boolean)
   at System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext, System.Threading.ContextCallback, System.Object)
   at System.Windows.Threading.DispatcherOperation.Invoke()
   at System.Windows.Threading.Dispatcher.ProcessQueue()
   at System.Windows.Threading.Dispatcher.WndProcHook(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.InvokeImpl(System.Windows.Threading.DispatcherPriority, System.TimeSpan, System.Delegate, System.Object, Int32)
   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 ApplicationName.App.Main()

我猜测这与我窗口中的一张图片有关,但不确定;)

有人之前见过这个异常并有解决方案吗?

注意:如果相关的话,我的安装程序会在必要时将WIC和完整的.NET Framework 4.0加载到目标计算机上。


你是否百分之百确定目标机器已经安装了你所针对的.NET Framework的最新服务包? - abramlimpin
我这里没有机器,但除非我错了,.Net Framework抛出了异常。错误日志的第二行显示“框架版本:v4.0.30319”,因此我认为安装的是该版本。 - Ryan Spears
如果与字体问题有关,您可能需要检查此链接:http://forums.silverlight.net/forums/p/176008/396460.aspx - abramlimpin
你可以使用Sysinternals Procmon工具来监视它正在访问哪些文件,从而可能找到它遇到问题的图像。 - Sam Holder
2个回答

8

感谢您提供的有关同一问题的提示。我发现在Shell.xaml窗口属性中,Icon属性设置为ICO。我删除了它,在VS中运行时会得到没有图标的窗口。然而,我去了项目属性并在那里选择了图标(VS在我的资源文件夹中找到了图标-很酷!)。当应用程序构建包含ICO的EXE时,令人惊喜的是,窗口现在具有相同的图标。


3
我建议可能窗口图标文件是以 .png 格式存储的,将文件转换为 .jpeg 格式并重试应该值得一试。
有趣的是,在看似有问题的机器上使用 Visual Studio 进行调试实际上没有问题,在我的经验中。
很抱歉我没有关于这个问题的参考资料,但是自己调查后发现研究结果非常稀少。我将尝试查找是否现在有任何信息,因为这是一段时间之前的事情。
编辑:

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/9e723d51-0e26-4609-8fe1-5e0edcf7385b

如何从png图像获取图标?


我按照你的建议从窗口中删除了图标,不再出现异常。我将把图像从.ico更改为.jpeg,看看效果如何。感谢你的帮助。 - Ryan Spears
看一下您的*.ico图像的位深度,它可能是问题的一部分,以及初始格式(我不是图标文件内部存储方面的专家)- 尝试使用不同的色彩范围保存您的图标。 - Grant Thomas
http://connect.microsoft.com/VisualStudio/feedback/details/321285/wpf-application-fails-if-window-icon-is-a-vista-ico-file - JulianM
我的图标原本是PNG格式,使用Icon Sushi转换成ICO格式后,现在可以正常使用了。 - Chris Fulstow

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