SoFunction
Updated on 2025-03-07

C# efficiently compares example code for large numbers of pictures

The traditional way of comparison was to traverse every pixel in the picture and then compare. Although such comparisons are a little less efficient in comparing a small number of pictures, there is nothing wrong with it. However, when comparing a large number of pictures, excessive reaction time and relatively high consumption on the server are definitely not good.

So Microsoft provides us with another method of comparison, which can correctly and efficiently compare whether the two pictures are the same. The principle of this method is: save the image to the data stream and then use Convert.ToBase64String to convert the data stream into a string. Then we only need to compare the strings of the two images and it will be OK. The code is as follows:

Copy the codeThe code is as follows:

public bool CheckImg(string filePath1, string filePath2)
        {

          
            MemoryStream ms1 = new MemoryStream();
            Image image1 = (filePath1);
            (ms1, );

            string img1 = Convert.ToBase64String(());

            Image image2 = (filePath2);
            (ms1, );
            string img2 = Convert.ToBase64String(());

            if ((img2))
            {
               return true;
            }
            else
            {
                return false;
            }
        }

I used the console to test the time for this method, and the time is about 0.015s. Still relatively efficient.

Comparison of a large number of pictures   
Comparing two pictures can meet the needs, what about a large number? I have also done this kind of test here. Among the 450 images, select the repeated images and display them. It is about 16 seconds, which can basically meet most needs.

The comparison method is to first talk about 450 pictures, convert them all into string type at one time, and store them in an array, which avoids the need for additional conversions in each loop, which reduces the efficiency of the program much.

Copy the codeThe code is as follows:

public static List<Dictionary<string, string>> chekImgss(string filePath)
        {

            List<Dictionary<string, string>> liststr = new List<Dictionary<string, string>>();
            DirectoryInfo dir = new DirectoryInfo(filePath);
            FileInfo[] files = ();
            foreach (FileInfo fileInfo in files)
            {
                Dictionary<string, string> dic = new Dictionary<string, string>();
                string ex = ;
                if (ex == ".jpg" || ex == ".png")
                {
                    MemoryStream ms1 = new MemoryStream();
                    Image image2 = (filePath + );
                    (ms1, );

                    string imgBase64 = Convert.ToBase64String(());

                    dic["base64"] = imgBase64;
                    dic["imgName"] = ;
                    (dic);
                }
            }

            return liststr;
        }

Store the base64string of the image and the name of the image in a dictionary, and then store it in the list array. In this way, when we compare, we only need to determine whether the two strings are equal.

Copy the codeThe code is as follows:

/// <summary>
/// Deep copy of the array
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
        public static List<Dictionary<string, string>> CopyList(List<Dictionary<string, string>> files)
        {
MemoryStream ms = new MemoryStream();//Serialization
            BinaryFormatter bf = new BinaryFormatter();
            (ms, files);
            = 0;

List<Dictionary<string, string>> array3 = (List<Dictionary<string, string>>)(ms); //Deserialization
            return array3;
        }

        /// <summary>
/// Compare pictures
        /// </summary>
        /// <param name="listDic"></param>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static List<Dictionary<object, string>> chekImg2(List<Dictionary<string, string>> listDic,string filePath)
        {
            List<Dictionary<object, string>> list = new List<Dictionary<object, string>>();

            DirectoryInfo dir = new DirectoryInfo(filePath);
            var files = ().ToList();

            for (int j = 0; j < ; j++)
            {
                var file = listDic[j];

               
                var fileList = CopyList(listDic);
                var index = 0;
                var isFirst = false;
                Dictionary<object, string> dic = new Dictionary<object, string>();
                for (int i = 0; i < ; i++)
                {
                    var fileInfo = fileList[i];

                    if (file["imgName"] == fileInfo["imgName"])
                    {
                        (fileInfo);
                        i -= 1;
                        continue;
                    }
//Using equals is much more efficient than ordinary ones, string==string
                    if (file["base64"].Equals(fileInfo["base64"]))
                    {
                        if (!isFirst)
                        {
                            dic[++index] = file["imgName"];
                            isFirst = true;
                        }
                        dic[++index] = fileInfo["imgName"];

                        (fileInfo);

                        ((c => (fileInfo["imgName"])).FirstOrDefault());
                        i -= 1;
                    }
                    else
                    {
                        (fileInfo);
                        i -= 1;
                    }

                }

                if ( > 0)
                {
                    (dic);
                    (file);
                    j -= 1;
                }
                else
                {
                    (file);
                    j -= 1;
                }
            }
            return list;
        }

In this way, we can take out the exact same pictures and store them in a dictionary. Each group of the same ones is a dictionary, and then store them in a list array. Just traverse it when needed.

Summarize     

When comparing a lot, it is best to convert the picture to string first. If this conversion is implemented in two for, the time will be many times higher than that now, because it causes many repeated conversions. Then during the comparison process, try to remove the pictures that have been compared from the array, so that you will find that they are getting faster and faster.