无法更新图片框内的图片

Cannot update Image inside picturebox

我正在制作一个小型 Reversi/Othello 游戏,在我绘制棋盘的方法中,我有一个嵌套的 for 循环来显示 PictureBoxes 的网格,每个 PictureBoxes 中都有一个图像。

第一次调用该方法时,所有 PictureBoxes 均已正确创建并且图像已放置在其中。但是,如果我再次调用该方法,我似乎无法覆盖已经存在的图像。

我已经研究了一段时间,据我了解,这可能与 PictureBox 被锁定或需要在向其写入另一张图像之前正确 Dispose() PictureBox 有关。我无法使这些解决方案中的任何一个起作用,因此非常感谢任何具体的帮助!

private void Draw()
{
    Bitmap White = Properties.Resources.white;
    Bitmap Black = Properties.Resources.black;
    Bitmap None = Properties.Resources.none;

    for (int r = 0; r <= grid.GetUpperBound(0); r++)
    {
        for (int c = 0; c <= grid.GetUpperBound(0); c++)
        {
            if (grid[r, c].value == 1)
            {
                var picbox = new PictureBox()                // initialise picturebox for displaying images
                {
                    Name = grid[r, c].name,
                    Size = new Size(64, 64),
                    Location = new Point(r * 65 + 15, c * 65 + 60),
                    Text = grid[r, c].name,
                    Image = White
                };

                Controls.Add(picbox);                // add picturebox to form 
                picbox.Click += ClickBox;
                MessageBox.Show("white draw" + grid[r, c].name);
            }

            if (grid[r, c].value == -1)
            {
                var picbox = new PictureBox()
                {
                    Name = grid[r, c].name,
                    Size = new Size(64, 64),
                    Location = new Point(r * 65 + 15, c * 65 + 60),
                    Text = grid[r, c].name,
                    Image = Black
                 };

                Controls.Add(picbox);
                picbox.Click += ClickBox;
                MessageBox.Show("black draw" + grid[r, c].name);
            }

            if (grid[r, c].value == 0)
            {
                var picbox = new PictureBox()
                {
                    Name = grid[r, c].name,
                    Size = new Size(64, 64),
                    Location = new Point(r * 65 + 15, c * 65 + 60),
                    Text = grid[r, c].name,
                    Image = None
                };

                Controls.Add(picbox);
                picbox.Click += ClickBox;
            }
        }
    }
}

问题是您创建了新的图片框,但没有 remove/update 来自 Controls 的现有图片框。 首先,您需要从 Controls.

中找到要更新或删除的图片框

我建议创建一个图片框的网格,这样你就可以从网格中获取图片框。首先创建字段。

private var pictureGrid = new PictureBox[8, 8];

然后

if (grid[r, c].value == 1)
{
    if (pictureGrid[r,c] != null) 
    {
        pictureGrid[r,c].Image = White;
    } 
    else 
    {
        var picbox = new PictureBox()                
        {
            Name = grid[r, c].name,
            Size = new Size(64, 64),
            Location = new Point(r * 65 + 15, c * 65 + 60),
            Text = grid[r, c].name,
            Image = White
        };

        pictureGrid[r,c] = picbox; 
        Controls.Add(picbox);                
        picbox.Click += ClickBox;
        MessageBox.Show("white draw" + grid[r, c].name);
    }
}

您也可以使用包含(grid.Value - 颜色)对的 Dictionary

private Dictionary<int, Bitmap> colors = new  Dictionary<int, Bitmap>();

private void Load() 
{
    Bitmap White = Properties.Resources.white;
    Bitmap Black = Properties.Resources.black;
    Bitmap None = Properties.Resources.none;

    colors.Add(1, White);
    colors.Add(-1, Black);
    colors.Add(0, None);
}

所以你的方法看起来像

private var pictureGrid = new PictureBox[8, 8];    
private Dictionary<int, Bitmap> colors = new  Dictionary<int, Bitmap>();

private void Load() 
{
    Bitmap White = Properties.Resources.white;
    Bitmap Black = Properties.Resources.black;
    Bitmap None = Properties.Resources.none;

    colors.Add(1, White);
    colors.Add(-1, Black);
    colors.Add(0, None);
}

private void Draw()
{
    for (int r = 0; r <= grid.GetUpperBound(0); r++)
    {
        for (int c = 0; c <= grid.GetUpperBound(0); c++)
        {
            if (pictureGrid[r, c] != null) 
            {
                pictureGrid[r,c].Image = colors[grid[r,c]];
            }

            else 
            {
                var picbox = new PictureBox()
                {
                    Name = grid[r, c].name,
                    Size = new Size(64, 64),
                    Location = new Point(r * 65 + 15, c * 65 + 60),
                    Text = grid[r, c].name,
                    Image = colors[grid[r,c]]
                 };

                pictureGrid[r,c] = picbox;
                Controls.Add(picbox);
                picbox.Click += ClickBox;
                MessageBox.Show("black draw" + grid[r, c].name);

            }

        }
    }
}

也可以找到图片框用linq更新

var pictureToRemove = this.Controls.OfType<PictureBox>().Where(x => x.Location.X == r * 65 + 15 && x.Location.Y == c * 65 + 60).First();