ArgumentException not caught when using BitmapImage.BeginInit()

天大地大妈咪最大 提交于 2020-01-06 07:06:36

问题


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?

Example 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);
}

Example 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);
}

回答1:


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;

to your declarations.



来源:https://stackoverflow.com/questions/5682822/argumentexception-not-caught-when-using-bitmapimage-begininit

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!