我试图用WPF文件和文件夹的图标填充树状视图,就像Windows资源管理器一样。问题是,这是非常缓慢的加载,因为我使用一个转换器,只是调用如何比较两个System.Drawing.Icon项目
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, c.Width, c.Height), BitmapSizeOptions.FromEmptyOptions());
我认为这为每个文件/文件夹中,我得到一个新的图标。我用ManagedWinAPI
扩展名检索图像。所以现在,我正在计划使用可以比较图标的字典。
但我怎样才能比较两个System.Drawing.Icon
对象?因为参考文献总是不同(测试)。我不需要像素比较器,因为我不认为这会加快我的过程。
更新
以@Roy Dictus'答复考虑在内,该词典还告诉我,有列表中没有相等的对象:
Dictionary<byte[], ImageSource> data = new Dictionary<byte[], ImageSource>();
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
Icon c = (Icon)value;
Bitmap bmp = c.ToBitmap();
// hash the icon
ImageConverter converter = new ImageConverter();
byte[] rawIcon = converter.ConvertTo(bmp, typeof(byte[])) as byte[];
MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider();
byte[] hash = md5.ComputeHash(rawIcon);
ImageSource result;
data.TryGetValue(hash, out result);
if (result == null)
{
PrintByteArray(hash); // custom method, prints the same values for two folder icons
result = Imaging.CreateBitmapSourceFromHIcon(c.Handle, new Int32Rect(0, 0, c.Width, c.Height), BitmapSizeOptions.FromEmptyOptions());
data.Add(hash, result);
}
else
{
Console.WriteLine("Found equal icons");
}
return result;
}
你怎么知道要加载哪个图标? –
有*必须*是比使用'CreateBitmapSourceFromHIcon'更有效的访问/转换图标的方法。这是为了处理非托管图标数据。 –
@Damien我还没有找到,这似乎是一个把它带到WFP ImageSource。 – Marnix