Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/264.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何比较两个System.Drawing.Icon项目_C#_Comparison_Icons - Fatal编程技术网

C# 如何比较两个System.Drawing.Icon项目

C# 如何比较两个System.Drawing.Icon项目,c#,comparison,icons,C#,Comparison,Icons,我正试图用文件和文件夹的图标填充WPF中的树视图,就像Windows资源管理器一样。问题是,加载速度非常慢,因为我使用的转换器只调用 return Imaging.CreateBitmapSourceFromHIcon(icon.Handle, new Int32Rect(0, 0, c.Width, c.Height), BitmapSizeOptions.FromEmptyOptions()); 我假设这会为我得到的每个文件/文件夹创建一个新图标。我使用ManagedWinAPI扩展名检索

我正试图用文件和文件夹的图标填充WPF中的树视图,就像Windows资源管理器一样。问题是,加载速度非常慢,因为我使用的转换器只调用

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;
}
字典数据=新字典();
公共对象转换(对象值、类型targetType、对象参数、System.Globalization.CultureInfo区域性)
{
图标c=(图标)值;
位图bmp=c.ToBitmap();
//散列图标
ImageConverter converter=新的ImageConverter();
byte[]rawIcon=converter.ConvertTo(bmp,typeof(byte[])作为字节[];
MD5CryptoServiceProvider md5=新的MD5CryptoServiceProvider();
byte[]hash=md5.ComputeHash(rawIcon);
图像源结果;
data.TryGetValue(散列,输出结果);
如果(结果==null)
{
PrintByteArray(hash);//自定义方法,为两个文件夹图标打印相同的值
结果=Imaging.CreateBitmapSourceFromHIcon(c.Handle,new Int32Rect(0,0,c.Width,c.Height),BitmapSizeOptions.FromEmptyOptions());
添加(散列、结果);
}
其他的
{
Console.WriteLine(“找到相等的图标”);
}
返回结果;
}

您必须比较位图,或者根据位图计算哈希值,然后进行比较

在Visual C上,Kicks向您展示了如何从位图计算哈希值

编辑:根据OP修改问题的方式提供一些额外信息:

我不会使用byte[]作为字典键——我不确定它是否实现了IComparable。如果可以将字节数组转换为字符串(实现IComparable),那么它可能会工作

您可以将字节数组转换为字符串,如下所示:

StringBuilder sb = new StringBuilder();
for (int i = 0; i < result.Length; i++)
{
    sb.Append(result[i].ToString("X2"));
}
StringBuilder sb=新建StringBuilder();
for(int i=0;i
使用
图标。将
作为字典键处理。

如何知道要加载哪个图标?必须有比使用
CreateBitmapSourceFromHIcon
更有效的方法来访问/转换图标。这是用于处理非托管图标数据的。@Damien我还没有找到一个,这似乎是一个可以将其带到WFP ImageSource的图标。已经尝试过了,但似乎它们也不同(即使是两个文件夹)。文章不错!对于相同的两个图标,哈希似乎给出了相同的字节[]。但不知何故,我的字典仍然说,关键不在列表中。我将在我的问题中公布我的完整转换方法。