.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;
        }
    }
}
相关推荐
用户6757049885024 分钟前
Redis有1亿个Key,如何优雅地找出特定前缀的那10万条?
后端
用户67570498850218 分钟前
程序员常犯的坑:别再用 VARCHAR 存 IP 了!用对方式,性能何止提升10倍!
后端
老马952721 分钟前
opencode8-桌面应用实战 3
前端·人工智能·后端
用户2986985301424 分钟前
Java 中的 Word 变量管理:添加、统计、获取与删除
java·后端
神奇小汤圆27 分钟前
互联网大厂精选面试八股文(附2026最新Java+AI高频题)
后端
EMA40 分钟前
智旅云图(一个智能旅游规划项目)学习指南
人工智能·后端
传说之后42 分钟前
Go 网络编程:从 TCP 字节流到自定义协议设计
后端·架构
Rust研习社42 分钟前
手把手带你使用 Bacon 高效开发应用
后端·rust·编程语言
Nturmoils1 小时前
书签真正难的不是收藏,而是找回来:我是怎么做这个 Chrome 插件的
javascript·后端·浏览器
XovH1 小时前
Django 静态文件与媒体文件处理:CSS、JS 与用户上传图片的最佳实践
后端