C# 比较两张图片是否完全相同

  本文演示如何比较两张图片是否完全相同。

(注意:如果是比较两张图片是否相似,则比较复杂,涉及到机器学习)

方法一:把图片保存到内存流中,然后转化成 Base64 字符串进行比较

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;

namespace CompareImage
{
    class Program
    {
        static string sourceImageString;
        static string targetImageString;
        static List<string> _paths = new List<string>();

        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            string targetImageString = GetImageBase64String(@"xxx");
            //待比较的图片文件路径集合
            ProcessDirectory(@"yyy");

            foreach (var filePath in _paths)
            {
                string sourceImageString = GetImageBase64String(filePath);

                if (targetImageString.Equals(sourceImageString))
                {
                    Console.WriteLine("same");
                }
                else
                {
                    Console.WriteLine("different");
                }
            }

            stopwatch.Stop();
            Console.WriteLine("比较了 " + _paths.Count + " 张照片,总用时:" + stopwatch.ElapsedMilliseconds + " 毫秒");

            Console.ReadLine();
        }


        // 根据传入的文件夹路径,递归查出它包含的所有文件和文件夹,并处理各个文件夹下包含的文件
        public static void ProcessDirectory(string targetDirectory)
        {
            // 处理 targetDirectory 路径下的文件列表
            string[] fileEntries = Directory.GetFiles(targetDirectory);

            foreach (string fileName in fileEntries)
            {
                ProcessFile(fileName);
            }

            // 递归到 targetDirectory 路径下的子路径中
            string[] subdirectoryEntries = Directory.GetDirectories(targetDirectory);

            foreach (string subdirectory in subdirectoryEntries)
            {
                ProcessDirectory(subdirectory);
            }
        }

        // 这里添加如何处理找到的文件的逻辑
        public static void ProcessFile(string path)
        {
            if (!path.Contains(".db"))
                _paths.Add(path);
        }

        static string GetImageBase64String(string imagePath)
        {
            string imageString = string.Empty;
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (Image image = Image.FromFile(imagePath))
                {
                    image.Save(memoryStream, System.Drawing.Imaging.ImageFormat.Jpeg);
                    imageString = Convert.ToBase64String(memoryStream.ToArray());
                }
            }

            return imageString;
        }
    }
}   

 方法二:按图片尺寸依次比较各个像素值(要求图片尺寸一样)

using System;
using System.Diagnostics;
using System.Drawing;

namespace CompareImage
{
    class Program
    {
        static void Main(string[] args)
        {
            Stopwatch stopwatch = new Stopwatch();
            stopwatch.Start();

            int countSame = 0;
            int countDifferent = 0;

            Bitmap bitmapSource = new Bitmap(@"xxx");
            Bitmap bitmapTarget = new Bitmap(@"yyy");

            //照片尺寸必须一样
            for (int i = 0; i < bitmapTarget.Width; i++)
            {
                for (int j = 0; j < bitmapTarget.Height; j++)
                {
                    if (bitmapSource.GetPixel(i, j).Equals(bitmapTarget.GetPixel(i, j)))
                    {
                        countSame++;
                    }
                    else
                    {
                        countDifferent++;
                    }
                }
            }

            stopwatch.Stop();
            Console.WriteLine("相同像素个数:" + countSame + ",不同像素个数:" + countDifferent + "用时:" + stopwatch.ElapsedMilliseconds + " 毫秒");

            Console.ReadLine();
        }
    }
}

 

posted on 2019-07-08 13:48  wangzhiliang  阅读(5845)  评论(0编辑  收藏  举报

导航