2012-08-15 62 views
3

我正在WPF中开发一个程序,从网上获取图片并使用图像控件。 我的图片列表有50张图片(来自vimeo的缩略图)。一切看起来不错,但数字45.图片有一些问题,当我得到第45张图片,我得到这个例外:一个有趣的例外,虽然从互联网上获取图像

价值不在预期的范围内。

exception http://img232.imageshack.us/img232/2748/5688301315b2497090468bc.png

我用的try-catch但我不能抓住它。因为它发生在Bitmap类中。下面是详细信息:

 
    at System.Windows.Media.ColorContext.GetColorContextsHelper(GetColorContextsDelegate getColorContexts) 
    at System.Windows.Media.Imaging.BitmapFrameDecode.get_ColorContexts() 
    at System.Windows.Media.Imaging.BitmapImage.FinalizeCreation() 
    at System.Windows.Media.Imaging.BitmapImage.OnDownloadCompleted(Object sender, EventArgs e) 
    at System.Windows.Media.UniqueEventHelper.InvokeEvents(Object sender, EventArgs args) 
    at System.Windows.Media.Imaging.LateBoundBitmapDecoder.DownloadCallback(Object arg) 
    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.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    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.LegacyInvokeImpl(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.Threading.Dispatcher.PushFrame(DispatcherFrame frame) 
    at System.Windows.Threading.Dispatcher.Run() 
    at System.Windows.Application.RunDispatcher(Object ignore) 
    at System.Windows.Application.RunInternal(Window window) 
    at System.Windows.Application.Run(Window window) 
    at System.Windows.Application.Run() 
    at youtube.App.Main() in C:\.........\obj\x86\Debug\App.g.cs:line 0 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ThreadHelper.ThreadStart_Context(Object state) 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at 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)); 

这里是源。只是这个形象:

problem image

我试了很多次,每次图像精细。但这一个是不同的?也许它是模糊的?

我该如何解决这个问题?也许我可以用不同的方式将源代码分配给image1?

我希望我描述得很好。

回答

8

试图忽略的颜色配置文件,也许是元数据被破坏:

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> 

也期待在这里Source

+0

哇工程:)非常感谢你,你是一个生命的救星:) – unbalanced 2012-08-15 10:20:40

1

这让我想起了我自己的问题。如果您在紧密的循环中加载图像,它将在X图像之后崩溃。您可能需要将线程返回给调度员一秒钟,以清理一些使用过的内存。

来源:

+0

非常感谢你。我会调查它.. – unbalanced 2012-08-15 10:24:54