c#高效比对大量图片的实例代码
以前传统的比较方式是遍历图片中的每一个像素,然后进行比对。这样的比对在少量图片的比对上虽然效率低一点,但是也没有什么不好。但是在大量图片比对的时候,过长的反应时间和对服务器比较高的消耗肯定是不行的。
所以微软给我们提供了另外一种比对的方法,能够正确,高效的比对出两张图片的是否相同。这种方法的原理是:将图片保存到数据流中然后使用convert.tobase64string将数据流转换为字符串,那么我们只需要比对两张图片的字符串就ok了。代码如下:
public bool checkimg(string filepath1, string filepath2)
{
memorystream ms1 = new memorystream();
image image1 = image.fromfile(filepath1);
image1.save(ms1, system.drawing.imaging.imageformat.jpeg);
string img1 = convert.tobase64string(ms1.toarray());
image image2 = image.fromfile(filepath2);
image2.save(ms1, system.drawing.imaging.imageformat.jpeg);
string img2 = convert.tobase64string(ms1.toarray());
if (img1.equals(img2))
{
return true;
}
else
{
return false;
}
}
这种方法我使用控制台测试了下时间,时间大概为0.015s左右比较一张图片的时间。还是比较高效的。
大量图片的比较
比较两张图片能够满足需求,那么大量呢? 我这边也做了这种测试的。在450张图片中,选择出其中重复的图片,并展示出来。大概时间是16s左右,基本能够满足大部分的需求了。
比较方法是,先讲450张图片,全部一次性转换为string类型,存放到一个数组里面,这样就避免了每次循环都要额外的去转换,使程序的效率降低很多。
public static list<dictionary<string, string>> chekimgss(string filepath)
{
list<dictionary<string, string>> liststr = new list<dictionary<string, string>>();
directoryinfo dir = new directoryinfo(filepath);
fileinfo[] files = dir.getfiles();
foreach (fileinfo fileinfo in files)
{
dictionary<string, string> dic = new dictionary<string, string>();
string ex = fileinfo.extension;
if (ex == ".jpg" || ex == ".png")
{
memorystream ms1 = new memorystream();
image image2 = image.fromfile(filepath + fileinfo.name);
image2.save(ms1, system.drawing.imaging.imageformat.jpeg);
string imgbase64 = convert.tobase64string(ms1.toarray());
dic["base64"] = imgbase64;
dic["imgname"] = fileinfo.name;
liststr.add(dic);
}
}
return liststr;
}
将图片的base64string和图片的名称存储在一个dictionary中,然后存储到list数组中。这样我们在比较的时候,只需要判断两个字符串是否相等就可以了。
/// <summary>
/// 对数组进行深拷贝
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
public static list<dictionary<string, string>> copylist(list<dictionary<string, string>> files)
{
memorystream ms = new memorystream();//序列化
binaryformatter bf = new binaryformatter();
bf.serialize(ms, files);
ms.position = 0;
list<dictionary<string, string>> array3 = (list<dictionary<string, string>>)bf.deserialize(ms); //反序列化
return array3;
}
/// <summary>
/// 比较图片
/// </summary>
/// <param name="listdic"></param>
/// <param name="filepath"></param>
/// <returns></returns>
public static list<dictionary<object, string>> chekimg2(list<dictionary<string, string>> listdic,string filepath)
{
list<dictionary<object, string>> list = new list<dictionary<object, string>>();
directoryinfo dir = new directoryinfo(filepath);
var files = dir.getfiles().tolist();
for (int j = 0; j < listdic.count; j++)
{
var file = listdic[j];
var filelist = copylist(listdic);
var index = 0;
var isfirst = false;
dictionary<object, string> dic = new dictionary<object, string>();
for (int i = 0; i < filelist.count; i++)
{
var fileinfo = filelist[i];
if (file["imgname"] == fileinfo["imgname"])
{
filelist.remove(fileinfo);
i -= 1;
continue;
}
//使用equals比普通的,string==string 高效很多倍
if (file["base64"].equals(fileinfo["base64"]))
{
if (!isfirst)
{
dic[++index] = file["imgname"];
isfirst = true;
}
dic[++index] = fileinfo["imgname"];
filelist.remove(fileinfo);
listdic.remove(listdic.where(c => c.values.contains(fileinfo["imgname"])).firstordefault());
i -= 1;
}
else
{
filelist.remove(fileinfo);
i -= 1;
}
}
if (dic.keys.count > 0)
{
list.add(dic);
listdic.remove(file);
j -= 1;
}
else
{
listdic.remove(file);
j -= 1;
}
}
return list;
}
这样,我们就可以将完全相同的图片都取出来存放在一个dictionary中,每一组相同的都是一个dictionary,然后存放到list数组中。需要的时候遍历出来就可以了。
总结
大量比对的时候,最好先将图片转换为string吧。如果在两个for里面实现这个转换,时间会是现在的很多倍,因为造成了许多重复的转换。然后在比对的过程中,尽量将已经比对过的图片从数组中剔除,这样比对,你会发现越来越快。
上一篇: Asp.Net Core Web Api 路由传参探幽
下一篇: 关于居中布局和IE双倍边距bug