【问题标题】:an Interesting Exception While get an Image from Internet从 Internet 获取图像时出现一个有趣的异常
【发布时间】:2012-08-11 15:27:16
【问题描述】:

我正在 WPF 中开发一个程序,从 Web 获取图片并使用图像控件。 我的图片列表有 50 张图片(来自 vimeo 的缩略图)。一切看起来都很好,但第 45 张。图片有一些问题,当我到达第 45 张图片时,我得到了这个异常:

值不在预期范围内。

exception 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 参数) 在 System.Windows.Media.Imaging.LateBoundBitmapDecoder.DownloadCallback(对象 arg) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.DispatcherOperation.InvokeImpl() 在 System.Windows.Threading.DispatcherOperation.InvokeInSecurityContext(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 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 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd,Int32 msg,IntPtr wParam,IntPtr lParam,Boolean& 处理) 在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象 o) 在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(委托回调,对象 args,Int32 numArgs) 在 MS.Internal.Threading.ExceptionFilterHelper.TryCatchWhen(对象源,委托方法,对象 args,Int32 numArgs,委托 catchHandler) 在 System.Windows.Threading.Dispatcher.LegacyInvokeImpl(DispatcherPriority 优先级,TimeSpan 超时,委托方法,对象 args,Int32 numArgs) 在 MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd,Int32 味精,IntPtr wParam,IntPtr lParam) 在 MS.Win32.UnsafeNativeMethods.DispatchMessage(味精和味精) 在 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 程序集,字符串 [] 参数) 在 System.AppDomain.ExecuteAssembly(字符串 assemblyFile,证据 assemblySecurity,String [] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(对象状态) 在 System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,ContextCallback 回调,对象状态,布尔值 preserveSyncCtx) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext,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?

我希望我描述得很好。

【问题讨论】:

    标签: c# wpf image argumentexception


    【解决方案1】:

    尝试忽略颜色配置文件,可能元数据已损坏:

    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。

    【讨论】:

      【解决方案2】:

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

      来源:

      【讨论】:

        猜你喜欢
        • 2021-01-10
        • 2020-08-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-26
        相关资源
        最近更新 更多