向数组元素添加图像
本文关键字:图像 添加 数组元素 | 更新日期: 2023-09-27 18:13:31
我正在制作一个纸牌游戏,我已经创建了10张不同的纸牌,我已经将图片实现到右边资源区域的程序中。我的卡片结构是这样的:
public Form1()
{
cards = new PlayCard[10];
cards[0] = new PlayCard("Harry", 97, 33, 40);
cards[1] = new PlayCard("Dave", 80, 91, 41);
cards[2] = new PlayCard("Randle", 90, 13, 45);
cards[3] = new PlayCard("Zach", 30, 98, 81);
cards[4] = new PlayCard("Shorty", 30, 89, 99);
cards[5] = new PlayCard("Matt", 75, 81, 72);
cards[6] = new PlayCard("Aaron", 89, 82, 80);
cards[7] = new PlayCard("Ryan", 25, 83, 71);
cards[8] = new PlayCard("Stephen", 96, 100, 100);
cards[9] = new PlayCard("David", 90, 37, 70);
InitializeComponent();
}
我想知道我如何得到相应的图片显示确定哪张牌显示
谢谢
使用ImageList
。然后,您可以为每个图像命名以与您的名称相对应。这里很好地介绍了ImageLists以及如何在运行时和设计时添加图像。
首先添加图像(在运行时显示):
imageList1.Images.Add("pic1", Image.FromFile("c:''mypic.jpg"));
从集合中删除图像:
imageList1.Images.RemoveAt(listBox1.SelectedIndex);
imageList1.Images..RemoveByKey("pic1");
访问图像,从图像集合中获取图像:
panel1.BackgroundImage = imageList1.Images["David"];
或用于数组:
panel1.BackgroundImage = imageList1.Images[cards[i][0]];
我希望这对你有帮助。
编辑。以解决上述不是面向对象的评论。而不是使用ImageList
,你可以添加一个Image
到你的PlayCard
对象。还有一个选项是使用Dictionary<string, Image>
来处理这个映射,但这也可能被解释为非oop。
将图像作为属性添加到PlayCard类中,然后为每个PlayCard分配图像(最简单的方法-更改构造函数)。每个playcard都知道它的图像,因此在现有代码中实现它非常容易,无需进行许多修改。