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

我已经编写了一个应用程序,需要下载* .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 Stack跟踪:at
System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri
uri,流,BitmapCacheOption cacheOption,Guid&clsId,
Boolean&isOriginalWritable,Stream&uriStream,UnmanagedMemoryStream&
unmanagedMemoryStream,SafeFileHandle和safeFilehandle)位于
System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri
baseUri,Uri uri,流流,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(Channel
频道,布尔skipOnChannelCheck)
System.Windows.Media.Imaging.BitmapSource.AddRefOnChannelCore(Channel
频道)
System.Windows.Media.Imaging.BitmapSource.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel
频道)
System.Windows.Media.ImageBrush.AddRefOnChannelCore(频道)

System.Windows.Media.Brush.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel
频道)
System.Windows.Media.RenderData.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel(Channel
通道)在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(Object
resizedCompositionTarget)位于
System.Windows.Media.MediaContext.RenderMessageHandler(Object
resizedCompositionTarget)位于
System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托
回调,对象args,Int32 numArgs)
MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
源,委托方法,对象参数,Int32 numArgs,委托
catchHandler)
System.Windows.Threading.DispatcherOperation.InvokeImpl()在
System.Threading.ExecutionContext.runTryCode(Object userData)在
System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode
代码,CleanupCode backoutCode,对象userData)位于
System.Threading.ExecutionContext.Run(ExecutionContext
executionContext,ContextCallback回调,对象状态,布尔值
ignoreSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext
executeContext,ContextCallback回调,对象状态),位于
System.Windows.Threading.DispatcherOperation.Invoke()在
System.Windows.Threading.Dispatcher.ProcessQueue()在
System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd,Int32
msg,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(委托
回调,对象args,Int32 numArgs)
MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(Object
源,委托方法,对象参数,Int32 numArgs,委托
catchHandler)
System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority
优先级,TimeSpan超时,委托方法,对象参数,Int32
MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,
Int32 msg,IntPtr wParam,IntPtr lParam)
MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&msg)位于
System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame
框架)在System.Windows.Application.RunDispatcher(对象忽略)
在System.Windows.Application.RunInternal(Window window)在
WPF_KSMMessenger.App.Main()在
C:\ Users \ Hossein \ Desktop \ WPF_KSMMessenger \ WPF_KSMMessenger \ obj \ x86 \ Debug \ App.g.cs:line
在System.AppDomain._nExecuteAssembly(RuntimeAssembly程序集,
String [] args)在
Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ExecutionContext.Run(ExecutionContext
executionContext,ContextCallback回调,对象状态,布尔值
ignoreSyncCtx)
System.Threading.ExecutionContext.Run(ExecutionContext
executeContext,ContextCallback回调,对象状态),位于
System.Threading.ThreadHelper.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”代替。尝试让编码器在将图像加载到画笔中之前“修复”图像。老实说,虽然这段代码对我有用,但我几乎没有发布此代码,因为我不知道它是否适合您的情况,而且我不喜欢在不知道结果的情况下发布代码。绝对让我知道是否有帮助。

C#无法将类型为“ System.Double”的对象转换为类型为“ System.Single” - c#

在判断此问题已得到回答之前,请阅读说明。我在下面有这个简单的代码:Dictionary<string, object> d = new Dictionary<string, object>(); d.Add("key" , 30d); System.Diagnostics.Debug.WriteLine($…

错误:无法将类型为“ System.Int32”的对象转换为类型为“ System.String”的对象 - c#

我已经完成了完美的编码注册页面,登录代码,现在UpdateCustomer页面有错误-背景信息:我正在使用Microsoft Access作为数据源 LabelState.Text = (string)Session["sState"]; LabelPostalCode.Text = (string)Session["sPost…

System.console()从Eclipse返回null,但可以通过命令提示符进行操作 - java

当我从Eclipse Helios使用System.console时,它总是返回null。但是,当我直接从命令行使用它时(即从命令提示符手动编译并执行Java源代码),我确实获得了一个Console对象。要知道为什么会这样,所以我检查了this链接。据此,当我从Eclipse运行Java代码时,后台作业调度程序必须正在启动我的JVM。这是什么意思?从命令行启…

在硬件级别模拟按键-Windows - java

我正在寻找一种语言或库,以使我可以尽可能地模拟按键,而无需实际按下按键。(我对按键级别的具体衡量标准是,当我的计算机已经在运行按键侦听器(例如MouseKey和StickyKeys)时,它是否会产生与物理按键相同的输出。)我已经尝试了许多模拟按键的方法。Java AWT库,Java win32api,python win32com sendKeys,pyth…

System.timer不会引发事件,并且控制台会立即终止 - c#

我需要一个每分钟执行一次的计时器,但是我很难让计时器完全用我以前使用的代码运行。所以我想我在做一些根本上与代码无关的错误,但是即使在Visual Studio Community 2017中刚刚创建的Console项目中,它也不会执行_timer_elapsed方法。控制台立即终止,没有错误,就好像它已经执行了所有代码using System; using …