【问题标题】:"No imaging component suitable to complete this operation was found."“未找到适合完成此操作的成像组件。”
【发布时间】: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


【解决方案1】:

.png 文件大小为零的文件会出现同样的错误。

【讨论】:

  • 这是我的问题。在第二次读取之前,我尝试读取相同的 Stream 两次而不将位置设置为 0。
  • 当您尝试加载刚从 Windows 资源管理器创建的 BMP 文件时也会发生这种情况。
【解决方案2】:

您的代码适用于我从 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”。这是一种尝试在将图像加载到画笔之前让编码器“修复”图像。老实说,虽然这段代码对我有用,但我几乎没有发布它,因为我不知道它是否适用于你的情况,而且我不喜欢在不知道结果时发布代码。如果有帮助,请务必告诉我。

【讨论】:

  • 非常感谢您的回复。我已经测试了你的代码,它在单独的项目中运行良好,但是,在我的程序中,它仍然有这个问题。
  • 您能否发布指向其中一张问题图片的链接?或者上传一个到像 imageshack 这样的地方?
  • 我使用了雅虎头像图片链接。我正在编写一个聊天客户端程序。所以,我必须下载并处理这些图像。我正在使用img.msg.yahoo.com/avatar.php?format=png&yids=<ID> 链接下载图片。
  • 我对实际图像更感兴趣。将给您带来问题的图像之一保存到文件中。然后将该文件上传到imageshack.us。选择“不调整大小”。获得位置后,将其发回此处。
  • 我发现了问题,Yahoo 无法响应某些ID 并且程序无法下载有效的*.png 文件(例如:img.msg.yahoo.com/avatar.php?format=png&yids=arefpj),导致程序崩溃。无论如何,非常感谢您的回复:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-22
  • 1970-01-01
  • 2015-10-14
相关资源
最近更新 更多