我正在开发一个使用.NET 3.5 sp1和C#从网络加载位图的应用程序。
加载代码如下:
try {
CurrentImage = pics[unChosenPics[index]];
bi = new BitmapImage(CurrentImage.URI);
// BitmapImage.UriSource must be in a BeginInit/EndInit block.
bi.DownloadCompleted += new EventHandler(bi_DownloadCompleted);
AssessmentImage.Source = bi;
}
catch {
System.Console.WriteLine("Something broke during the read!");
}
在bi_DownloadCompleted上加载的代码如下:
void bi_DownloadCompleted(object sender, EventArgs e) {
try {
double dpi = 96;
int width = bi.PixelWidth;
int height = bi.PixelHeight;
int stride = width * 4; // 4 bytes per pixel
byte[] pixelData = new byte[stride * height];
bi.CopyPixels(pixelData, stride, 0);
BitmapSource bmpSource = BitmapSource.Create(width, height, dpi, dpi, PixelFormats.Bgra32, null, pixelData, stride);
AssessmentImage.Source = bmpSource;
Loading.Visibility = Visibility.Hidden;
AssessmentImage.Visibility = Visibility.Visible;
}
catch {
System.Console.WriteLine("Exception when viewing bitmap.");
}
}
有时会出现一些图片会导致读者崩溃。我猜这是可以预料的。但是,异常似乎没有被try/catch块捕获,而是在我无法处理的地方抛出。我可以像这个SO问题中说的那样使用全局的WPF异常来处理它。但是,这将严重干扰程序的控制流,如果可能的话,我想避免这种情况。
我必须进行双重源分配,因为许多图像似乎缺少微软位图加载器需要的宽度/高度参数。所以,第一个分配似乎强制下载,第二个分配则使dpi/图像尺寸正确发生。
我应该怎么做来捕获和处理这个异常呢?
如果您想要复制,尝试将此图像作为URI加载:
http://i.pbase.com/o2/26/519326/1/123513540.Yub8hciV.Longford12.jpg
异常本身是:
System.ArgumentException in PresentationCore
Value does not fall within the expected range.
内部异常为:
An invalid character was found in text context.
堆栈跟踪:
at MS.Internal.HRESULT.Check(Int32 hr)
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, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.DispatcherOperation.InvokeImpl()
at System.Threading.ExecutionContext.runTryCode(Object userData)
at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
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, Boolean isSingleParameter)
at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
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.TranslateAndDispatchMessage(MSG& msg)
at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
at System.Windows.Application.RunInternal(Window window)
at LensComparison.App.Main() in C:\Users\Mark64\Documents\Visual Studio 2008\Projects\LensComparison\LensComparison\obj\Release\App.g.cs:line 48
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()