"没有找到适合完成此操作的成像组件."

Hos*_*her 14 .net c# wpf exception-handling

我编写了一个应用程序,它需要下载*.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);
Run Code Online (Sandbox Code Playgroud)

当下载完成事件发生时:

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;
}
Run Code Online (Sandbox Code Playgroud)

注意 因为运行这些代码块BackgroundWorker,我Dispatcher用来设置按钮Background属性

所以,当我运行程序时,System.NotSupportedException发生如下:

来自HRESULT的异常: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.UpdateBitmapSourceResource(信道的信道,布尔skipOnChannelCheck)在System.Windows.Media.Imaging.BitmapSource.AddRefOnChannelCore(Chan System.Windows上的System.Windows.Media.ImageBrush.AddRefOnChannelCore(Channel channel)的System.Windows.Media.Imaging.BitmapSource.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(频道频道)
.系统中System.Windows.Media.RenderData.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(渠道渠道)的Media.Brush.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(渠道渠道). Windows.UIElement.RenderContent(RenderContext中CTX,布尔isOnChannel)在System.Windows.Media.Visual.UpdateContent(RenderContext中CTX,VisualProxyFlags标志,布尔isOnChannel)在System.Windows.Media.Visual.RenderRecursive(RenderContext中CTX)在System.Windows System.Windows.Media上System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄) .Visual.RenderRecursive(RenderContext ct x)位于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上的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的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄). 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,资源 处理System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)处理System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)处理句柄) 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). System.Windows.Media.Visual.RenderRecursive上System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的UpdateChildren(RenderContext 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句柄)在System.Windows上的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的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)中的Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄). Visual.RenderRecursive(RenderContext ctx)位于System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄),位于System.Windows.Media.Visual.UpdateChildren(RenderContext) ctx,Resour System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)处的System.Windows.Media.Visual.UpdateChildren(RenderContext ctx,ResourceHandle句柄)处的System.Windows.Media.Visual.RenderRecursive(RenderContext ctx)处的ceHandle句柄) 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.CompositionTarget上System.Windows.Media.CompositionTarget.Compile(Channel channel)的System.Windows.Media.Visual.Render(RenderContext ctx,UInt32 childIndex)中的.Media.Visual.RenderRecursive(RenderContext ctx).位于S的System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget)的System.Windows.Media.MediaContext.Render(ICompositionTarget resizedCompositionTarget)中的System.Windows.Media.ICompositionTarget.Render(Boolean inResize,Channel channel)ystem.Windows.Media.MediaContext.RenderMessageHandler(对象resizedCompositionTarget)在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回调,对象指定参数时,的Int32 numArgs)在MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象args,Int32 numArgs,Delegate catchHandler)在System.Windows.Threading.DispatcherOperation.InvokeImpl()处于System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TestCode代码,CleanupCode backoutCode,SystemC)的System.Threading.ExecutionContext.runTryCode(Object userData) System.Whows.Threading.DispatcherOperation上System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态)的System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback回调,Object状态,Boolean ignoreSyncCtx)中的Object userData) System.Windows.Threading.Di上System.Windows.Threading.Dispatcher.ProcessQueue()的.Invoke()在MS.Win32.HwndSubclass的MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling)的spatcher.WndProcHook(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean&handling). System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback,Object args,Int32 numArgs)中的DispatcherCallbackOperation(Object o)位于MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source,Delegate方法,Object args,Int32 numArgs,Delegate catchHandler) )在System.Windows.Threading.Dispatcher.InvokeImpl(的DispatcherPriority优先权,时间跨度超时,委托方法,对象指定参数时,的Int32 numArgs)在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr的HWND,MSG的Int32,IntPtr的wParam中,IntPtr的LPARAM)在MS
位于System.Windows.Application.RunInternal(窗口窗口)a的System.Windows.Application.RunDispatcher(Object ignore)上的System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame框架)中的.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)t位于C:\ Users\Hossein\Desktop\WPF_KSMMessenger\WPF_KSMMessenger\obj\x86\Debug\App.g.cs中的WPF_KSMMessenger.App.Main():System.AppDomain._nExecuteAssembly中的第0行(RuntimeAssembly程序集,String [] args )在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回调,对象的状态,布尔ignoreSyncCtx)在System.Threading.ExecutionContext.Run(的ExecutionContext的ExecutionContext,ContextCallback回调,对象System.Threading.ThreadHelper.ThreadStart())

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

感谢您的关注.

小智 14

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


Pau*_*lls 12

您的代码在从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 );
}
Run Code Online (Sandbox Code Playgroud)

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