ArrayCreator
ArrayCreator

Reputation: 93

How to get difference between 2 images and save it to an image

And what i mean by the title is I have 2 images - each one is from a different time ( from screen cap) i want to get the difference between the two make all the same parts transparent, send the difference to a client and place it on top of an image that he has

Don't worry about the sending part - I have covered that. The thing I need help is how to get the difference, save it to an image / stream / byte array and merging it to one image.

Upvotes: 1

Views: 5974

Answers (2)

TaW
TaW

Reputation: 54433

You create and fill a target Bitmap.

Do try to use LockBits if performance is too bad. Using a library should be even faster.

Here is a quick piece of code; you pass in two Bitmaps and the Color the differences should be painted in, e.g. Color.Red or Color.Transparent.

It returns the difference Bitmap or null if the Bitmap Sizes don't match.

public Bitmap getDifferencBitmap(Bitmap bmp1, Bitmap bmp2, Color diffColor)
{
    Size s1 = bmp1.Size;
    Size s2 = bmp2.Size;
    if (s1 != s2) return null;

    Bitmap bmp3 = new Bitmap(s1.Width, s1.Height);

    for (int y = 0; y < s1.Height; y++)
        for (int x = 0; x < s1.Width; x++)
        {
            Color c1 =    bmp1.GetPixel(x, y);
            Color c2 =    bmp2.GetPixel(x, y);
            if (c1 == c2) bmp3.SetPixel(x, y, c1);
            else          bmp3.SetPixel(x, y, diffColor);
        }
    return bmp3;
}

You could call it like this:

Bitmap bmp1 = new Bitmap(filepath1);
Bitmap bmp2 = new Bitmap(filepath2);
Bitmap bmp3 = getDifferencBitmap(bmp1, bmp2, Color.Transparent);

bmp3.Save(filepath3, System.Drawing.Imaging.ImageFormat.Png);

bmp1.Dispose();
bmp2.Dispose();
bmp3.Dispose();

Make sure you dispose of the Bitmaps when you are done with them!

Of course you can easily change the logic to make identical parts Transparent and non-identical parts whatever.. I assumed you really wanted to show the differences, though.

Here is a post which contains the code for a LockBits version; you just would need to change the inner loop's code to this:

    for (int x = 0; x < s1.Width; x++)
    {
        int index1 = y * bmp1Data.Stride + x * bpp1;
        int index2 = y * bmp2Data.Stride + x * bpp2;
        int index3 = y * bmp3Data.Stride + x * bpp3;
        Color c1, c2;
        if (bpp1 == 4)
                c1 = Color.FromArgb(data1[index1 + 3], data1[index1 + 2], data1[index1 + 1], data1[index1 + 0]);
        else  c1 = Color.FromArgb(255, data1[index1 + 2], data1[index1 + 1], data1[index1 + 0]);
        if (bpp1 == 4)
                c2 = Color.FromArgb(data2[index2 + 3], data2[index2 + 2], data2[index2 + 1], data2[index2 + 0]);
        else  c2 = Color.FromArgb(255, data2[index2 + 2], data2[index2 + 1], data2[index2 + 0]);
        Color putColor = (c1 == c2 ? c1 : diffColor);

        data3[index3 + 0] = putColor.B;
        data3[index3 + 1] = putColor.G;
        data3[index3 + 2] = putColor.R;
        data3[index3 + 3] = putColor.A;
    }

Upvotes: 3

Anant Dabhi
Anant Dabhi

Reputation: 11104

Here is a method that you can check two image and compare pixel to pixel

public void matchimage(System.Drawing.Bitmap img1, System.Drawing.Bitmap img2)
    {
        string img1_ref, img2_ref;
        int count1 = 0, count2 = 0;
        bool flag = true;

        if (img1.Width == img2.Width && img1.Height == img2.Height)
        {
            for (int i = 0; i < img1.Width; i++)
            {
                for (int j = 0; j < img1.Height; j++)
                {
                    img1_ref = img1.GetPixel(i, j).ToString();
                    img2_ref = img2.GetPixel(i, j).ToString();
                    if (img1_ref != img2_ref)
                    {
                        count2++;
                        flag = false;
                        break;
                    }
                    count1++;
                }

            }
            if (flag == false)
                MessageBox.Show("Sorry, Images are not same , " + count2 + " wrong pixels found");
            else
                MessageBox.Show(" Images are same , " + count1 + " same pixels found and " + count2 + " wrong pixels found");
        }
        else
            MessageBox.Show("can not compare this images");

        img1.Dispose();
        img2.Dispose();
    }

Upvotes: 0

Related Questions