我正在開發一個應用程序,它使用.NET 3.5 sp1和C#將位圖加載到網絡之外。如何捕獲此WPF位圖加載異常?
加載代碼如下所示:
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塊攔截,這個異常顯然會被拋出我無法處理的地方。我可以使用全局WPF異常來處理它,如this SO question。但是,這將嚴重影響我的程序的控制流程,如果可能的話,我想避免這種情況。
我必須做雙源分配,因爲它顯示許多圖像缺少寬度/高度參數在微軟位圖加載程序期望他們的地方。因此,第一項任務似乎強制下載,第二項任務正確地獲得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()
碰到這個......我從使用類似技術向海報加載Flickr中的任意圖像時看到了同樣的問題。 – 2010-06-10 21:42:05