Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 显示数组中的图像,单击时显示图像的名称_C#_Arrays_Random_Bitmap - Fatal编程技术网

C# 显示数组中的图像,单击时显示图像的名称

C# 显示数组中的图像,单击时显示图像的名称,c#,arrays,random,bitmap,C#,Arrays,Random,Bitmap,作为先兆:我对C非常陌生,是的,这是家庭作业 我正在开发一个程序,该程序将52张card.bmp存储在一个数组中,随机化显示在5个图片框中的卡片,然后当用户单击卡片时,它会显示卡片的名称。我被这最后一部分难住了。提前谢谢 public partial class cardIdentifier : Form { public cardIdentifier() { InitializeComponent(); pictureBox1.SizeMode

作为先兆:我对C非常陌生,是的,这是家庭作业

我正在开发一个程序,该程序将52张card.bmp存储在一个数组中,随机化显示在5个图片框中的卡片,然后当用户单击卡片时,它会显示卡片的名称。我被这最后一部分难住了。提前谢谢

 public partial class cardIdentifier : Form
{
    public cardIdentifier()
    {
        InitializeComponent();
        pictureBox1.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox2.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox3.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox4.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox5.SizeMode = PictureBoxSizeMode.StretchImage;
        pictureBox1.Image = deck[new Random().Next(0, deck.Length)];
        pictureBox2.Image = deck[new Random(+1).Next(0, deck.Length)];
        pictureBox3.Image = deck[new Random(+5).Next(0, deck.Length)];
        pictureBox4.Image = deck[new Random(+10).Next(0, deck.Length)];
        pictureBox5.Image = deck[new Random(-7).Next(0, deck.Length)];
    }
    //Creating and adding images to the array
    Image[] deck = new Image[52] { Card_Identify.Properties.Resources.clubAce, Card_Identify.Properties.Resources.clubTwo, Card_Identify.Properties.Resources.clubThree, Card_Identify.Properties.Resources.clubFour, Card_Identify.Properties.Resources.clubFive , Card_Identify.Properties.Resources.clubSix , Card_Identify.Properties.Resources.clubSeven , Card_Identify.Properties.Resources.clubEight , Card_Identify.Properties.Resources.clubNine , Card_Identify.Properties.Resources.clubTen , Card_Identify.Properties.Resources.clubJack , Card_Identify.Properties.Resources.clubQueen , Card_Identify.Properties.Resources.clubKing , Card_Identify.Properties.Resources.spadeAce , Card_Identify.Properties.Resources.spadeTwo , Card_Identify.Properties.Resources.spadeThree , Card_Identify.Properties.Resources.spadeFour , Card_Identify.Properties.Resources.spadeFive , Card_Identify.Properties.Resources.spadeSix , Card_Identify.Properties.Resources.spadeSeven , Card_Identify.Properties.Resources.spadeEight , Card_Identify.Properties.Resources.spadeNine , Card_Identify.Properties.Resources.spadeTen , Card_Identify.Properties.Resources.spadeJack , Card_Identify.Properties.Resources.spadeQueen , Card_Identify.Properties.Resources.spadeKing , Card_Identify.Properties.Resources.heartAce , Card_Identify.Properties.Resources.heartTwo , Card_Identify.Properties.Resources.heartThree , Card_Identify.Properties.Resources.heartFour , Card_Identify.Properties.Resources.heartFive , Card_Identify.Properties.Resources.heartSix , Card_Identify.Properties.Resources.heartSeven , Card_Identify.Properties.Resources.heartEight , Card_Identify.Properties.Resources.heartNine , Card_Identify.Properties.Resources.heartTen , Card_Identify.Properties.Resources.heartJack , Card_Identify.Properties.Resources.heartQueen , Card_Identify.Properties.Resources.heartKing , Card_Identify.Properties.Resources.diamondAce , Card_Identify.Properties.Resources.diamondTwo , Card_Identify.Properties.Resources.diamondThree , Card_Identify.Properties.Resources.diamondFour , Card_Identify.Properties.Resources.diamondFive , Card_Identify.Properties.Resources.diamondSix , Card_Identify.Properties.Resources.diamondSeven , Card_Identify.Properties.Resources.diamondEight , Card_Identify.Properties.Resources.diamondNine , Card_Identify.Properties.Resources.diamondTen , Card_Identify.Properties.Resources.diamondJack , Card_Identify.Properties.Resources.diamondQueen , Card_Identify.Properties.Resources.diamondKing  };

//Changes the card shown in picture box
    private void button1_Click(object sender, EventArgs e)
    {
        pictureBox1.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button2_Click(object sender, EventArgs e)
    {
        pictureBox2.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button3_Click(object sender, EventArgs e)
    {
        pictureBox3.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button4_Click(object sender, EventArgs e)
    {
        pictureBox4.Image = deck[new Random().Next(0, deck.Length)];
    }

    private void button5_Click(object sender, EventArgs e)
    {
        pictureBox5.Image = deck[new Random().Next(0, deck.Length)];
    }


    public void pictureBoxes_Click(object sender, EventArgs e)
    {
        //THIS IS WHERE IM STUCK, NEED TO DISPLAY NAME OF CARD WHEN ITS CLICKED!
    }
}
}

在您的代码中,我没有看到关系位图/卡名

您可以执行以下操作,使用一个小类来定义卡:

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        private Card[] _cards;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // build your array of cards
            Card[] cards =
            {
                new Card
                {
                    Path = "ace_heart.png",
                    Name = "Ace Of Hearts"
                }
                // etc ...
            };

            // load their bitmaps
            foreach (var card in cards)
            {
                card.Bitmap = new Bitmap(card.Path);
            }

            // keep a reference
            _cards = cards;
        }

        private int GetRandomCardIndex(int seed)
        {
            var random = new Random(seed);
            var next = random.Next(52);
            return next;
        }

        private void Demo()
        {
            var cardIndex = GetRandomCardIndex(1234);
            var card = _cards[cardIndex];
            var cardName = card.Name; // there you are
            var cardBitmap = card.Bitmap;
        }
    }

    internal class Card
    {
        public Bitmap Bitmap;
        public string Name;
        public string Path;
    }
}
我看到您的卡已放入资源中,在我的示例中,这些是本地文件。最终可以从资源中获取卡片名称,但这可能超出了您的作业范围

要在项目中部署卡映像,请执行以下操作:

在项目中放置位图 全部选中并转到“属性”窗口 构建操作=内容 复制到输出目录=如果较新,则复制 或者只需在bin\Debug子目录中复制EXE旁边的所有位图
创建另一个类,如下所示:

public class Card
{
    public Image CardImage {get; set; }

    public string Name {get; set; }
}
var card5 = deck[new Random(-7).Next(0, deck.Length)];
pictureBox5.Image = card5.CardImage;
pictureBox5.Tag = card5; // Later on you will read this to get the name
public void pictureBoxes_Click(object sender, EventArgs e)
{
    //THIS IS WHERE IM STUCK, NEED TO DISPLAY NAME OF CARD WHEN ITS CLICKED!
    var cardName = ((sender as PictureBox).Tag as Card).Name
}
创建这些类的数组,而不是像这样创建图像数组。这将帮助您将卡的图像和名称保存在单个实例中,以便您以后可以获取:

Card[] deck = new Card[52] { new Card { CardImage = Card_Identify.Properties.Resources.clubAce, Name = "ClubAce" }, // ... the rest of your cards };
创建picturebox时,将Tag属性设置为卡的名称,如下所示:

public class Card
{
    public Image CardImage {get; set; }

    public string Name {get; set; }
}
var card5 = deck[new Random(-7).Next(0, deck.Length)];
pictureBox5.Image = card5.CardImage;
pictureBox5.Tag = card5; // Later on you will read this to get the name
public void pictureBoxes_Click(object sender, EventArgs e)
{
    //THIS IS WHERE IM STUCK, NEED TO DISPLAY NAME OF CARD WHEN ITS CLICKED!
    var cardName = ((sender as PictureBox).Tag as Card).Name
}
并更改单击方法,如下所示:

public class Card
{
    public Image CardImage {get; set; }

    public string Name {get; set; }
}
var card5 = deck[new Random(-7).Next(0, deck.Length)];
pictureBox5.Image = card5.CardImage;
pictureBox5.Tag = card5; // Later on you will read this to get the name
public void pictureBoxes_Click(object sender, EventArgs e)
{
    //THIS IS WHERE IM STUCK, NEED TO DISPLAY NAME OF CARD WHEN ITS CLICKED!
    var cardName = ((sender as PictureBox).Tag as Card).Name
}

添加了表单图片。您可以使用字典将字符串与图像配对,而不是使用类(这是最可扩展的解决方案)。这是一个单独构建的附加类,还是与代码的其余部分内联?我很抱歉,同样,更新到C和OOP是的,这个帮助程序类位于表单类之下,我没有麻烦将它移动到它自己的文件中。有100种方法可以满足你的需求,我只是采用了我认为最简单的方法。为什么@JacobZayak会这样做?他已经在资源文件中保存了这些图像,这实际上比这种方法要好。他只需要从卡片图像到卡片名称的映射…我理解这个解决方案,但在使用这个解决方案时,我创建随机卡片的语句似乎不正确。我知道这与阵列的构建方式有关,但我不知道如何修复它。代码pictureBox1.Image=deck[new Random.Next0,deck.Length];我得到错误CS0029无法隐式地将类型“Card\u identifier.Card”转换为“System.Drawing.Image”不,我的答案中不是这样的。请记住,现在您的阵列具有卡类型,因此对于每个picturebox,您需要按照我在回答中的方式执行:1。随机获取卡片:var card5=deck[new random-7.Next0,deck.Length];2.从卡中分配图像:pictureBox5.image=card5.CardImage;3.a标记签名:pictureBox5.tag=card5;//稍后,您将阅读这篇文章,以获得您需要的名称,以便对所有PictureBox这样做,但显然更改了随机数等。我要认真感谢您迄今为止的帮助。我修复了随机卡,我想,但是当我尝试向数组代码卡添加更多卡时,会出现错误。[卡片组=新卡[]{new Card{CardImage=Card_identification.Properties.Resources.clubAce,Name=Ace of Clubs},{CardImage=Card_identification.Properties.Resources.clubTwo,Name=Two of Clubs};我得到错误CS0623数组初始值设定项只能在变量或字段初始值设定项中使用。尝试使用一个新的表达式。你必须为你创建的每一张卡不断添加新的关键字。你忘了。这样做:Card[]deck=new Card[]{new Card{CardImage=Card_identification.Properties.Resources.clubAce,Name=Ace of Clubs},new Card{CardImage=Card_identification.Properties.Resources.clubTwo,Name=Two of Clubs};看看逗号之后,又有一个新的。所以,如果你正在制作52张卡片,你将有52条新闻。你是最棒的,非常感谢你,现在完全可以开始工作了。