.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;
        }
    }
}
相关推荐
小兔崽子去哪了5 分钟前
Java 自动化部署
java·后端
Selicens5 分钟前
git批量删除本地多余分支
前端·git·后端
哈密瓜的眉毛美9 分钟前
Java 基础补充:零基础学Java | Scanner 类详解
后端
ma_king10 分钟前
入门 java 和 数据库
java·数据库·后端
平平无奇的开发仔14 分钟前
Mybaitis 项目多模块多依赖xml加载classpath:和classpath*:的区别
后端
神奇小汤圆29 分钟前
MySQL的10种高级SQL,性能飞升
后端
AI探索者31 分钟前
LangGraph 人工干预:Human-in-the-loop 机制详解
后端
神奇小汤圆33 分钟前
Java并发核心:你以为AQS很复杂?无非是"两个队列"和"一个状态"
后端
shark_chili36 分钟前
Spring AI Alibaba 入门与实战:一文构建智能天气查询助手
后端
Java编程爱好者38 分钟前
Java 高频面试题总结(2026通用版)
后端