1

I have a label that is setup to look like a UK Standard Number Plate.

The font is set to Charles Wright.

I want to be able to toggle the background image of this label with the use of a button, but I want this button to be able to identify what image is currently being used so that it can change the image accordingly.

The images are stored in the 'Properties.Resources' as 'plainFrontNumberPlate.bmp' and 'borderedFrontNumberPlate.bmp'.

I have tried:

if(this.label1.Image == Resources.plainFrontNumberPlate)
{
    this.label1.Image = Resources.borderedFrontNumberPlate;
}
else
{
    this.label1.Image = Resources.plainFrontNumberPlate;
}

But when I try to test this. The first click changes the image to 'borderedFrontNumberPlate.bmp but not back to 'plainFrontNumberPlate.bmp when I click a twice.

  • 1
    `Properties.Resource` (assuming you mean that with `Resources`) is a Factory, it creates a new object each time you ask for one, so you're comparing each time a different object **reference**. Assign your Image Resources to a collection (e.g., a `List`) or to a couple of Fields, if you have just two Images. Then you can compare the references of these Images, since the reference won't change each time. Dispose of the Images when the Form closes. – Jimi Feb 02 '21 at 12:02
  • Yes, 'Resources' is what I meant for 'Properties.Resource'. My apologies for that typo. Brilliant! I will give this a go later today. Thank you for your advise. I will respond later to confirm this has worked. – Max Allan-Smith Feb 02 '21 at 12:44
  • As far as I understand you want to compare two images so you have to convert the image to byte or string and then compare. I entered the code for you. – Meysam Asadi Feb 02 '21 at 13:53

2 Answers2

0

use this code

public Form1()
{
    InitializeComponent();
    this.label1.Image.Tag = "plainFrontNumberPlate";
}
private void btnChangeImage_Click(object sender, EventArgs e)
{
    switch (this.label1.Image.Tag.ToString())
    {
        case "plainFrontNumberPlate":
           object borderedFrontNumberPlate = Resources.ResourceManager.GetObject("borderedFrontNumberPlate");
           this.label1.Image = (Image)borderedFrontNumberPlate;
           this.label1.Image.Tag = "borderedFrontNumberPlate";
           break;
        case "borderedFrontNumberPlate":
           object plainFrontNumberPlate = Resources.ResourceManager.GetObject("plainFrontNumberPlate");
           this.label1.Image = (Image)plainFrontNumberPlate;
           this.label1.Image.Tag = "plainFrontNumberPlate";
           break;
    }
}
Meysam Asadi
  • 6,438
  • 3
  • 7
  • 17
  • 1
    Read my comment on the question. – Jimi Feb 02 '21 at 13:18
  • You don't have to compare the **content** of the Images, you have to compare two **references**. Assign the Image to two Fields, then you can compare the references when you assign one of the two objects referenced by the Fields to the Property of a Control. -- Never use an object return by `Properties.Resource` directly for comparisons. Possibly, for nothing else. Just assign the returned value to a variable of sort. – Jimi Feb 02 '21 at 13:57
  • So you have a bitmap or PNG image and an image control how does the bitmap value match the image? I put another code. – Meysam Asadi Feb 02 '21 at 14:37
  • The Image format is irrelevant. When you load a Bitmap, you have a bit-map (a sequence of bytes in memory) and create a managed object. When you assign a Bitmap object to a variable, the variable references the object's address in memory. A Bitmap is a Reference Type, not a Value Type. When you compare Bitmaps, you compare their References. When you assign a variable of Type Bitmap to a Control's Property, you assign the Reference: it points to the same object to which the variable points to. A comparison will then compare the References, to see whether they both point to the same object. – Jimi Feb 02 '21 at 14:56
  • What you need to do is to assign a Resource-generated object to a Field, so it will create a single object. Don't use `Project.Resource` as it was a storage from where you take out the same object each time you ask. As described, `Project.Resource` is a **Factory**: it will generate a new Object each time, so the comparison with an already assigned Object will always fail, since you'll be comparing, each time, different objects. – Jimi Feb 02 '21 at 15:06
  • I do not think it is possible to access the control reference. That both images refer to the same reference. see this link :https://stackoverflow.com/questions/410705/best-way-to-determine-if-two-path-reference-to-same-file-in-c-sharp – Meysam Asadi Feb 02 '21 at 15:36
  • Of course you can, you always do: `private Bitmap bitmap1 = Properties.Resources.SomeBitmap; [...] pictureBox1.Image = bitmap1`. Now `pictureBox1.Image` and `bitmap1` point to the same object (same Reference). Test with `bool isSameImage = pictureBox1.Image == bitmap1;` -- The question you linked is about matching the Path of an image file to an Image object stored in memory: completely different scenario. – Jimi Feb 02 '21 at 15:40
  • Can this code be used? Bool isSameImage = bitmap.Flags == Properties.Resources.yourimage.Flags; – Meysam Asadi Feb 02 '21 at 15:58
  • As mentioned, you cannot use objects returned by `Properties.Resources` for any kind of comparison. -- In general, those Flags can be the same in completely different Image objects. – Jimi Feb 02 '21 at 16:03
  • I am at work currently, when I get home I will respond. – Max Allan-Smith Feb 02 '21 at 17:16
0

Thank you both 'Jimi' & 'Meysam Asadi' both your answers & comments worked. I preferred to use 'Meysam Asadi' code example as it was clearly laid out and easier for me to understand.

The combination of your help has solved my issue. Thank you