我正在用 WPF 开发一个程序,从网上获取图片并使用图像控件。 我的图片列表有 50 张图片(来自 vimeo 的缩略图)。一切看起来都很好,但编号 45。图片有问题,当我到达第 45 张图片时,出现以下异常:
Value does not fall within the expected range.
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()
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);
}
当我设置 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?
希望我描述的很好。
最佳答案
尝试忽略颜色配置文件,可能元数据已损坏:
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 .
关于c# - 从 Internet 获取图像时的一个有趣异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967535/