“没有找到适合完成此操作的成像组件。”

我编写了一个应用程序,它需要下载* .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的exception:0x88982F50错误代码:-2003292336消息:未找到适合完成此操作的映像组件。 来源:PresentationCore Stack Trace:在System.Windows.Media的System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri,Stream stream,BitmapCacheOption cacheOption,Guid&clsId,Boolean&isOriginalWritable,Stream&uriStream,UnmanagedMemoryStream&unmanagedMemoryStream,SafeFileHandle&safeFilehandle)。在System.Windows.Media.Imaging的System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()处的Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri,Uri uri,Stream stream,BitmapCreateOptions createOptions,BitmapCacheOption cacheOption,RequestCachePolicy uriCachePolicy,Boolean insertInDecoderCache)。 BitmapSource.CompleteDelayedCreation()
在System.Windows.Media.Imaging.BitmapSource.get_WicSourceHandle()
在System.Windows.Media.Imaging.BitmapSource.get_DUCECompatiblePtr()
位于System.Windows.Media.Imaging.BitmapSource.System.Windows的System.Windows.Media.Imaging.BitmapSource.AddRefOnChannelCore(Channel channel)的System.Windows.Media.Imaging.BitmapSource.UpdateBitmapSourceResource(Channel channel,Boolean skipOnChannelCheck)。 System.Windows.Media.ImageBrush.AddRefOnChannelCore(频道频道)中的Media.Composition.DUCE.IResource.AddRefOnChannel(频道频道)
处于System.Windows.Media.RenderData.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(频道)的System.Windows.Media.Brush.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(频道频道)在System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateContent(RenderContext ctx,VisualProxyFlags flags,Boolean isOnChannel)上的System.Windows.UIElement.RenderContent(RenderContext ctx,Boolean isOnChannel)处的频道) )在System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)处于System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)at at在System.Windows.Media的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)中的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) .Visual.UpdateChildren(RenderCo System.Windows.Media.Visual.RenderRecursive中的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的ntext ctx,ResourceHandle句柄(RenderContext ctx) )在System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)处于System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)at at在System.Windows.Media的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上System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄) .RenderRecur 在System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)上的Sive(RenderContext ctx),System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)处的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)处的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)处的ResourceHandle句柄) System.Windows上System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)位于System.Windows.Media.Visual的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)中的.Media.Visual.RenderRecursive(RenderContext ctx)。 UpdateChildren(渲染 在System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)上的上下文ctx,ResourceHandle句柄) )在System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)处于System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)at at在System.Windows.Media的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上System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄) .RenderRec 位于System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)中的ursive(RenderContext ctx),位于System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, System.Windows.Media.Visual.Render上的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)处的SystemHindle句柄处于System.Windows.Media.CompositionTarget.Compile(通道通道)处的System.Windows.Media.Visual.Render(RenderContext ctx,UInt32 childIndex) System.Windows.Media.MediaContext.RenderMessageHandlerCore上的System.Windows.Media.MediaContext.Render(ICompositionTarget resizedCompositionTarget)中的.Windows.Media.CompositionTarget.System.Windows.Media.ICompositionTarget.Render(Boolean inResize,Channel channel)(Object resizedCompositionTarget )位于MS.Internal的System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,Object args,Int32 numArgs)的System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget)。 在System.Runtime的System.Threading.ExecutionContext.runTryCode(Object userData)的System.Windows.Threading.DispatcherOperation.InvokeImpl()中的Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler)。 System.Threading.ExecutionContext.Run上的CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode代码,CleanupCode,backoutCode,Object userData)System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback callback,Object state,Boolean ignoreSyncCtx)(ExecutionContext executionContext,ContextCallback callback) System.Windows.Threading.Dispatcher.ProcessQueue()在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)的System.Windows.Threading.DispatcherOperation.Invoke()处于System.Windows.Threading.DispatcherOperation.Invoke() ,布尔和处理)在MS.Win32.Hw的MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling) System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)中的ndSubclass.DispatcherCallbackOperation(Object o)位于MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam)的System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority,TimeSpan timeout,Delegate方法,Object args,Int32 numArgs)中委托catchHandler)在System.Windows.Application.RunDispatcher(Object ignore)的System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)的MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)处
在System.Windows.Application.RunInternal(窗口窗口)中,位于C:\ Users \ Hossein \ Desktop \ WPF_KSMMessenger \ WPF_KSMMessenger \ obj \ x86 \ Debug \ App.g.cs中的WPF_KSMMessenger.App.Main():系统中的第0行Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()中的.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,String [] args)
在System.Threading.Teread.ThreadStart()的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态,布尔值ignoreSyncCtx)处于System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,对象状态)处于System.Threading.Thread.ThreadStart()

我该怎么做才能解决这个问题?

感谢您的关注。

您的代码在从Google图像中选择的随机图像上正常工作,因此我猜您正在下载的图像存在某种问题。 您可以在Dispatcher.Invoke之前的处理程序中尝试以下代码。

 var encoder = new PngBitmapEncoder(); var image = new BitmapImage( new Uri( "MyImage.png", UriKind.Relative ) ); encoder.Frames.Add( BitmapFrame.Create( image ) ); using ( var stream = new FileStream( "MyImage2.png", FileMode.Create, FileAccess.Write ) ) { encoder.Save( stream ); } 

然后使用“MyImage2.png”代替。 在将图像加载到画笔之前,尝试让编码器“修复”图像。 说实话,虽然这段代码适合我,但我几乎没有发布这个,因为我不知道它是否适用于你的情况,而且我不喜欢在我不知道结果时发布代码。 绝对让我知道它是否有帮助。

文件大小为零的.png文件会产生同样的错误。