问题描述
我正在开发加载位图还是使用.NET 3.5 SP1和C#网络的应用。
加载code如下:
{尝试
CurrentImage =图片[unChosenPics [指数]];
双向=新的BitmapImage(CurrentImage.URI);
// BitmapImage.UriSource必须在BeginInit在/ EndInit在块。
bi.DownloadCompleted + =新的EventHandler(bi_DownloadCompleted);
AssessmentImage.Source =双向;
}
抓{
的System.Console.WriteLine(一些在读期间发生!);
}
和code对bi_DownloadCompleted加载是:
无效bi_DownloadCompleted(对象发件人,EventArgs的发送){
尝试{
双DPI = 96;
INT宽度= bi.PixelWidth;
INT高度= bi.PixelHeight; INT跨距=宽* 4;每像素// 4字节
字节[] = PixelData取出新的字节[步幅*高]。
bi.CopyPixels(PixelData取出,步幅,0); 的BitmapSource bmpSource = BitmapSource.Create(宽度,高度,新闻部,新闻部,PixelFormats.Bgra32,空,PixelData取出,步幅); AssessmentImage.Source = bmpSource;
Loading.Visibility = Visibility.Hidden;
AssessmentImage.Visibility = Visibility.Visible;
}
抓{
的System.Console.WriteLine(异常观看时的位图。);
}
}
每隔一段时间,图像到来,打破了读者。我想这是可以预期的。然而,而不是被抓或者由那些try / catch块,异常显然是越来越扔在那里我可以处理它之外。我可以使用全局WPF例外,如这太问题。不过,这严重打乱我的程序的控制流,我想避免,如果在所有可能的。
我要做的双源分配,因为它看起来是缺乏在微软位图装载机希望他们成为地方宽度/高度参数许多图像。所以,第一项任务将出现强制下载,第二分配得到DPI /图像尺寸正确发生。
我能做些什么来捕获和处理这个异常?
如果您想进行复制,尝试加载此图像作为URI:
http://i.pbase.com/o2/26/519326/1/123513540.Yub8hciV.Longford12.jpg
例外本身是:
System.ArgumentException在presentationCore
值没有在预期的范围内。
内的例外是:
无效的字符在文本上下文找到。
堆栈跟踪:
在MS.Internal.HRESULT.Check(的Int32小时)
在System.Windows.Media.Imaging.BitmapFrameDe code.get_ColorContexts()
在System.Windows.Media.Imaging.BitmapImage.FinalizeCreation()
在System.Windows.Media.Imaging.BitmapImage.OnDownloadCompleted(对象发件人,EventArgs的发送)
在System.Windows.Media.UniqueEventHelper.InvokeEvents(对象发件人,EventArgs参数)
在System.Windows.Media.Imaging.LateBoundBitmapDe coder.DownloadCallback(对象ARG)
在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回调,对象ARGS,布尔isSingleParameter)
在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,代表回调,对象ARGS,布尔isSingleParameter,代表catchHandler)
在System.Windows.Threading.DispatcherOperation.InvokeImpl()
在System.Threading.ExecutionContext.runTry code(用户数据对象)
在System.Runtime.CompilerServices.RuntimeHelpers.Execute codeWithGuaranteedCleanup(试行code code,清理code撤销code,对象用户数据)
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Windows.Threading.DispatcherOperation.Invoke()
在System.Windows.Threading.Dispatcher.ProcessQueue()
在System.Windows.Threading.Dispatcher.WndProcHook(IntPtr的HWND,味精的Int32,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)
在MS.Win32.HwndWrapper.WndProc(IntPtr的HWND,味精的Int32,IntPtr的的wParam,lParam中的IntPtr,布尔和放大器;处理)
在MS.Win32.HwndSubclass.DispatcherCallbackOperation(对象o)
在System.Windows.Threading.ExceptionWrapper.InternalRealCall(代表回调,对象ARGS,布尔isSingleParameter)
在System.Windows.Threading.ExceptionWrapper.TryCatchWhen(对象源,代表回调,对象ARGS,布尔isSingleParameter,代表catchHandler)
在System.Windows.Threading.Dispatcher.InvokeImpl(优先级的DispatcherPriority,时间跨度超时,代表法,对象ARGS,布尔isSingleParameter)
在MS.Win32.HwndSubclass.SubclassWndProc(IntPtr的HWND,味精的Int32,IntPtr的的wParam,lParam中的IntPtr)
在MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG&安培; MSG)
在System.Windows.Threading.Dispatcher.TranslateAndDispatchMessage(MSG&安培; MSG)
在System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame帧)
在System.Windows.Application.RunInternal(窗窗)
在LensComparison.App.Main()在C:\\用户\\ Mark64 \\文档\\ Visual Studio 2008的\\项目\\ LensComparison \\ LensComparison \\ OBJ \\发布\\ App.g.cs:48行
在System.AppDomain._nExecuteAssembly(议会会议,字串[] args)
在Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
在System.Threading.ExecutionContext.Run(ExecutionContext中的ExecutionContext,ContextCallback回调,对象状态)
在System.Threading.ThreadHelper.ThreadStart()
这是例外的图像中包含的'破'的颜色配置文件信息的结果。如果你不关心这个信息(或想尝试的异常后重新解析),使用BitmapCreateOptions.IgnoreColorProfile标志。
例如:
我的BitmapImage =新的BitmapImage();
i.BeginInit();
i.CreateOptions | = BitmapCreateOptions.IgnoreColorProfile;
i.UriSource =新的URI(@http://www.bing.com/fd/hpk2/KiteFestival_EN-US2111991920.jpg);
i.EndInit();
如果您正在寻找更多的信息,请查看Scott Hanselman的帖子。 (我们都在今天这个问题上的沟通,通过电子邮件)。
I'm developing an application that loads bitmaps off of the web using .NET 3.5 sp1 and C#.
The loading code looks like:
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!");
}
and the code to load on bi_DownloadCompleted is:
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.");
}
}
Every so often, an image comes along that breaks the reader. I guess that's to be expected. However, rather than being caught by either of those try/catch blocks, the exception is apparently getting thrown outside of where I can handle it. I could handle it using global WPF exceptions, like this SO question. However, that will seriously mess up the control flow of my program, and I'd like to avoid that if at all possible.
I have to do the double source assignment because it appears that many images are lacking in width/height parameters in the places where the microsoft bitmap loader expects them to be. So, the first assignment appears to force the download, and the second assignment gets the dpi/image dimensions happen properly.
What can I do to catch and handle this exception?
If you'd like to replicate, try loading this image as the uri:
http://i.pbase.com/o2/26/519326/1/123513540.Yub8hciV.Longford12.jpg
The exception itself is:
System.ArgumentException in PresentationCore
Value does not fall within the expected range.
The inner exception is:
An invalid character was found in text context.
Stack trace:
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()
This exception is the result of 'broken' color profile information contained in the image. If you don't care about this information (or would like to try parsing again after exception), use the BitmapCreateOptions.IgnoreColorProfile flag.
Example:
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();
If you're looking for more information, check out Scott Hanselman's post. (We were all in communication about this issue today, via email.)
这篇关于我该如何抓住这个WPF位图装载例外?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!