Home > DeveloperSection > Forums > C# check button image
Ankit Singh

Total Post:341

Points:2389
Posted on    February-04-2014 5:26 AM

 C# C# 
Ratings:


 1 Reply(s)
 1406  View(s)
Rate this:

I need to check if the button have no image(BackColor changes to green) or an image called Atorre(BackColor changes to red), heres my code:

public static bool IsEnemyOrEmptyA(Button check)

{

    var Atorre = teste.Properties.Resources.Atorre;

    bool res;

    if (check == null || check.Image == null)

    {

        res = true;

        check.BackColor = Color.Green;

        return res;

    }

    else if (check.Image == teste.Properties.Resources.Atorre)

    {

        res = true;

        check.BackColor = Color.Red;

        return res;

    }

    else

    {

        res = false;

        return res;

    }

}

but even if its an other image, the button displays backcolor red or nothing. Any suggestions?



Pravesh Singh

Total Post:411

Points:2881
Posted on    February-04-2014 5:53 AM

Hi Ankit,

This is the only way to compare images, you shouldn't be comparing images but instead compare some variables.

   private void button1_Click(object sender, EventArgs e)

    {

        var Atorre = Resource1.test;

        var DifferentImage = Resource1.test2;

        byte[] a = BitmapToBytes(Atorre);

        byte[] b = BitmapToBytes(DifferentImage);

        bool isEqual = true;

        if (a.Length == b.Length && a != null && b != null)

        {

            for (int i = 0; i < b.Length; i++)  //compare every byte

            {

                if (b[i] != a[i])

                {

                    isEqual = false;

                    break;

                }

            }

        }

        else

        {

            isEqual = false;

        }

        if(isEqual)

            MessageBox.Show("It's EQUAL");

        else

            MessageBox.Show("Not EQUAL");

}

    //Convert Image to Bytes

    public static byte[] BitmapToBytes(Bitmap Bitmap)

    {

        System.IO.MemoryStream ms = null;

        try

        {

            ms = new System.IO.MemoryStream();

            Bitmap.Save(ms, Bitmap.RawFormat);

            byte[] byteImage = new Byte[ms.Length];

            byteImage = ms.ToArray();

            return byteImage;

        }

        catch (ArgumentNullException ex)

        {

            throw ex;

        }

        finally

        {

            ms.Close();

        }

    }



Don't want to miss updates? Please click the below button!

Follow MindStick