user3092756
user3092756

Reputation: 49

Determine if two images are identical

Is there any way to determine if two images are identical? I want to change an image every time my timer ticks (animation). But, I need to see which image is displaying, so is there any way to compare 2 images to do what I want?

if (myImage.Flags == (Image.FromFile(@"Images/Enemy.png").Flags))
{
     myImage = Image.FromFile(@"Images/Enemy2.png");
}
else 
{
     myImage = Image.FromFile(@"Images/Enemy.png");
}

Upvotes: 1

Views: 364

Answers (3)

Sinatr
Sinatr

Reputation: 21969

Here goes simple answer.

In case of just 2 images, use flag

// field, true if enemy2.png is loaded
bool _image2;

// somewhere
if(_image2)
{
    myImage = Image.FromFile(@"Images/Enemy.png");
    _image2 = false;
}
else
{
    myImage = Image.FromFile(@"Images/Enemy2.png");
    _image2 = true;
}

Upvotes: 0

Thomas Levesque
Thomas Levesque

Reputation: 292355

Don't compare the images, just maintain the index of the current image in a variable.

Here's an example that works for any number of images:

private int _currentImageIndex;
private string[] _imagePaths =
{
    "Images/Enemy.png",
    "Images/Enemy2.png",
    "Images/Enemy3.png",
};


...


void NextImage()
{
    // Dispose the current image
    Image img = pictureBox1.Image;
    pictureBox1.Image = null;
    if (img != null)
        img.Dispose();

    // Show the next image
    _currentImageIndex = (_currentImageIndex + 1) % _imagePaths.Length;
    string path = _imagePaths[_currentImageIndex];
    pictureBox1.Image = Image.FromFile(path);
}

Upvotes: 3

BenG
BenG

Reputation: 53

I'd try to compare ImageLocation. Although it doesn't work if you have your pictures as resources.

if (PictureBox1.ImageLocation == PictureBox2.ImageLocation)
{

}

See my question: Dynamically changing image in a picturebox

Upvotes: 0

Related Questions