I am drawing on a PictureBox
control a grid with a small image on it.
When pressing a Button
, I need to update the small image position on the grid, drawing it again on a different position.
I am drawing first time with:
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);
}
and the second time, when updating the PictureBox, I am using:
using (var g = Graphics.FromImage(matrixPictureBox.Image))
but I am getting the error saying that matrixPictureBox.Image is null
Does anybody know the problem?