How to check if image object is the same as one from resource?

戏子无情 提交于 2019-12-17 10:06:31

问题


So I'm trying to create a simple program that just change the picture in a picture box when it's clicked. I'm using just two pictures at the moment so my code for the picture box click event function looks like that:

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 it's not working and the picture don't change. What should I do?


Note: original code contained bug with second if undoing effect of first if condition would work with fix suggested by Cyral's answer, but adding else did not fix the issue - stepping through the code with else still shows no matches for either 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; 

回答1:


     if (pictureBox1.Image == Labirint.Properties.Resources.first)

There's a trap here that not enough .NET programmers are aware of. Responsible for a lot of programs that run with bloated memory footprints. Using the Labirint.Properties.Resources.xxxx property creates a new image object, it will never match any other image. You need to use the property only once, store the images in a field of your class. Roughly:

    private Image first;
    private Image reitmi;

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

And now you can compare them:

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

And to avoid the memory bloat:

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



回答2:


You need to use else if, because if the image is first, you set it to reitmi, then you check if it is reitmi, which it now is, and change it back to first. This ends up not appearing to change at all.

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;



回答3:


Maybe this code can be a bit large, but works just fine with me, try it:

This is the requeriment:

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

And here is the code to compare:

        // 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");
        }

this code compares each byte image to generate the result. may have other easier ways.



来源:https://stackoverflow.com/questions/27749541/how-to-check-if-image-object-is-the-same-as-one-from-resource

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!