Bitmap Image as a dictionary key

坚强是说给别人听的谎言 提交于 2021-01-28 20:12:53

问题


I want to know what size should be the image to use it as a hashset/dictionary key. I'm also thinking about using hash functions for this purpose but i'm afraid of hash collisions. I need to store about million of images.


回答1:


A bitmap image as a key? That's not a very good idea. Hashset keys should be as small as possible, otherwise performance will suffer significantly.

What you can do is calculate a hash value (say a SHA1) from the image, and then use that as the key. It's only 20 bytes so that should be a good size, much better than a full bitmap image anyway.




回答2:


It doesn't matter what data you use for key in a HashSet or Dictionary, but you need to implement a GetHashCode and an Equals method for it.

Hash collisions is not a problem. That will just put the two images in the same bucket, and use the Equals method to find the right image.




回答3:


Given your Image's bytes, you can use an algorithm such as MD5 or SHA1 to calculate a hash for it.

Example with bytes being your picture's byte array:

using (var md5 = new System.Security.Cryptography.MD5CryptoServiceProvider())
{
    var hash = Convert.ToBase64String(md5.ComputeHash(bytes));
    //Store hash
}


来源:https://stackoverflow.com/questions/13915954/bitmap-image-as-a-dictionary-key

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