.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;
        }
    }
}
相关推荐
神奇的程序员2 小时前
从已损坏的备份中拯救数据
运维·后端·前端工程化
oden2 小时前
AI服务商切换太麻烦?一个AI Gateway搞定监控、缓存和故障转移(成本降40%)
后端·openai·api
李慕婉学姐3 小时前
【开题答辩过程】以《基于Android的出租车运行监测系统设计与实现》为例,不知道这个选题怎么做的,不知道这个选题怎么开题答辩的可以进来看看
java·后端·vue
m0_740043733 小时前
SpringBoot05-配置文件-热加载/日志框架slf4j/接口文档工具Swagger/Knife4j
java·spring boot·后端·log4j
招风的黑耳4 小时前
我用SpringBoot撸了一个智慧水务监控平台
java·spring boot·后端
Miss_Chenzr5 小时前
Springboot优卖电商系统s7zmj(程序+源码+数据库+调试部署+开发环境)带论文文档1万字以上,文末可获取,系统界面在最后面。
数据库·spring boot·后端
期待のcode5 小时前
Springboot核心构建插件
java·spring boot·后端
2501_921649495 小时前
如何获取美股实时行情:Python 量化交易指南
开发语言·后端·python·websocket·金融
serendipity_hky5 小时前
【SpringCloud | 第5篇】Seata分布式事务
分布式·后端·spring·spring cloud·seata·openfeign
五阿哥永琪6 小时前
Spring Boot 中自定义线程池的正确使用姿势:定义、注入与最佳实践
spring boot·后端·python