I am trying to fill a treeview in WPF with icons of files and folders, just like Windows Explorer does. The problem is, that it is very slow to load, because I am using a converter that just calls
return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, c.Width, c.Height), BitmapSizeOptions.FromEmptyOptions());
我认为,这为我收到的每份档案/文件创造了新的一席。 我用<代码>ManagedWinAPI的延伸检索图像。 因此,我现在正计划使用一种可相互比较的理论。
But how can I compare two System.Drawing.Icon
objects? Because the reference is always different (tested). I don t need a pixel comparator, because I don t think that will speed up my process.
<>Update>
考虑到
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;
}