在图片框中显示拆分数组

时间:2018-03-10 07:02:09

标签: c# image display

我搜索了大多数文章,并获得了如何在图片框中显示的基本概念。我有一个卡片图像的文件名数组以及卡片的相关值。我需要在2个图片框中以随机顺序显示卡片。我的洗牌正确显示正确的值,两个图片框显示相同的图像! 所以我把数组分成2个阵列,现在已经迷失了自己如何在图片框中显示那些......整个部分现在都是红色的线条。任何建议? 以下是我的完整代码

namespace lab4
{
    public partial class Form1 : Form
    {
        string path = @"C:\Users\abura\Downloads\cardimages\";
        Image [] images = new Image[52];
        int[] cardValues = new int[52];
        int[] shuffledCards = new int[52];
        int computerCard = 0;
        int playerCard = 0;
        int[] values = { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
        string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10",
                "jack", "queen", "king", "ace" };
        string[] suits = { "diamonds", "hearts", "spades", "clubs" };
             int currentIndex = 0;

        public Form1()
        {
            InitializeComponent();
            // Setting Card Names
                for (int i = 0; i < ranks.Length; i++)
                {
                    for (int j = 0; j < suits.Length; j++)
                    {
                        string filename = $"{path}{ranks[i]}_of_{suits[j]}.png";

                        images[currentIndex] = Image.FromFile(filename);
                        cardValues[currentIndex] = values[i];
                    }
                }
            //shuffle
            Random random = new Random();
            for (int k=currentIndex; k>0; k--)
            { int l = random.Next(k);
                int m = shuffledCards[k];
                shuffledCards[k] = shuffledCards[k - 1];
                shuffledCards[k - 1] = m;
            }
            //set into 2 decks
            var firstHalf=shuffledCards.Take(shuffledCards.Length/2).ToArray();
            var secondHalf = shuffledCards.Skip(shuffledCards.Length / 2).ToArray();

//here is where I redline!!
            Image[] playerCard = firstHalf;
            Image[] computerCard = secondHalf;

            pictureBox1.Image = images[computerCard];
            pictureBox1.SizeMode = PictureBoxSizeMode.Zoom;
            textBox1.Text = cardValues[computerCard].ToString();

            computerCard++;
            pictureBox2.Image = images[playerCard];
            pictureBox2.SizeMode = PictureBoxSizeMode.Zoom;
            textBox2.Text = cardValues[playerCard].ToString();

           playerCard++;
        }

0 个答案:

没有答案