2010-04-28 34 views
4

我正在開發一個應用程序,它使用.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() 
+0

碰到這個......我從使用類似技術向海報加載Flickr中的任意圖像時看到了同樣的問題。 – 2010-06-10 21:42:05

回答

0

這似乎工作:

try 
{ 
    frame = BitmapFrame.Create(new Uri("http://i.pbase.com/o2/26/519326/1/123513540.Yub8hciV.Longford12.jpg")); 
} 
catch 
{ 
    return; 
} 

BitmapFrame有DecodeFailed事件,但我不能把它掛,因爲它創建後的BitmapFrame被凍結。

13

此例外情況是圖像中包含「損壞的」顏色配置文件信息的結果。如果您不關心此信息(或者想要在異常之後再次嘗試解析),請使用BitmapCreateOptions.IgnoreColorProfile標誌。

例子:

BitmapImage i = new BitmapImage(); 
i.BeginInit(); 
i.CreateOptions |= BitmapCreateOptions.IgnoreColorProfile; 
i.UriSource = new Uri(@"http://www.bing.com/fd/hpk2/KiteFestival_EN-US2111991920.jpg"); 
i.EndInit(); 

如果您正在尋找更多的信息,請Scott Hanselman's post。 (我們今天都在通過電子郵件通知這個問題。)

+1

+1鏈接到Scott Hanselman的帖子,非常具有攻擊性! – David 2011-11-30 09:56:04