I have 2 buttons in my Form.
I set the property of Image of 2 buttons load from 1 image in my hardrive.
But I cannot use "==" operator to check whether two images in two buttons is the same??(I knew they are the same)
ex:
if (button1.Image == button2.Image) //The images in button1 and button I loaded from the same file!
MessageBox.Show("the same images");
else
MessageBox.Show("different images");
--->They show MessageBox02: different images
Please help me how to compare the property of images in 2 buttons.
Thanks in advance.
Loading
AlanPosted Nov 21, 2008, 4:53 PM
The probable reason why that doesn't work is that you've loaded the image from the file twice using Image.FromFile() or some other method.
A new Image object would be created each time even though you were loading the image from the same file and so Equals() would not work.
In my sample code, I was only loading the image from the file once and then assigning it to the Image properties of the two buttons.
If you're using a fixed number of images in your application, then you could consider loading them all into an image array to begin with and then assigning them to the buttons as needed. That way the same image file would always be represented by the same Image object and so the Equals() method would work.
If this isn't practical, then the only way to compare the images would be byte by byte as in Manish's code.
kPosted Nov 21, 2008, 3:38 PM
Hi Alan, I modified your code into my application, but it doesn’t work yet.
Button btn01 = new Button();
Button btn02 = new Button();
Image img;
//Image in Tribe.fields.square[i, j].Image and //Tribe.squareimage.person.Images[0] are from the same file
img = Tribe.fields.square[i, j].Image;
btn01.Image = img;
btn02.Image = Tribe.squareimage.person.Images[0];
Tribe.fields.square[i, j].Image = null;
if (btn01.Image.Equals(btn02.Image))
{
Tribe.fields.square[cell.Row, cell.Col].Image = img;
//Tribe.squareimage.person.Images[0];
Tribe.fields.square[cell.Row, cell.Col].BackColor = Color.Red;
}
else
MessageBox.Show("not the same");
The result is “not the same” in MessageBox, L
AlanPosted Nov 21, 2008, 7:14 AM
If you just want to check that the Image objects are the same, then load them to a temporary variable first:
Image img = Image.FromFile(path);
button1.Image = img;
button2.Image = img;
if (button1.Image.Equals(button2.Image))
MessageBox.Show("the same images");
else
MessageBox.Show("different images");
Blocked AccountPosted Nov 20, 2008, 10:53 PM
Hi,
I think u can't directly compare the two images. u can do comparison by using this code.
Public Class Form1
Dim f1, f2 As String
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim ofd As New OpenFileDialog
ofd.Filter = "JPG files (*.jpg)|*.jpg|All files (*.*)|*.*"
MsgBox("Select the first file")
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
f1 = ofd.FileName
End If
MsgBox("Select the second file")
If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
f2 = ofd.FileName
End If
If Samebytes(f1, f2) Then
MsgBox("Files are the same")
Else
MsgBox("Files are different")
End If
End Sub
Private Function Samebytes(ByVal f1 As String, _
ByVal f2 As String) As Boolean
Dim byte1() As Byte = My.Computer.FileSystem.ReadAllBytes(f1)
Dim byte2() As Byte = My.Computer.FileSystem.ReadAllBytes(f2)
If byte1.Length <> byte2.Length Then
Return False
End If
For x As Integer = 0 To byte1.Length - 1
If byte1.GetValue(x).ToString <> byte2.GetValue(x).ToString Then
Return False
End If
Next
Return True
End Function
End Class