【问题标题】:WPF Application will not start due to a System.IO.FileFormatException()由于 System.IO.FileFormatException(),WPF 应用程序将无法启动
【发布时间】:2012-01-06 05:53:01
【问题描述】:

我在 .NET 4.0 中编写了一个 WPF 应用程序。

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

  • Windows XP SP2
  • 远景
  • 窗口 7(32 位)
  • Windows 7(64 位)

在装有 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 加载到目标机器上。

【问题讨论】:

  • 您是否 100% 确定目标计算机具有您所针对的 .NET Framework 的最新服务包?
  • 我这里没有机器,但除非我弄错了,.Net Framework 抛出了异常。错误日志的第二行显示“Framework Version: v4.0.30319”,所以我假设是安装的版本。
  • 如果它与字体问题有关,您可能需要查看此链接:forums.silverlight.net/forums/p/176008/396460.aspx
  • 您可以通过使用 sysinternals procmon 工具监控它正在访问的文件来定位它有问题的图像。

标签: .net wpf


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    我不得不建议 /a 窗口图标文件可能是 .png 格式,将文件转换为 .jpeg 并重试应该是值得的。

    请注意,有趣的是,根据我的经验,在看似有缺陷的机器上使用 Visual Studio 进行调试实际上并没有问题。

    抱歉,我没有关于此事的任何参考资料,但我通过自我调查发现了这一点,因为研究结果并不稀缺。我会试着看看现在是否有任何信息,就像前一段时间一样。

    编辑:

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

    How do I get an Icon from a png image?

    【讨论】:

    • 我按照您的建议从窗口中删除了图标,并且不再出现异常。我会将图像从 .ico 更改为 .jpeg,看看效果如何。感谢您的帮助。
    • 看看你的 *.ico 图像/s 的位数,这可能是问题的一部分以及初始格式(我不是图标文件内部存储方面的专家) -尝试使用不同的颜色范围保存您的图标。
    • 我的图标是 PNG,使用 Icon Sushi 将其转换为 ICO,现在可以正常工作了。
    猜你喜欢
    • 1970-01-01
    • 2011-08-10
    • 1970-01-01
    • 2011-08-22
    • 2022-01-03
    • 2019-12-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多