-1

I have a grid already drawn to a picturebox and I wish to draw a rectangle in the cell the mouse is clicked in. I'm trying to do this by drawing a rectangle in the top left corner of the cell and having it fill the entire cell. However when I click the grid disappears. How do I make it so the grid stays?

and is there an obvious better method for doing this?

The only way to redraw the grid is by pressing the button again but I want it to stay there. Thanks.

enter image description here

Idle_Mind
  • 38,363
  • 3
  • 29
  • 40
  • 3
    Because you used `Control.CreateGraphics()` instead of handling the Paint event of the Control. BTW, you need to post your code. – Jimi Nov 25 '20 at 17:19
  • 3
    Everything you want to have drawn must be drawn everytime in the paint event. Presummably you don't do that. But without code we can only guess. – Ralf Nov 25 '20 at 17:20
  • [See here](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?r=SearchResults&s=2|50.3469#27341797) – TaW Nov 25 '20 at 17:51

2 Answers2

0

You need to create a Graphics

Image bm;// Or Bitmap
            
using (var graphics = Graphics.FromImage(bm))
{
    Draw(rects, graphics);              
}

private static void Draw(List<Rectangle> rectangles, Graphics graphics)
{
    Pen redPen = new Pen(Color.Red, 1);
    foreach (var rect in rectangles)
    {
        graphics.DrawRectangle(redPen, rect);
    }
}
Stanislav
  • 459
  • 3
  • 6
0

At every mouse click in the mouse_click event at first you dispose the image of the picture box and then again assign the bitmap to the picturebox image otherwise you might face out of memory exception if you click on the grid too many times

private void InitializePictureBoxImage(PictureBox pictureBox)
{
    if(pictureBox.Image != null)
    {
          var image = pictureBox.Image;  // cache it to dispose
          pictureBox.Image = null;  // don't dispose an used object
          image.Dispose();   // and dispose of it
    }

    Bitmap bitmap = new Bitmap(pictureBox.Width, pictureBox.Height);
    pictureBox.Image = bitmap;  // assign bitmap to image
}
    

Then call the method in the mouse_click event which you used to draw the picture box when you press the Generate Grid button in your form. Afterwards use graphics inside the mouse_click event to colour the grid which you pressed taking the X-Y coordinate from MouseEventArgs.

Altogether I think it will be something like this

private void Mouse_ClickEvent(object sender, MouseEventArgs e)
{
    InitializePictureBoxImage(pictureBox);
    DrawThePictureBoxImage(pictureBox);

    using (Graphics graphics = Graphics.FromImage(pictureBox.Image))
    {
       Color color = Color.Blue;
       color = Color.FromArgb(150, color.R, color.G, color.B);  // lower the opacity so the writing in the grid is visible
       var brush = new SolidBrush(color);
       
       // Calculate the starting X-Y coordinate and Width,Height of the grid to color

       graphics.FillRectangle(brush, startX, startY,
                    width, height);
    }