0

I am struggling with getting the graphic to be redrawn with the invalidate method when the form goes the screen the graphic isn't redrawn i have tried to create an image from the graphics for it to been redrawn with the invalidate method but it doesn't work could somebody help edit the code for and do it in simple terms as i am still a beginner thanks would be much appreciated

private void squareButton_Click(object sender, EventArgs e)
        {

            // Declaring a new graphics object has been assigned null
            Graphics objGraphics = null;
            // This will create the picture graphics to be drawn in the picturebox
            objGraphics = PictureBox1.CreateGraphics();
            // This will redraw the picture box with a fill chosen after the systemcolors
            objGraphics.Clear(SystemColors.ControlDark);
            // This will draw the rectangle with a red pen 10,10 represent position and 50,50 reprsent the width and height 
            objGraphics.DrawRectangle(Pens.Red, 10, 10, 50, 50);
            // This will draw the rectangle
            objGraphics.Dispose();
Bitmap imgbit = (Bitmap)Picturebox.Image;
Graphics.FromImage(imgbit);
picturebox.invalidate();


// This is not redrawing the graphic it just shows a blank form

        }
  • Why don't you use the Paint event? Your not drawing an Image with the graphics object so nothing exists there. – OneFineDay Oct 19 '13 at 18:00
  • So what does the graphics object do does it not edit the picturebox image ? i want to know how to force the graphic to be redrawn with the invalidate method but i am struggling –  Oct 19 '13 at 18:05
  • Invalidate calls the Paint event so what ever the e.Graphics object draws the pitcurebox will show, but it's not the Image. – OneFineDay Oct 19 '13 at 18:07
  • Is there anyway to store it as an image or is the only way to use the paint event –  Oct 19 '13 at 18:09
  • Yes, after you draw on the surface you can. – OneFineDay Oct 19 '13 at 18:15
  • Whats the best way you would do it ? –  Oct 19 '13 at 18:20
  • 1
    You asked this question before and got the advice to stop using CreateGraphics(). You didn't follow that advice. There's just not much point asking questions here if you don't use the help you get. – Hans Passant Oct 19 '13 at 18:26
  • I don't know other ways i've tried other ways I'm still stuck the help I'm getting isn't explaining it clearly as a beginner i dont know what to do –  Oct 19 '13 at 18:29
  • I wrote above to use the paint event - which exposes the `e.Graphics` object that does the painting. – OneFineDay Oct 19 '13 at 18:30
  • The picturebox draws itself. Don't mess with it.; Invalidate does not draw. It marks part of the control as needed for a redraw (which will then overwrite your changes). – usr Oct 19 '13 at 19:17

1 Answers1

0

Using the DrawToBitmap method.

Bitmap bmp = new Bitmap(pb.Width, pb.Height);
pb.DrawToBitmap(bmp, pb.ClientRectangle);
bmp.Save({your path});
bmp.Dispose();

You can use the CreateGraphics object when your not wanting a persistent drawing. If this was in a button click event it will draw on the surface, but it will not stay once the system wants to redraw.

using (Graphics g = pb.CreateGraphics)
{
g.FillRectangle(Brushes.Blue, new Rectangle(20, 20, 20, 20));
g.DrawRectangle(Pens.Red, new Rectangle(20, 20, 20, 20));
}
OneFineDay
  • 9,004
  • 3
  • 26
  • 37
  • Thats very helpful @DonA by save would that be the path to my hardrive its going to run when a button is clicked so would it be best to use the second one ? you say it won't stay when the system wants to be drawn so if i minimise it will it not redraw ? –  Oct 19 '13 at 18:42
  • Sorry for all these questions i'm just learning all these new things –  Oct 19 '13 at 18:43
  • Correct! If you use the `CreateGraphics` and not a Paint event. – OneFineDay Oct 19 '13 at 18:44
  • Thanks your very helpful being noob i just want to get the best and simplistic way to do it –  Oct 19 '13 at 18:46