.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;
        }
    }
}
相关推荐
计算机学姐42 分钟前
基于SpringBoot的同城宠物照看管理系统
java·vue.js·spring boot·后端·mysql·mybatis·宠物
探索未来 航行现在2 小时前
Go语言--语法基础4--基本数据类型--类型转换
开发语言·后端·golang
我命由我123452 小时前
C++ - 数据容器之 forward_list(创建与初始化、元素访问、容量判断、元素遍历、添加元素、删除元素)
c语言·开发语言·c++·后端·visualstudio·visual studio·后端开发
Cxzzzzzzzzzz2 小时前
go语言实现用户管理系统
开发语言·后端·golang
努力也学不会java3 小时前
【RabbitMQ】 RabbitMQ快速上手
分布式·后端·中间件·rabbitmq·ruby
magic 2454 小时前
SpringMVC——第五章:视图View
java·后端·springmvc
江沉晚呤时4 小时前
深入了解 OpenIddict:实现 OAuth 2.0 和 OpenID Connect 协议的 .NET 库
后端·c#·.net·.net core
奔驰的小野码5 小时前
SpringAI实现AI应用-搭建知识库
java·人工智能·spring boot·后端·spring·知识图谱
Code哈哈笑6 小时前
【图书管理系统】环境介绍、设计数据库和表、配置文件、引入依赖
java·数据库·spring boot·后端·mybatis
浪裡遊7 小时前
利用flask设计接口
前端·后端·python·flask·web3.py·httpx