2011-09-03 33 views
13

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

NOTE のBackgroundWorkerでコードのこれらのブロックを実行するので、私は、ボタンの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() 

この問題を解決するにはどうすればよいですか?

ご協力いただきありがとうございます。

+2

ファイル自体を確認しましたか?それが正しいか? –

+1

オペレーティングシステムとは何ですか? XP上のWPFのPNGは問題を引き起こす可能性があります。 –

+1

@ Mr.失望:私はXPではなくWin7を使用しています! –

答えて

11

あなたのコードは、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"を使用してください。画像をブラシにロードする前にエンコーダに画像を「修正」させようとする試みです。正直言って、このコードは私にとっては効果的ですが、あなたの状況でうまくいくかどうかわからないのでほとんど投稿しませんでした。結果が分からないときにコードを投稿するのは好きではありません。間違いなく助けてくれたら教えてください。

+0

ご回答いただきありがとうございます。あなたのコードをテストしましたが、それは別のプロジェクトでうまく動作しますが、私のプログラムではまだ問題があります。 –

+0

問題の画像の1つにリンクを投稿できますか?または、imageshackのような場所にアップロードしますか? –

+0

ヤフーのアバター画像リンクを使用しました。私はチャットクライアントプログラムをプログラミングしています。ですから、私はこの画像をダウンロードして処理する必要があります。 http://img.msg.yahoo.com/avatar.php?format=png&yids= 画像をダウンロードするリンクを使用しています。 –

11

.pngファイルサイズがゼロのファイルでは、この同じエラーが発生します。

関連する問題