an Interesting Exception While get an Image from Internet
Asked Answered
F

2

7

I am developing a program in WPF, getting a picture from the web and used an image control. My image list has 50 pictures (from vimeo's thumbnails). Everything looks fine but number 45. The picture has some problem and when I get to the 45th picture, I get this exception:

Value does not fall within the expected range.

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

I used try-catch but I can't catch it. Because it occurs in the Bitmap class. Here are the details:

   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()

Here is my code:

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

when I set the p.VideoInfo = results[i]; property, it assigns something:

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

Here is source of the problem. but just for this image:

problem image

I tried many times and each images are fine. But this one is different? Maybe it's blur?

How can I fix this problem? Maybe I can use different way to assign source to image1?

I hope that I described well.

Feder answered 15/8, 2012 at 10:14 Comment(0)
F
13

Try to ignore the color profile, maybe the metadata is corrupted:

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;

or using XAML:

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

Also look here Source.

Frontlet answered 15/8, 2012 at 10:18 Comment(0)
R
1

It reminds me of my own question. If you load the images in a tight loop it will crash after X images. You may need to return the thread to the dispatcher for a second to clean up some used memory.

Sources:

Ripsaw answered 15/8, 2012 at 10:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.