“Aucun composant d’imagerie adapté à cette opération n’a été trouvé.”

J’ai programmé une application qui doit télécharger les fichiers * .png et la définir à l’arrière-plan du bouton dans WPF . Donc, quand je lance ce programme, il fait face à l’erreur comme

Aucun composant d’imagerie approprié pour effectuer cette opération n’a été trouvé. ”

Mon code est comme ci-dessous:

La première application télécharge le fichier avec l’object de la classe 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); 

et quand l’événement de téléchargement terminé a eu lieu:

 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; } 

NOTE En raison de l’exécution de ces blocs de code dans BackgroundWorker , j’utilise Dispatcher pour définir la propriété du bouton d’ Background .

Ainsi, lorsque System.NotSupportedException le programme, System.NotSupportedException s’est produit comme ci-dessous:

Exception de HRESULT: 0x88982F50 Code d’erreur: -2003292336 Message: Aucun composant de création d’image approprié pour effectuer cette opération n’a été trouvé. Source: PresentationCore Stack Trace: at System.Windows.Media.Imaging.BitmapDeCoder.SodupFromUriO (Uri uri, StreamMap, EpidBoard, Epandeur De Brûlée, éponge). Imaging.BitmapDecoder.CreateFromUri BitmapSource.CompleteDelayedCreation ()
à System.Windows.Media.Imaging.BitmapSource.get_WicSourceHandle ()
à System.Windows.Media.Imaging.BitmapSource.get_DUCECompatiblePtr ()
sur System.Windows.Media.Imaging.BitmapSource.UpdateBitmapSourceResource (Channel channel, Boolean skipOnChannelCheck) sur System.Windows.Media.Imaging.BitmapSource.AddRefOnChannelCore (Channel channel) sur System.Windows.Imaging.Bitch Media.Composition.DUCE.IResource.AddRefOnChannel (canal de canal) à System.Windows.Media.ImageBrush.AddRefOnChannelCore (canal de canal)
sur System.Windows.Media.Brush.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel (canal de canal) sur System.Windows.Media.RenderData.System.Windows.Media.Composition.DUCE.IResource.AddRefOnChannel (Channel) channel) à System.Windows.UIElement.RenderContent (RenderContext ctx, Boolean isOnChannel) à System.Windows.Media.Visual.UpdateContent (RenderContext ctx, VisualProxyFlags flags, Boolean isOnChannel) à System.White ) à System.Windows.Media.Visual.UpdateChildren (ctx RenderContext, descripteur ResourceHandle) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à RenderContext ctx). System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, descripteur ResourceHandle) à System.Windows.Media.Visual.Media.Visual.RenderRecursive (RenderContext). .Visual.UpdateChildren (RenderCo ntext ctx, descripteur ResourceHandle) sur System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) sur System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, ResourceHandle handle) sur System.Windows. ) à System.Windows.Media.Visual.UpdateChildren (ctx RenderContext, descripteur ResourceHandle) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à RenderContext ctx). System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, descripteur ResourceHandle) à System.Windows.Media.Visual.Media.Visual.RenderRecursive (RenderContext). .Visual.UpdateChildren (ctx RenderContext, descripteur ResourceHandle) à l’adresse System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Vedia.UpdateChildren (RenderContext ctx, ResourceHandle handle). .RenderRecur sive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, ResourceHandle handle) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visite ResourceHandle (Gestionnaire de ressources) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, ResourceHandle handle) à System.Windows.Media.Visual.Remours (Render) (Render). .Windows.Media.Visual.UpdateChildren (ctx RenderContext, descripteur ResourceHandle) à System.Windows.Media.Vender.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RediaRecursive (RenderContext ctx) à System.Windows.Media.Visual.Media.UpdateChildren (RenderContext ctx) .Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, ResourceHandle handle) à System.Windows.Media.Visual.RenderRecursive (RenderContext, RenderContext) à System.Wal. UpdateChildren (Render) Context ctx, ResourceHandle handle) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, ResourceHandle handle) à System.Windows. ) à System.Windows.Media.Visual.UpdateChildren (ctx RenderContext, descripteur ResourceHandle) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à RenderContext ctx). System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, descripteur ResourceHandle) à System.Windows.Media.Visual.Media.Visual.RenderRecursive (RenderContext). .Visual.UpdateChildren (ctx RenderContext, descripteur ResourceHandle) à l’adresse System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Vedia.UpdateChildren (RenderContext ctx, ResourceHandle handle). .RenderRec ursive (RenderContext ctx) à System.Windows.Media.Visual.UpdateChildren (RenderContext ctx, ResourceHandle handle) à System.Windows.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Visualiser ResourceHandle (Gestionnaire de ressources) à System.Windows.Media.Visual.RenderRecursive (RenderContext ctx) à System.Windows.Media.Visual.Render (RenderContext ctx, UInt32 childIndex) à System.Windows.Media.CompositionTarget.Comstack (Channel Channel) .Windows.Media.CompositionTarget.System.Windows.Media.ICompositionTarget.Render (Boolean inResize, Channel channel) à System.Windows.Media.MediaContext.Render (ICompositionTarget redimensionné, Channel) à ) à System.Windows.Media.MediaContext.RenderMessageHandler (Object resizedCompositionTarget) à System.Windows.Threading.ExceptionWrapper.InternalRealCall (rappel de délégué, arguments, Int32 numArgs) à MS.Internal. Threading.ExceptionFilterHelper.TryCatchWhen (Source de l’object, méthode Delegate, Arguments de l’object, Int32 numArgs, Delegate catchHandler) à System.Windows.Threading.DispatcherOperation.InvokeImpl () à System.Threading.ExecutionContext.runTryCode (Object userData) à System.Runtime. ComstackrServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup (TryCode code, CleanupCode backoutCode, Object userData) à System.Threading.ExecutionContext.Run (en anglais seulement) , Etat de l’object) à System.Windows.Threading.DispatcherOperation.Invoke () à System.Windows.Threading.Dispatcher.ProcessQueue () à System.Windows.Threading.Dispatcher.WndProcHook (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, , Booléen & manipulé) chez MS.Win32.HwndWrapper.WndProc (IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean & manipulé) chez MS.Win32.Hw ndSubclass.DispatcherCallbackOperation (Object o) à System.Windows.Threading.ExceptionWrapper.InternalRealCall (Rappel de délégué, Arguments d’object, IntArt numArgs) à MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen (Source d’object, Méthode de décompte, Argument de rôle, Déléguer catchHandler) à System.Windows.Threading.Dispatcher.InvokeImpl (priorité DispatcherPriority, délai TimeSpan, méthode Delegate, arguments arg, Int32 numArgs) à MSp.Win32.HwndSubclass.SubclassWndProc (IntPp hp à MS.Win32.UnsafeNativeMethods.DispatchMessage (MSG & msg) à System.Windows.Threading.Dispatcher.PushFrameImpl (cadre DispatcherFrame) à System.Windows.Application.RunDispatcher (Object ignore)
sur System.Windows.Application.RunInternal (fenêtre) sur WPF_KSMMessenger.App.Main () dans C: \ Utilisateurs \ Hossein \ Desktop \ WPF_KSMMessenger \ WPF_KSMMessenger \ obj \ x86 \ Debug \ App.g.cs: ligne 0 dans System .AppDomain._nExecuteAssembly (assembly RuntimeAssembly, argument Ssortingng []) chez Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
à System.Threading.ExecutionContext.Run (ExecutionContext executionContext, rappel ContextCallback, état de l’object, Boolean ignoreSyncCtx) à System.Threading.ExecutionContext.Run (ExecutionContext executionContext, rappel ContextCallback, état de l’object) à System.Threading.ThreadHead.png.

Que dois-je faire pour résoudre ce problème?

Merci de votre attention.

Votre code fonctionne bien pour moi sur une image aléatoire sélectionnée à partir d’images Google. Je suppose donc que l’image que vous téléchargez présente un problème. Vous pouvez essayer le code suivant dans le gestionnaire juste avant 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 ); } 

Ensuite, utilisez “MyImage2.png” à la place. C’est une tentative pour que l’encodeur “corrige” l’image avant de la charger dans le pinceau. Pour être honnête, bien que ce code fonctionne pour moi, je ne l’ai presque pas posté car je ne sais pas si cela fonctionnera dans votre cas, et je n’aime pas poster de code lorsque je ne connais pas le résultat. Laissez-moi savoir si cela peut aider.

Les fichiers .png avec une taille de fichier nulle donneront cette erreur identique.