检测图像框中使用的图像

时间:2022-09-19 22:33:00

I'm trying too make a memory game.
In this game when a button is klicked the button and an picturebox will be send into a List.
I want too use the images inside the pictureboxes as a way too use this code. But even when the two images are the same the code wont work. Is there a way too check the image used like Name.jpg.

我正在努力做一个记忆游戏。在这个游戏中,当按下按钮时,按钮和图片框将被发送到列表中。我也想使用图片框内的图像作为使用此代码的方式。但即使两个图像相同,代码也无法工作。有没有办法检查像Name.jpg使用的图像。

if(buttonCount == 2)
{
    if(pictureList[0].Image == pictureList[1].Image)
    {
        buttonCount = 0;
        buttonList.RemoveAt(0)
        buttonList.RemoveAt(0);
        pictureList.RemoveAt(0);
        pictureList.RemoveAt(0);
    }
}

2 个解决方案

#1


1  

You could save an Id of the image (or e.g. the filename like you suggested) in the Tag.
So when loading the image into the picture box:

您可以在标记中保存图像的ID(或者像您建议的文件名)。因此,将图像加载到图片框中时:

string path = "PATH";
pictureBox.Image = Image.FromFile(path);
pictureBox.Tag = path;

Then you could compare the Tag.

然后你可以比较标签。

BUT I think (show us how you load the images please) this is not working as it is, because you load the image twice from the disk like:

但我认为(告诉我们你如何加载图像)这不是原样,因为你从磁盘加载图像两次,如:

pictureBox1.Image = Image.FromFile(path);
pictureBox2.Image = Image.FromFile(path);

Because then you have differen instances and so the equals returns false.
If you do it like the following it should also work:

因为那时你有不同的实例,所以equals返回false。如果你这样做,它也应该工作:

var image = Image.FromFile(path);
pictureBox1.Image = image;
pictureBox2.Image = image;

#2


0  

In your current application, you do not have enough information associated with the image object to identify it. As such, you need to possibly extend the Image class to include this information or store it in some other way for comparison.

在当前应用程序中,您没有足够的与图像对象关联的信息来识别它。因此,您需要扩展Image类以包含此信息或以其他方式存储它以进行比较。

Extending Image class

扩展Image类

public class GameImage : Image
{
    public static GameImage GetImage(string filename)
    {
        GameImage img = (GameImage)Image.FromFile(filename);
        img.FileName = filename;
        return img;
    }

    public string FileName { get; private set; }
}

Then the comparison becomes

然后比较变成了

if(buttonCount == 2)
{
    if(((GameImage)pictureList[0].Image).FileName == ((GameImage)pictureList[1].Image).FileName)
    {
        buttonCount = 0;
        buttonList.RemoveAt(0)
        buttonList.RemoveAt(0);
        pictureList.RemoveAt(0);
        pictureList.RemoveAt(0);
    }
}

Note: Note tested!

注意:注意测试!

#1


1  

You could save an Id of the image (or e.g. the filename like you suggested) in the Tag.
So when loading the image into the picture box:

您可以在标记中保存图像的ID(或者像您建议的文件名)。因此,将图像加载到图片框中时:

string path = "PATH";
pictureBox.Image = Image.FromFile(path);
pictureBox.Tag = path;

Then you could compare the Tag.

然后你可以比较标签。

BUT I think (show us how you load the images please) this is not working as it is, because you load the image twice from the disk like:

但我认为(告诉我们你如何加载图像)这不是原样,因为你从磁盘加载图像两次,如:

pictureBox1.Image = Image.FromFile(path);
pictureBox2.Image = Image.FromFile(path);

Because then you have differen instances and so the equals returns false.
If you do it like the following it should also work:

因为那时你有不同的实例,所以equals返回false。如果你这样做,它也应该工作:

var image = Image.FromFile(path);
pictureBox1.Image = image;
pictureBox2.Image = image;

#2


0  

In your current application, you do not have enough information associated with the image object to identify it. As such, you need to possibly extend the Image class to include this information or store it in some other way for comparison.

在当前应用程序中,您没有足够的与图像对象关联的信息来识别它。因此,您需要扩展Image类以包含此信息或以其他方式存储它以进行比较。

Extending Image class

扩展Image类

public class GameImage : Image
{
    public static GameImage GetImage(string filename)
    {
        GameImage img = (GameImage)Image.FromFile(filename);
        img.FileName = filename;
        return img;
    }

    public string FileName { get; private set; }
}

Then the comparison becomes

然后比较变成了

if(buttonCount == 2)
{
    if(((GameImage)pictureList[0].Image).FileName == ((GameImage)pictureList[1].Image).FileName)
    {
        buttonCount = 0;
        buttonList.RemoveAt(0)
        buttonList.RemoveAt(0);
        pictureList.RemoveAt(0);
        pictureList.RemoveAt(0);
    }
}

Note: Note tested!

注意:注意测试!