0

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?

Jimi
  • 29,621
  • 8
  • 43
  • 61
GrandTim
  • 47
  • 7
  • PBox has two bitmaps (Image and BackgroundImage ) and a surface. You set the bitmaps and you can draw __onto__ the surface, best in the `Paint` event (or __into__ the bitmaps. ) - See [here](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=2|20.8251#27341797) for the difference! Drawing onto the surface will not in any way affect the bitmap let alone create them.. – TaW Nov 17 '18 at 18:52
  • Have you ever set a Bitmap as the `PicturBox.Image` property? Also, could you show the complete code that performs this drawing? – Jimi Nov 17 '18 at 18:52
  • i have updated with the code that draws. – GrandTim Nov 17 '18 at 18:56
  • After reading my comment do you have any more questions? – TaW Nov 17 '18 at 18:58
  • yes,I tried now to draw on the image, did not succeed... like `using (Graphics g = Graphics.FromImage(matrixPictureBox.Image))`, because my `matrixPictureBox.Image` is null – GrandTim Nov 17 '18 at 19:03
  • Of course you first need to create a Bitmap:`Size sz = pbox.Clientsize; Bitmap bmp = new Bitmap(sz.Width, sz.Height)`. After each drawing into it you need set it again as pbox.Image. - But you may instead chose to draw the original image at varying positions! – TaW Nov 17 '18 at 19:14
  • you are talking about `pBox.DrawToBitmap(bmp, matrixPictureBox.ClientRectangle); ` ? – GrandTim Nov 17 '18 at 19:26
  • No. That is meant to make a PictureBox draw all its content, ie both bitmaps and its surface drawings into one new bitmap. Probably not at all what you want. Of course you didn't tell us much about what you are actually trying to achieve. – TaW Nov 17 '18 at 19:54
  • I have a grid which is kind of a labirynth, On 1st cell a have a warrior, and I want to draw the warrior to move through the labirynth till the end cell, in this way to see how he travels. – GrandTim Nov 17 '18 at 19:56
  • So what you want is this: After changing the position you call pbox.Invalidate(); This will trigger the Paint event and the stuff will be drawn with the new position etc.. – TaW Nov 17 '18 at 19:57
  • Ok. So, for now, I have a function, DrawToImage, where I am drawing my first bitmap on the pictureBox. Then I have another function which I am calling every time I want my warrior to move, this function is taking the pBox image and draws on it. Well, it works, but I am seeing the full warrior path, ie when he finds the exit cell, I have on the pBox the whole path drawn. How I cannot have this ? I mean to not save the warrior position every time on the pbox, just visual to see that he is moving – GrandTim Nov 17 '18 at 20:01
  • That's why in your case you should not draw into the image but onto the control. This way only the last drawing it visible. Do it all in the Paint event or call your draw function from there, passing the e.Graphics obejct to the function! – TaW Nov 17 '18 at 21:29
  • yes, I was drawing into the control from the beginning, but how to redraw again ? I am using `using (var g = Graphics.FromImage(matrixPictureBox.Image))` but the image is `null` – GrandTim Nov 19 '18 at 16:17
  • When using the `e.Graphics` object, you perform your drawings on a Control's surface. The Control's `Image` property, if any, is not involved. Then: 1) You are not showing where/when these drawing are performed. 2) *and second time, when updating the pictureBox* <- this statement creates a lot of doubts on what is actually happening. Show your complete (and testable) code. -> nobody knows what `matrix` is (one could infer it by the methods used, but why should one have to do it, it's your duty to make things as clear as possible). – Jimi Nov 20 '18 at 01:03

0 Answers0