【发布时间】:2011-11-09 16:51:44
【问题描述】:
我编写了一个应用程序,它需要下载 *.png 文件并将其设置到 WPF 中按钮的背景中。所以,当我运行这个程序时,它面临的错误是
找不到适合完成此操作的成像组件。"
我的代码如下:
第一个应用程序下载带有WebClient 类对象的文件:
System.Net.WebClient wClient = new System.Net.WebClient();
Uri downloadUri = new Uri(MyUri, UriKind.Absolute);
wClient.DownloadFileAsync(downloadUri, "MyImage.png");
wClient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(wClient_DownloadFileCompleted);
当下载完成事件发生时:
ImageBtn.Dispatcher.Invoke(new Action(() =>
{
ImageBrush ib = new ImageBrush();
BitmapImage bi = new BitmapImage();
bi.BeginInit();
bi.UriSource = new Uri("MyImage.png", UriKind.Relative);
bi.EndInit();
ib.ImageSource = bi;
ImageBtn.Background = ib;
}
注意
由于在BackgroundWorker 中运行这些代码块,我使用Dispatcher 设置按钮Background 属性
所以,当我运行程序时,System.NotSupportedException 发生如下:
来自 HRESULT 的异常:0x88982F50 错误代码:-2003292336 消息: 未找到适合完成此操作的成像组件。 来源:PresentationCore 堆栈跟踪:在 System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream 流, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle & safeFilehandle) 在 System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream 流, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, 布尔值 insertInDecoderCache) 在 System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() 在 System.Windows.Media.Imaging.BitmapSource.CompleteDelayedCreation()
在 System.Windows.Media.Imaging.BitmapSource.get_WicSourceHandle()
在 System.Windows.Media.Imaging.BitmapSource.get_DUCECompatiblePtr()
在 System.Windows.Media.Imaging.BitmapSource.UpdateBitmapSourceResource(通道 频道,布尔型 skipOnChannelCheck)在 System.Windows.Media.Imaging.BitmapSource.AddRefOnChannelCore(通道 通道)在 System.Windows.Media.Imaging.BitmapSource.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(通道 通道)在 System.Windows.Media.ImageBrush.AddRefOnChannelCore(频道频道)
在 System.Windows.Media.Brush.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(频道 通道)在 System.Windows.Media.RenderData.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(通道 通道)在 System.Windows.UIElement.RenderContent(RenderContext ctx,布尔 isOnChannel)在 System.Windows.Media.Visual.UpdateContent(RenderContext ctx, VisualProxyFlags 标志,布尔 isOnChannel)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle 句柄)在 System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) 在 System.Windows.Media.Visual.Render(RenderContext ctx,UInt32 childIndex) 在 System.Windows.Media.CompositionTarget.Compile(频道频道)在 System.Windows.Media.CompositionTarget.System.Windows.Media.ICompositionTarget.Render(布尔 inResize,频道频道)在 System.Windows.Media.MediaContext.Render(ICompositionTarget resizedCompositionTarget) 在 System.Windows.Media.MediaContext.RenderMessageHandlerCore(对象 resizedCompositionTarget) 在 System.Windows.Media.MediaContext.RenderMessageHandler(对象 resizedCompositionTarget) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象参数,Int32 numArgs)在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源,委托方法,对象参数,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Threading.ExecutionContext.runTryCode(对象用户数据)在 System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode 代码,CleanupCode backoutCode,对象 userData)在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Windows.Threading.DispatcherOperation.Invoke() 在 System.Windows.Threading.Dispatcher.ProcessQueue() 在 System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 味精,IntPtr wParam,IntPtr lParam,布尔值&处理)在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam, IntPtr lParam,布尔值&处理)在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托 回调,对象参数,Int32 numArgs)在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象 源,委托方法,对象参数,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority 优先级、TimeSpan 超时、委托方法、对象参数、Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 消息,IntPtr wParam,IntPtr lParam)在 MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) 在 System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame 框架)在 System.Windows.Application.RunDispatcher(对象忽略)
在 System.Windows.Application.RunInternal(窗口窗口) WPF_KSMMessenger.App.Main() 在 C:\Users\Hossein\Desktop\WPF_KSMMessenger\WPF_KSMMessenger\obj\x86\Debug\App.g.cs:line 0 在 System.AppDomain._nExecuteAssembly(RuntimeAssembly 程序集, 字符串 [] 参数)在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态、布尔值 ignoreSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext、ContextCallback 回调、对象状态)在 System.Threading.ThreadHelper.ThreadStart()
我应该怎么做才能解决这个问题?
感谢您的关注。
【问题讨论】:
-
您检查了文件本身吗?对吗?
-
什么是操作系统? XP 上 WPF 中的 PNG 可能会导致问题。
-
@失望先生:我用的是Win7,不是XP!
-
@Thomas : 是的,文件是正确的,我可以通过 Windows Image Viewer 查看它!
-
复制它的另一种方法是使用损坏的图像。例如,将文本文件另存为图像将产生相同的结果。
标签: c# .net wpf exception-handling