C# CHECK BUTTON IMAGE

Ankit Singh

Total Post:341

Points:2389
Posted by  Ankit Singh
C# 
C#
 1759  View(s)
Ratings:
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?

  1. Pravesh Singh

    Post:412

    Points:2888
    Re: C# check button image

    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();

            }

        }


Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!