使用BitmapImage.BeginInit()时未捕获ArgumentException [英] ArgumentException not caught when using BitmapImage.BeginInit()

查看:120
本文介绍了使用BitmapImage.BeginInit()时未捕获ArgumentException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么由于image.jpg具有无效的元数据头而发生ArgumentException时,第一个示例未捕获该异常,而第二个示例未捕获该异常?

Why when an ArgumentException occurs because image.jpg has an invalid metadata header does the first example catch the exception, and the second example does not?

示例1:

try
{
Uri myUri = new Uri("http://example.com/image.jpg", UriKind.RelativeOrAbsolute);
JpegBitmapDecoder decoder2 = new JpegBitmapDecoder(myUri,
                             BitmapCreateOptions.PreservePixelFormat,
                             BitmapCacheOption.Default);
BitmapSource bitmapSource2 = decoder2.Frames[0];
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

示例2:

try
{
BitmapImage src = new BitmapImage();
src.BeginInit();
src.UriSource = new Uri("http://example.com/image.jpg");
src.EndInit();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}

推荐答案

它可能正在等待,直到请求加载图像为止,例如被设置为Image控件的源.

It might be waiting until the image is requested to load it up, such as being set as the source for an Image control.

添加后也许会给你一个例外

Perhaps it would give you an exception if you added

src.CacheOption = BitmapCacheOption.OnLoad;

您的声明.

这篇关于使用BitmapImage.BeginInit()时未捕获ArgumentException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆