7

私はWPFでプログラムを開発しており、Webから画像を取得し、画像コントロールを使用しています。私の画像リストには50枚の写真があります(vimeoのサムネイルから)。すべてが正常に見えますが、番号は45です。画像に問題があり、45番目の画像に到達すると、次の例外が発生します。

値が期待範囲内にありません。

例外http://img232.imageshack.us/img232/2748/5688301315b2497090468bc.png

try-catchを使用しましたが、キャッチできません。Bitmapクラスで発生するためです。詳細は次のとおりです。

   System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorContextsDelegate getColorContexts)で
   System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts()で
   System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()で
   System.Windows.Media.Imaging.BitmapImage.OnDownloadCompleted(オブジェクト送信者、EventArgs e)で
   System.Windows.Media.UniqueEventHelper.InvokeEvents(オブジェクト送信者、EventArgs args)で
   System.Windows.Media.Imaging.LateBoundBitmapDecoder.DownloadCallback(Object arg)で
   System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback、Object args、Int32 numArgs)で
   MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(オブジェクトソース、デリゲートメソッド、オブジェクト引数、Int32 numArgs、デリゲートcatchHandler)で
   System.Windows.Threading.DispatcherOperation.InvokeImpl()で
   System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state)で
   System.Threading.ExecutionContext.RunInternal(ExecutionContext executeContext、ContextCallbackコールバック、オブジェクト状態、ブール値preserveSyncCtx)で
   System.Threading.ExecutionContext.Run(ExecutionContext executeContext、ContextCallbackコールバック、オブジェクト状態、ブール値preserveSyncCtx)で
   System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext、ContextCallbackコールバック、オブジェクト状態)で
   System.Windows.Threading.DispatcherOperation.Invoke()で
   System.Windows.Threading.Dispatcher.ProcessQueue()で
   System.Windows.Threading.Dispatcher.WndProcHook(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean&handled)で
   MS.Win32.HwndWrapper.WndProc(IntPtr hwnd、Int32 msg、IntPtr wParam、IntPtr lParam、Boolean&handled)で
   MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)で
   System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback、Object args、Int32 numArgs)で
   MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(オブジェクトソース、デリゲートメソッド、オブジェクト引数、Int32 numArgs、デリゲートcatchHandler)で
   System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority priority、TimeSpan timeout、Delegate method、Object args、Int32 numArgs)で
   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.Threading.Dispatcher.PushFrame(DispatcherFrameフレーム)で
   System.Windows.Threading.Dispatcher.Run()で
   System.Windows.Application.RunDispatcher(オブジェクト無視)で
   System.Windows.Application.RunInternal(ウィンドウウィンドウ)で
   System.Windows.Application.Run(ウィンドウウィンドウ)で
   System.Windows.Application.Run()で
   C:\ ......... \ obj \ x86 \ Debug \ App.g.cs:line 0のyoutube.App.Main()で
   System.AppDomain._nExecuteAssembly(RuntimeAssemblyアセンブリ、String [] args)で
   System.AppDomain.ExecuteAssembly(String assemblyFile、Evidence assemblySecurity、String [] args)で
   Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()で
   System.Threading.ThreadHelper.ThreadStart_Context(オブジェクト状態)で
   System.Threading.ExecutionContext.RunInternal(ExecutionContext executeContext、ContextCallbackコールバック、オブジェクト状態、ブール値preserveSyncCtx)で
   System.Threading.ExecutionContext.Run(ExecutionContext executeContext、ContextCallbackコールバック、オブジェクト状態、ブール値preserveSyncCtx)で
   System.Threading.ExecutionContext.Run(ExecutionContextexecutionContext、ContextCallbackコールバック、オブジェクト状態)で
   System.Threading.ThreadHelper.ThreadStart()で

これが私のコードです:

for (int i = 0; i <50 ; i++)
{
    product p = new product();

    Common.SelectedOldColor = p.Background;
    p.VideoInfo = results[i];
    Common.Products.Add(p, false);
    p.Visibility = System.Windows.Visibility.Hidden;
    p.Drop_Event += new product.DragDropEvent(p_Drop_Event);
    main.Children.Add(p);
}

プロパティを設定すると、次のp.VideoInfo = results[i];ように割り当てられます。

private VideoList videoInfo;
public VideoList VideoInfo
{
    get { return videoInfo; }
    set
    {
        videoInfo = value;
        label1.Content = videoInfo.Title;
        try
        {
             image1.Source = new BitmapImage(new Uri(videoInfo.ThumbNail));
        }
        catch (Exception ex)
        {

        }             
    }
}

image1.Source = new BitmapImage(new Uri(videoInfo.ThumbNail));

これが問題の原因です。しかし、この画像のためだけに:

問題のある画像

何度も試してみましたが、それぞれの画像は問題ありません。しかし、これは違いますか?多分それはぼやけていますか?

この問題を解決するにはどうすればよいですか?たぶん私は別の方法を使ってソースをimage1に割り当てることができますか?

よく説明したと思います。

4

2 に答える 2

12

カラープロファイルを無視してみてください。メタデータが破損している可能性があります。

var bi = new BitmapImage();
bi.BeginInit();
    bi.CreateOptions = BitmapCreateOptions.IgnoreColorProfile;
    bi.UriSource = new Uri("http://hanselman.com/blog/images/JPGwithBadColorProfile.jpg");
bi.EndInit();

foo.Source = bi;

またはXAMLを使用する:

<Image>
    <Image.Source>
        <BitmapImage CreateOptions="IgnoreColorProfile" UriSource="{Binding ....}"/>
    </Image.Source>
</Image>

こちらもソースをご覧ください。

于 2012-08-15T10:18:18.217 に答える
1

それは私自身の質問を思い出させます。タイトなループで画像をロードすると、X画像の後にクラッシュします。使用済みメモリをクリーンアップするために、スレッドをディスパッチャに1秒間戻す必要がある場合があります。

出典:

于 2012-08-15T10:18:53.283 に答える