-1

C# - Windows Form Application I have some troubles with a button BackgroundImage. I want to check if 2 buttons have the same background image, i try this:

if (button1.BackgroundImage == button2.BackgroundImage)
            MessageBox.Show("works!");

but is not working.

How can i check if 2 buttons have the same background image?

Marius Vuscan
  • 170
  • 1
  • 2
  • 16
  • This seems like you're driving business logic from the state of the UI. This is probably quite a bad approach. In a properly structured application, the state of your application model would give you the answer without referring back to the UI... What are you ***actually*** trying to achieve? – spender Mar 10 '18 at 17:33
  • I want to make a matching game. – Marius Vuscan Mar 10 '18 at 17:35
  • @Marius98 Replace BackgroundImage with Image – Héctor M. Mar 10 '18 at 17:41
  • 1
    So create a game model that exists **independently** of the UI. Create a lookup (a dictionary or even an array) that maps an identifier (maybe an integer) to an image, and fill your model (perhaps a 2d array, if I understand your game correctly) with pairs of these identifiers. Provide a means of establishing if 2 positions in your model are a match. Now, think about the UI. You can pass it your game model and the map of images. Use this to render the UI. When it's time to match the images, refer back to the model and ask it if you have a match. ***Don't store your state as the UI***. – spender Mar 10 '18 at 17:46
  • I was thinking to something like that, maybe this will be the solution of my problem, thank you for your answer! :-) – Marius Vuscan Mar 10 '18 at 17:54
  • @Marius98 I apologize, is that my internet has fallen, but here is my solution – Héctor M. Mar 10 '18 at 21:00

2 Answers2

0

Try this

if(button1.Image == button2.Image)
{
   MessageBox.Show("Works");
}
Community
  • 1
  • 1
Héctor M.
  • 2,302
  • 4
  • 17
  • 35
0

Well, then try this

private bool ImageCompareArray(Bitmap firstImage, Bitmap secondImage)
        {
            bool flag = true;
            string firstPixel;
            string secondPixel;
         
            if (firstImage.Width == secondImage.Width 
                && firstImage.Height == secondImage.Height)
            {
                for (int i = 0; i<firstImage.Width; i++)
                {
                    for (int j = 0; j<firstImage.Height; j++)
                    {
                   firstPixel = firstImage.GetPixel(i, j).ToString();
                   secondPixel = secondImage.GetPixel(i, j).ToString();
                   if (firstPixel != secondPixel)
                   {
                       flag = false;
                       break;
                   }
               }
           }
    
           if (flag == false)
           {
               return false;
           }
           else
           {
               return true;
           }
       }
       else
       {
           return false;
       }
   }

And usage

private void Form2_Load(object sender, EventArgs e)
        {
            if(ImageCompareArray((Bitmap)button1.BackgroundImage, (Bitmap)button2.BackgroundImage))
            {
                MessageBox.Show("Yes");
            }
            else
            {
                MessageBox.Show("No");
            }
        }
Community
  • 1
  • 1
Héctor M.
  • 2,302
  • 4
  • 17
  • 35