pictureBox1.Image即使在PictureBox上绘制后也为null

时间:2018-11-17 18:50:09

标签: c# graphics paint picturebox

我正在PictureBox控件上绘制带有小图像的网格。
按下Button时,我需要更新网格上的小图像位置,然后在其他位置再次绘制。

我是第一次绘画:

Bitmap ime = new Bitmap(Properties.Resources.ime);
Image imge= ime;
Graphics g = e.Graphics;

using (Pen pen = new Pen(Color.Black, 2))
{
    int rows = matrix.GetUpperBound(0) + 1 - matrix.GetLowerBound(0); // = 3, this value is not used
    int columns = matrix.GetUpperBound(1) + 1 - matrix.GetLowerBound(1); // = 4

    for (int index = 0; index < matrix.Length; index++)
    {
        int i = index / columns;
        int j = index % columns;

        if (matrix[i, j] == 0)
        {
            Rectangle rect = new Rectangle(new Point(5 + step * j, 5 + step * i), new Size(width, height));
            g.DrawRectangle(pen, rect);
            g.FillRectangle(Brushes.Black, rect);
        }
    }
        Rectangle rect1 = new Rectangle(new Point(5 + step * 10, 5 + step * 10), new Size(width, height));
        g.DrawImage(imge, rect1);                
}

第二次,当更新PictureBox时,我正在使用:

using (var g = Graphics.FromImage(matrixPictureBox.Image))

但我收到错误消息,说matrixPictureBox.Image is null

有人知道这个问题吗?

0 个答案:

没有答案