.NET 小案例—— 同质不同名图片识别

参考

.net6读取文件目录_mob649e8162c013的技术博客_51CTO博客

c#比较两张图片是否相同_c# 对比两次截屏是否一致-CSDN博客

需求

现在有一系列图像,它们的内容有的可能相同,但是名称不同,需要将他们分辨出来

环境搭建

假设图片都放在同一个目录下,现在2,4,5,6内容相同,需要将他们分辨出来

代码实现

ini 复制代码
using System.Drawing;

internal class Program
{
    private static void Main(string[] args)
    {
        // 创建目录信息对象
        DirectoryInfo directoryInfo = new DirectoryInfo("C:\\Users\\Administrator\\Desktop\\file");

        // 获取目录下的所有文件
        FileInfo[] files = directoryInfo.GetFiles();

        Dictionary<Bitmap, List<string>> dictionary = new Dictionary<Bitmap, List<string>>();

        // 遍历文件
        foreach (FileInfo file in files)
        {
            // 将 Image 对象转换为位图格式的数据
            Bitmap key = Image.FromFile(file.FullName) as Bitmap;

            string value = file.Name;
            handlePic(ref dictionary, key, value);
        }

        foreach(var item in dictionary)
        {
            List<string> values = item.Value;
            if (values.Count() > 1)
            {
                string res = "";
                foreach (string value in values)
                {
                    res += value + ",";
                }
                Console.WriteLine("发现了重复的图片:分别为"+ res);
            }
        }
    }

    public static void handlePic(ref Dictionary<Bitmap, List<string>> dic, Bitmap key,string value)
    {
        bool isExist = false;
        Bitmap temp = null;
        foreach(var item in dic)
        {
            if (ImageCompareString(item.Key, key))
            {
                isExist = true;
                temp = item.Key;
                break;
            };
        }

        if (isExist)
        {
            dic[temp].Add(value);
        }
        else
        {
            dic.Add(key, new List<string> { value });
        }
    }

    /// <summary>
    /// 比较两张图片是否完全一样
    /// 
    /// 速度快,但对大图片可能有些误差
    /// 
    /// (方法:将图片转换为Base64后,匹配Base64)
    /// </summary>
    public static bool ImageCompareString(Bitmap firstImage, Bitmap secondImage)
    {
        MemoryStream ms = new MemoryStream();
        firstImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String firstBitmap = Convert.ToBase64String(ms.ToArray());
        ms.Position = 0;
        secondImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
        String secondBitmap = Convert.ToBase64String(ms.ToArray());
        if (firstBitmap.Equals(secondBitmap))
        {
            return true;
        }
        else
        {
            return false;
        }
    }
}
相关推荐
Emma歌小白3 分钟前
完整后台模块模板
后端
得物技术5 分钟前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
Emma歌小白7 分钟前
配套后端(Node.js + Express + SQLite)
后端
isysc131 分钟前
面了一个校招生,竟然说我是老古董
java·后端·面试
uhakadotcom41 分钟前
静态代码检测技术入门:Python 的 Tree-sitter 技术详解与示例教程
后端·面试·github
幂简集成explinks1 小时前
e签宝签署API更新实战:新增 signType 与 FDA 合规参数配置
后端·设计模式·开源
River4161 小时前
Javer 学 c++(十三):引用篇
c++·后端
RoyLin1 小时前
TypeScript设计模式:迭代器模式
javascript·后端·node.js
爱海贼的无处不在1 小时前
一个需求竟然要开14个会:程序员的日常到底有多“会”?
后端·程序员