Comparing two images to see if they are the same

In this tutorial I’m going to show you how to compare two images to see whether they are the same or not using C#.

/// <summary>
/// This method deals with checking whether the two bitmaps that are passed to it are the same or not
/// </summary>
/// <param name="img1">The first image</param>
/// <param name="img2">The second image</param>
/// <returns>True if the images are the same, else false</returns>
public static bool imagesAreTheSame(Bitmap img1, Bitmap img2)
    int imgHeight = img1.Height;
    int imgWidth = img1.Width;
    int hCounter = 0;
    int wCounter = 0;
    bool rtn = true;
    string pxl1;
    string pxl2;

    while (hCounter < imgHeight)
        wCounter = 0;
        while (wCounter < imgWidth)
            pxl1 = img1.GetPixel(hCounter, wCounter).ToString();
            pxl2 = img2.GetPixel(hCounter, wCounter).ToString();
            if (pxl1 != pxl2)
                rtn = false;
            wCounter += 1;
        hCounter += 1;
        if (!rtn)

    return rtn;

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.