How to check if an image object matches a resource?

So, I'm trying to create a simple program that just changes the image in the image window when clicked. Currently, I use only two photos, so my code for the click click event function looks like this:

private void pictureBox1_Click(object sender, EventArgs e)
    {
       if (pictureBox1.Image == Labirint.Properties.Resources.first)
            pictureBox1.Image = Labirint.Properties.Resources.reitmi;
       else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
            pictureBox1.Image = Labirint.Properties.Resources.first;
    }

For some reason, the if statement does not work and the image does not change. What should I do?


Note: the source code contained an error with the second ifnegative effect of the first, if the condition works with the correction proposed by Cyral answer , but the addition elsedoes not fix the problem - step-by-step execution of the code with the help elsestill does not display matches for any image.

if (pictureBox1.Image == Labirint.Properties.Resources.first)
    pictureBox1.Image = Labirint.Properties.Resources.reitmi;
if (pictureBox1.Image == Labirint.Properties.Resources.reitmi) // was missing else
    pictureBox1.Image = Labirint.Properties.Resources.first; 
+4
3
     if (pictureBox1.Image == Labirint.Properties.Resources.first)

, .NET. , . Labirint.Properties.Resources.xxxx , - . , . :

    private Image first;
    private Image reitmi;

    public Form1() {
        InitializeComponent();
        first = Labirint.Properties.Resources.first;
        reitmi = Labirint.Properties.Resources.reitmi;
        pictureBox1.Image = first;
    }

:

    private void pictureBox1_Click(object sender, EventArgs e) {
        if (pictureBox1.Image == first) pictureBox1.Image = reitmi;
        else pictureBox1.Image = first;
    }

:

    private void Form1_FormClosed(object sender, FormClosedEventArgs e) {
       first.Dispose();
       reitmi.Dispose();
    }
+15

else if, first, reitmi, , reitmi, , first. .

if (pictureBox1.Image == Labirint.Properties.Resources.first)
    pictureBox1.Image = Labirint.Properties.Resources.reitmi;
else if (pictureBox1.Image == Labirint.Properties.Resources.reitmi)
    pictureBox1.Image = Labirint.Properties.Resources.first;
+5

, , , :

:

using System.Drawing;
using System.Drawing.Imaging;
using System.Runtime.InteropServices;
using System.Collections;

:

        // Your Images
        Image img1 = pictureBox1.Image;
        Image img2 = pictureBox2.Image;

        // Now set as bitmap
        Bitmap bmp1 = new Bitmap(img1);
        Bitmap bmp2 = new Bitmap(img2);

        // here will be stored the bitmap data
        byte[] byt1 = null;
        byte[] byt2 = null;

        // Get data of bmp1
        var bitmapData = bmp1.LockBits(new Rectangle(0, 0, bmp1.Width, bmp1.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp1.PixelFormat);
        var length = bitmapData.Stride * bitmapData.Height;
        //
        byt1 = new byte[length];
        //
        Marshal.Copy(bitmapData.Scan0, byt1, 0, length);
        bmp1.UnlockBits(bitmapData);

        // Get data of bmp2
        var bitmapData2 = bmp2.LockBits(new Rectangle(0, 0, bmp2.Width, bmp2.Height), System.Drawing.Imaging.ImageLockMode.ReadOnly, bmp2.PixelFormat);
        var length2 = bitmapData2.Stride * bitmapData2.Height;
        //
        byt2 = new byte[length2];
        //
        Marshal.Copy(bitmapData2.Scan0, byt2, 0, length2);
        bmp2.UnlockBits(bitmapData2);

        // And now compares these arrays
        if (StructuralComparisons.StructuralEqualityComparer.Equals(byt1, byt2))
        {
            MessageBox.Show("Is Equal");
        }
        else
        {
            MessageBox.Show("Isn`t equal");
        }

. .

0

Source: https://habr.com/ru/post/1569772/


All Articles