Compare picturebox.image with image from a resource file
-
I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:
if (pictureBox1.Image == resourceFile.Image1)
{
pictureBox1.Image = resourceFile.Image2;
}else
{
pictureBox1.Image = resourceFile.Image1;
}This code compiles but the else-statement always executes. How do I make this work?
-
I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:
if (pictureBox1.Image == resourceFile.Image1)
{
pictureBox1.Image = resourceFile.Image2;
}else
{
pictureBox1.Image = resourceFile.Image1;
}This code compiles but the else-statement always executes. How do I make this work?
try using
pictureBox1.Image.Equals
-
I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:
if (pictureBox1.Image == resourceFile.Image1)
{
pictureBox1.Image = resourceFile.Image2;
}else
{
pictureBox1.Image = resourceFile.Image1;
}This code compiles but the else-statement always executes. How do I make this work?
That is because the equality operator only valid for the same objects not for the same contents. Though your pictures' contents are same but as they are two different objects in the memory, that is why the control always goes to the else block. Now comparing the two images is not so simple. One way to do is to save the two images in two different MemoryStream using
var ms = new MemeoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp);
byte[] byteArray = ms.ToArray();Do the same thing for the other image also and then check the equality of two byte arrays by reading each byte. Hope this will solve your problem.
Regards, Anindya Chatterjee[^]
-
try using
pictureBox1.Image.Equals
-
That is because the equality operator only valid for the same objects not for the same contents. Though your pictures' contents are same but as they are two different objects in the memory, that is why the control always goes to the else block. Now comparing the two images is not so simple. One way to do is to save the two images in two different MemoryStream using
var ms = new MemeoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp);
byte[] byteArray = ms.ToArray();Do the same thing for the other image also and then check the equality of two byte arrays by reading each byte. Hope this will solve your problem.
Regards, Anindya Chatterjee[^]
Actually i guess Image don't have any implementation for Equals method, so it fails. Byte check should work then.
-
That is because the equality operator only valid for the same objects not for the same contents. Though your pictures' contents are same but as they are two different objects in the memory, that is why the control always goes to the else block. Now comparing the two images is not so simple. One way to do is to save the two images in two different MemoryStream using
var ms = new MemeoryStream();
image.Save(ms, System.Drawing.Imaging.ImageFormat.MemoryBmp);
byte[] byteArray = ms.ToArray();Do the same thing for the other image also and then check the equality of two byte arrays by reading each byte. Hope this will solve your problem.
Regards, Anindya Chatterjee[^]
Didn't work! This is what I got from your code example. "Tarning1" is one picture in the resource file. "bild" is the picture box name.
System.IO.MemoryStream ms1 = new System.IO.MemoryStream();
Bitmap pic1 = new Bitmap(bild.Image);
pic1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteArray1 = ms1.ToArray();System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
Bitmap pic2 = DicePictures.Tarning1;
pic2.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteArray2 = ms2.ToArray();if (byteArray1 == byteArray2)
Console.WriteLine("Equal");
else
Console.WriteLine("Not equal");Did I miss something?
-
Didn't work! This is what I got from your code example. "Tarning1" is one picture in the resource file. "bild" is the picture box name.
System.IO.MemoryStream ms1 = new System.IO.MemoryStream();
Bitmap pic1 = new Bitmap(bild.Image);
pic1.Save(ms1, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteArray1 = ms1.ToArray();System.IO.MemoryStream ms2 = new System.IO.MemoryStream();
Bitmap pic2 = DicePictures.Tarning1;
pic2.Save(ms2, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] byteArray2 = ms2.ToArray();if (byteArray1 == byteArray2)
Console.WriteLine("Equal");
else
Console.WriteLine("Not equal");Did I miss something?
Some remarks for your code. 1. Use Image class directly instead of Bitmap. And don't use Jpeg format use MemoryBmp, it will ease byte comparing.
bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);
2. Don't use
if(byteArray1 == byteArray2)
coz you are doing the same wrong thing again. I told you to compare byte by byte using looping like as follows
if(byteArray1.Length == byteArray2.Length)
{
for(int i = 0; i < byteArray1.Length; i++)
{
if(byteArray1[i] != byteArray2[i])
return false;
}
return true;
}
return false;Hope this will give you the desired result.
Regards, Anindya Chatterjee[^]
-
Some remarks for your code. 1. Use Image class directly instead of Bitmap. And don't use Jpeg format use MemoryBmp, it will ease byte comparing.
bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);
2. Don't use
if(byteArray1 == byteArray2)
coz you are doing the same wrong thing again. I told you to compare byte by byte using looping like as follows
if(byteArray1.Length == byteArray2.Length)
{
for(int i = 0; i < byteArray1.Length; i++)
{
if(byteArray1[i] != byteArray2[i])
return false;
}
return true;
}
return false;Hope this will give you the desired result.
Regards, Anindya Chatterjee[^]
We tried that but,
bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);
Returns an ArgumentNullException. "Value cannot be null, parameter name: encoder". I guess the MemoryBmp ImageFormat is null or something. Didn't you get this exception?
-
We tried that but,
bild.Image.Save(ms1, System.Drawing.Imaging.ImageFormat.MemoryBmp);
Returns an ArgumentNullException. "Value cannot be null, parameter name: encoder". I guess the MemoryBmp ImageFormat is null or something. Didn't you get this exception?
Use System.Drawing.Imaging.ImageFormat.Bmp instead. Here is the test code
Image img1 = pictureBox1.Image;
Image img2 = pictureBox2.Image;MemoryStream ms1 = new MemoryStream(), ms2 = new MemoryStream(); img1.Save(ms1, ImageFormat.Bmp); img2.Save(ms2, ImageFormat.Bmp); byte\[\] byteArray1, byteArray2; byteArray1 = ms1.ToArray(); byteArray2 = ms2.ToArray(); if (byteArray1.Length == byteArray2.Length) { for (int i = 0; i < byteArray1.Length; i++) { if(byteArray1\[i\] != byteArray2\[i\]) MessageBox.Show("Not Same"); } MessageBox.Show("Image Same"); } else MessageBox.Show("Not Same");
Inform me if it solves the problem.
Regards, Anindya Chatterjee[^]
-
Use System.Drawing.Imaging.ImageFormat.Bmp instead. Here is the test code
Image img1 = pictureBox1.Image;
Image img2 = pictureBox2.Image;MemoryStream ms1 = new MemoryStream(), ms2 = new MemoryStream(); img1.Save(ms1, ImageFormat.Bmp); img2.Save(ms2, ImageFormat.Bmp); byte\[\] byteArray1, byteArray2; byteArray1 = ms1.ToArray(); byteArray2 = ms2.ToArray(); if (byteArray1.Length == byteArray2.Length) { for (int i = 0; i < byteArray1.Length; i++) { if(byteArray1\[i\] != byteArray2\[i\]) MessageBox.Show("Not Same"); } MessageBox.Show("Image Same"); } else MessageBox.Show("Not Same");
Inform me if it solves the problem.
Regards, Anindya Chatterjee[^]
-
I have added 4 pictures to a resource file which I want to compare with the image in a picture box An example of what I want to do is:
if (pictureBox1.Image == resourceFile.Image1)
{
pictureBox1.Image = resourceFile.Image2;
}else
{
pictureBox1.Image = resourceFile.Image1;
}This code compiles but the else-statement always executes. How do I make this work?
Try this one!! it works for me!
bool b = false;
for (int i = 0; i < 255; i++)
{
if (pictureBox_OK.Image.Palette.Entries[i] == Properties.Resources.OK.Palette.Entries[i])
{
b = true;
}
else
{
b = false;
break; // break the ( for (int i = 0; i < 255; i++))
}
}