A picture box problem in C# windows application

Aug 28 2009 6:04 AM

Hi my code for images comparison is like this
string img1_ref, img2_ref;
img1 =
new Bitmap(pictureBox2.Image);
img2 =
new Bitmap(pictureBox1.Image);
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++;
}
// progressBar1.Value++;

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

img1.Dispose();
img2.Dispose();
pictureBox2.Image = pictureBox1.Image; //after comparison iam setting picturebox1 image in picturebox2 and the picturebox1 gets new image from webcam, like this i want to compare every image with it's previous image
but it's not working after comparision the picbox1 image has displyed on picbox2 and picbox1 got same image from webcam(previous image and current image both are same) then also it showing IAMGES ARE NOT SAME
BUT interchanging happening picbox2 displaying the picbox1 image by loosing it's old image which has set from form_load and if the new image of picbox1 IS similar to previous one(at the begining picb1 and picb2 both has diff images so reuslut is IMAGES R DIFF)
means now picb1 and  picb2 have the same images then result must be IMAGES R SAME but it showing IMAGES R DIFF
 
PLESE SOLVE THIS PROBLEM FOR ME
   this problem is not occuring if i set the picb1 and picb2 with same images at beginig or before doing image comparison only it occuring if set picturebox2.image=picturebox1.image after one time image comparision has done