Tuesday, 27 May 2025

Comparing Two Images in .NET Core Using Bitmap

Leave a Comment

Create a.net core Console application in Visual Studio and install System.Drawing. The common library can be found in the NuGet Package Manager or the command line. In this post, we will compare two photos using the BitMap library in the dot net core application.

For the NuGet Package Manager Console, run the below command.

Install-Package System.Drawing.Common

For the .NET CLI command line, run the below command.

dotnet add package System.Drawing.Common

Basic image identification can be done with Hashing MD5 using the Cryptography library. For quick comparison generate a hash for each image and compare the image hashes and it will give the result.

Code Example

using System.Drawing;
using System.Security.Cryptography;

Console.WriteLine("Cryptography MD5 Quick Comparison ");
bool st= AreImagesIsIdentical(@"image1.PNG",@"image2.PNG");


Console.WriteLine("is Same image :" + st);
string GetImageHash(string imagePath)
{
    using (var md5 = MD5.Create())
    {
        using (var stream = new FileStream(imagePath, FileMode.Open))
        {
            var hash = md5.ComputeHash(stream);
            return BitConverter.ToString(hash).Replace("-", "").ToLowerInvariant();
        }
    }
}

 bool AreImagesIdentical(string imagePath1, string imagePath2)
{
    return GetImageHash(imagePath1) == GetImageHash(imagePath2);
}

Output

The bitmap library will help to compare pixels in the images. For comparing the pixel values of the two images, the code example is below.

using System.Drawing;

Console.WriteLine("Pixel Comparison ");
bool st= AreImagesIsIdentical(@"image1.PNG",@"image2.PNG");

bool AreImagesIsIdentical(string imagePath1, string imagePath2)
{
    using (Bitmap bmp1 = new Bitmap(imagePath1))
    using (Bitmap bmp2 = new Bitmap(imagePath2))
    {
        if (bmp1.Width != bmp2.Width || bmp1.Height != bmp2.Height)
        {
            return false;
        }

        for (int y = 0; y < bmp1.Height; y++)
        {
            for (int x = 0; x < bmp1.Width; x++)
            {
                if (bmp1.GetPixel(x, y) != bmp2.GetPixel(x, y))
                {
                    return false;
                }
            }
        }
    }
    return true;
}

Output

 

Best ASP.NET Core 10.0 Hosting Recommendation

One of the most important things when choosing a good ASP.NET Core 8.0 hosting is the feature and reliability. HostForLIFE is the leading provider of Windows hosting and affordable ASP.NET Core, their servers are optimized for PHP web applications. The performance and the uptime of the hosting service are excellent and the features of the web hosting plan are even greater than what many hosting providers ask you to pay for. 

At HostForLIFEASP.NET, customers can also experience fast ASP.NET Core hosting. The company invested a lot of money to ensure the best and fastest performance of the datacenters, servers, network and other facilities. Its datacenters are equipped with the top equipments like cooling system, fire detection, high speed Internet connection, and so on. That is why HostForLIFEASP.NET guarantees 99.9% uptime for ASP.NET Core. And the engineers do regular maintenance and monitoring works to assure its Orchard hosting are security and always up.

0 comments:

Post a Comment