假设我从ImageSourceConverter类中找到了正确的代码,当您将源指定为字符串时,转换器正在尝试执行此操作:
if (((value is string) && !string.IsNullOrEmpty((string) value)) || (value is Uri))
{
UriHolder uriFromUriContext = TypeConverterHelper.GetUriFromUriContext(context, value);
return BitmapFrame.CreateFromUriOrStream(uriFromUriContext.BaseUri, uriFromUriContext.OriginalUri, null, BitmapCreateOptions.None, BitmapCacheOption.Default, null);
}
BitmapFrame反过来使用 aBitmapDecoder来加载图像。当源是 aUri时BitmapDecoder,在一堆安全和健全性检查中,使用 a WpfWebRequestHelper(未记录的)来请求或“下载”图像。如果生成的响应流是有效文件,它会直接将流加载到新的FileStream.
之后,本机 Windows 图像解码功能将接管您的图像。另请注意,它BitmapDecoder会被缓存,因此如果您连续加载多个图像,则无需重新初始化新的BitmapDecoder. 我不能说这是否与您的性能问题有关。
总之,我猜测 WPF 在内部使用来加载图像的方法是一种高度优化的方法。我没有研究过简单下载图像的实现HttpClient与可能使用简单HttpWebRequest下载图像的可能性,但我怀疑您的方法的开销大于内置方法的开销,并且是导致性能下降的原因。
如果您想知道我是如何破译这些信息的,我只是使用名为ReflectorSystem.Windows.Media的工具检查了程序集中命名空间中的几个类。PresentationCore