c# - 无法更新图片框内的图像

标签 c# image updates picturebox dispose

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

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

我已经研究了一段时间,据我了解,这可能与 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;
            }
        }
    }
}

最佳答案

问题是您创建了新的图片框,但没有从控件中删除/更新现有的图片框。 首先,您需要找到要从控件中更新或删除的图片框。

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

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);
    }
}

您还可以使用 Dictionary包含(grid.Value - Color)对。

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();

关于c# - 无法更新图片框内的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36554310/

相关文章:

c# - 将 ComponentRegistration 向下转换为非泛型类型

ruby-on-rails - 如何使用 ruby​​ 将 base64 字符串保存为图像

python - 如何在 python 3 中获得 jpeg 分辨率,而不需要任何外来库?

python - 如何在 Windows 中离线更新整个 Anaconda Python 发行版?

c++ - 我有两个用 Visual Studio 构建的 Windows C++ 应用程序。现在我想开发一个更新应用程序来更新这些应用程序

c# - 找不到编译动态表达式所需的一种或多种类型

c# - 使用 OWIN 静态文件时配置客户端缓存

java - 将 Retrofit 与 Imgur 的 API 结合使用

angularjs - 使用 Angular JS 加载图像后更新 DIV 元素

java - 如何将 Java import 转换为 C# using 语句