如何使用C#在Visual Studio中访问图片框中的图像名称 [英] How do you access the name of the image in a picturebox in Visual Studio using C#

查看:140
本文介绍了如何使用C#在Visual Studio中访问图片框中的图像名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个程序,其中有16个使用图片框的网格图块,但仅使用5张图像,其余图块只是黑色图像.

I have a program which has 16 grid tiles using picturebox but only uses 5 images, the rest of the tiles are just a black image.

我想知道用户"点击了哪个图片.

I would like to be able to tell which image the 'user' clicks on.

我有一个名为image_Click(object sender,EventArgs e)的方法

I have a method called image_Click(object sender, EventArgs e)

此方法中有一个if语句,指出:

I have an if statement inside this method that states:

if (peckedSquare.BackColor == Color.Black)
{
    System.Diagnostics.Debug.WriteLine("Pecked a black square");
    return;
}

这会发送一个字符串,让我知道何时单击了黑色正方形.

This sends a String that lets me know when a black square has been clicked.

有没有一种简单的方法可以说:

Is there an easy way to perhaps say:

//伪代码:

if (peckedSquare.ImageName == pigeon1.png)
{
    System.Diagnostics.Debug.WriteLine("Pecked Pigeon number 1");
}

我已经用Google搜索了我的查询,但是没有找到合适的答案.

I have googled my query but I have not found any suitable answers.

//编辑 我刚刚重新阅读了我的代码. 我使用随机数将每张图片分配到一个图片框正方形. 我使用此随机数作为变量,因此我可以使用该变量来确定单击了哪个图像. IE.

//EDIT I have just re-read my code. I was assigning each picture to a picturebox square using a randomnumber. I had this random number as a variable, so I can just use that variable to determine which image was clicked. ie.

if (randomNumber == 1)
{
    System.Diagnostics.Debug.WriteLine("Pecked Pigeon number 1");
}

或更胜一筹

pigeonSelected = randomNumber + 1 //as I am using an array to store the images
System.Diagnostics.Debug.WriteLine("Pecked Pigeon Number {0}", pigeonSelected);

推荐答案

肮脏的解决方案,我会使用Tag属性,对于黑色图块使用null,对于其他图块使用文件路径(即使您的图像来自资源,它始终可用),如下所示:

As quick & dirty solution I would use Tag property for that, null for black tiles and file path for the others (and it's always available, even if your image comes from resources), something like this:

if (peckedSquare.Tag == null)
{
    Debug.WriteLine("Pecked a black square");
}
else
{
    switch (Path.GetFileName(peckedSquare.Tag.ToString()))
    {
        case "pigeon1.png":
        break;
    }
}

当然,当您创建图块时,必须将文件路径存储在Tag中:

Of course when you create tiles you have to store file path in Tag:

PictureBox tile = new PictureBox();
tile.Image = Image.FromFile(path); // Or another source, of course
tile.Tag = path;

作为替代方案,您甚至可以为此使用Name属性,每个控件都被命名(主要用于设计器代码集成),但是如果您在运行时创建控件,则可以将该值设置为所需的任何值(不仅有效身份标识).与上述用法相同,只是无需调用ToString().

As alternative you may even use Name property for this, each control is named (primary for designer-code integration) but if you create controls at run-time you can set that value to anything you want (not only valid identifiers). Same usage as above just no need to call ToString().

如何改进?

请让我说这种解决方案不是非常好的面向对象.即使不进行大量重构,我们也可以做得更好.请注意,您可以将任何内容存储在Tag属性中.一个数字,一个与文件名无关的简单字符串,或者甚至(可能更好)是一个classenum代表该图像(将动作委派给该对象).这是一个非常原始的示例:

Please let me say this solution is not very OOP. Even without a big refactoring we can do little bit better. Note that you can store whatever you want in Tag property. A number, a simple string unrelated to file name or even (maybe better) a class or an enum that represents that image (to delegate action to that object). This is a very raw example:

abstract class Tile {
    public abstract void Activate();
}

sealed class EmptyTile : Tile {
    public virtual void Activate() {
        Debug.WriteLine("Pecked a black square");
    }
}

sealed class ImageTile : Tile {
    public ImageTile(string content) {
        _content = content;
    }

    public virtual void Activate() {
        Debug.WriteLine(_content);
    }

    private string _content;
}

通过这种方式,您可以在点击事件处理程序中执行以下操作:

In this way in your click event handler you can do this:

((Tile)peckedTile.Tag).Activate();

无需检查内部内容或与null进行比较.没有if也没有switch,只是在创建图块时不要忘记放置适当的对象(ImageTileBlackTile).

No need to check what's inside or to compare with null. No if and no switch, just don't forget to put proper object (ImageTile or BlackTile) when you create tiles.

这篇关于如何使用C#在Visual Studio中访问图片框中的图像名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆