我在.NET 4.0中编写了一个WPF应用程序.
我已在以下操作系统上安装并成功运行该应用程序:
在使用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, …Run Code Online (Sandbox Code Playgroud) 我使用Win32 SHGetFileInfo来获取属于某个文件的图标的句柄.有很多描述如何执行此操作,也可以在stackoverflow上执行此操作,例如:获取shell使用的图标
调用该函数后,您将拥有一个带有Icon图标句柄的结构.使用静态方法Icon.FromHandle我可以将它转换为System.Drawing.Icon类的对象.该类实现System.IDisposable.正确的用法如下:
using (Icon icon = Icon.FromHandle(shFileInfo.hIcon))
{
// do what you need to do with the icon
}
Run Code Online (Sandbox Code Playgroud)
在离开using语句时,处理图标对象.
MSDN在Icon.FromHandle的描述中警告(点击查看):
使用此方法时,必须使用Win32 API中的DestroyIcon方法处置原始图标,以确保释放资源.
释放此Icon使用的所有资源.
题:
Dispose()对象是否足够,或者我应该调用Dispose()和DestroyIcon,还是调用DestroyIcon而不是Disposing对象?