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

前端 未结 2 810
Happy的楠姐
Happy的楠姐 2020-12-10 23:18

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

2条回答
  •  粉色の甜心
    2020-12-10 23:42

    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();
        }
    

提交回复
热议问题