c# - "No imaging component suitable to complete this operation was found."

标签 c# .net wpf exception

我编写了一个应用程序,它需要下载 *.png 文件并将其设置到 WPF 中按钮的背景中。所以,当我运行这个程序时,它面临的错误为

No imaging component suitable to complete this operation was found."

我的代码如下:

第一个应用程序使用 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 发生如下:

Exception from HRESULT: 0x88982F50 Error Code : -2003292336 Message : No imaging component suitable to complete this operation was found. Source : PresentationCore Stack Trace : at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle) at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache) at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() at System.Windows.Media.Imaging.BitmapSource.CompleteDelayedCreation()
at System.Windows.Media.Imaging.BitmapSource.get_WicSourceHandle()
at System.Windows.Media.Imaging.BitmapSource.get_DUCECompatiblePtr()
at System.Windows.Media.Imaging.BitmapSource.UpdateBitmapSourceResource(Channel channel, Boolean skipOnChannelCheck) at System.Windows.Media.Imaging.BitmapSource.AddRefOnChannelCore(Channel channel) at System.Windows.Media.Imaging.BitmapSource.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel channel) at System.Windows.Media.ImageBrush.AddRefOnChannelCore(Channel channel)
at System.Windows.Media.Brush.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel channel) at System.Windows.Media.RenderData.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel channel) at System.Windows.UIElement.RenderContent(RenderContext ctx, Boolean isOnChannel) at System.Windows.Media.Visual.UpdateContent(RenderContext ctx, VisualProxyFlags flags, Boolean isOnChannel) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.UpdateChildren(RenderContext ctx, ResourceHandle handle) at System.Windows.Media.Visual.RenderRecursive(RenderContext ctx) at System.Windows.Media.Visual.Render(RenderContext ctx, UInt32 childIndex) at System.Windows.Media.CompositionTarget.Compile(Channel channel) at System.Windows.Media.CompositionTarget.System.Windows.Media.ICompositionTarget.Render(Boolean inResize, Channel channel) at System.Windows.Media.MediaContext.Render(ICompositionTarget resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandlerCore(Object resizedCompositionTarget) at System.Windows.Media.MediaContext.RenderMessageHandler(Object resizedCompositionTarget) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.DispatcherOperation.InvokeImpl() at System.Threading.ExecutionContext.runTryCode(Object userData) at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Windows.Threading.DispatcherOperation.Invoke() at System.Windows.Threading.Dispatcher.ProcessQueue() at System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled) at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o) at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs) at MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object source, Delegate method, Object args, Int32 numArgs, Delegate catchHandler) at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Int32 numArgs) at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam) at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg) at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame) at System.Windows.Application.RunDispatcher(Object ignore)
at System.Windows.Application.RunInternal(Window window) at WPF_KSMMessenger.App.Main() in C:\Users\Hossein\Desktop\WPF_KSMMessenger\WPF_KSMMessenger\obj\x86\Debug\App.g.cs:line 0 at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx) at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.ThreadHelper.ThreadStart()

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

感谢您的关注。

最佳答案

文件大小为零的 .png 文件将给出相同的错误。

关于c# - "No imaging component suitable to complete this operation was found.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7292764/

相关文章:

c# - ASP.NET 中 CurrencyManager 的命名空间是什么?

c# - AvalonEdit 滚动到行

.net - 为什么TreeNodeCollection不会表现出IEnumerable <TreeNode>?

.net - 在客户端请求反序列化之前和响应序列化之后 Hook 到 WCF 服务

c# - WPF TabControl with Materials Design(没有 Dragablz)

c# - 在控制台应用程序中使用 ASP.NET 成员身份提供程序

c# - 使用 lambda 重新定义函数或调用标准 API (Math.Ceiling)

c# - 中继命令上的 CanExecute 不起作用

c# - .net WPF IValueConverter 构造函数

wpf - Surface SDK 可以在 Visual Studio 2012 上运行吗?