Albert Laure
Albert Laure

Reputation: 1722

Drawing in panel paint renders incomplete objects

Good day every one i have this code to create a simple circle

 private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g;
            Pen myPen = new Pen(Color.Black, 1);
            Point sp = new Point(5, 5);//starting point sp
            Point ep = new Point(100, 100);//ending point ep
            g = this.CreateGraphics();//tells compiler that we are going to draw on this very form
            g.DrawLine(myPen, sp, ep);

            g = this.CreateGraphics();
            g.DrawEllipse(myPen, 0, 0, 90, 90);

        }

the problem is whe i run it i renders this kind of incomplete output

enter image description here

Can you tell me why this happens? thank you

Upvotes: 0

Views: 179

Answers (3)

Hans Passant
Hans Passant

Reputation: 942247

   g = this.CreateGraphics();

Never use CreateGraphics(), it is only suitable for game-style frame based drawing. The kind you do when you create a game where every frame is different. You compounded the problem by using this, that will create a Graphics object that renders to the form surface, not the panel. Which is clipped by the controls on the form, the panel in your case.

The Paint event already hands you a Graphics object that's prepared to draw to the control surface. Or to the double-buffering buffer that you get from setting the DoubleBuffered property to true, your next likely question. You should also dispose the drawing objects you use. Fix:

    private void panel1_Paint(object sender, PaintEventArgs e) {
        using (var myPen = new Pen(Color.Black, 1)) {
            e.Graphics.DrawLine(myPen, new Point(5, 5), new Point(100, 100));
            e.Graphics.DrawEllipse(myPen, 0, 0, 90, 90);
        }
    }

It can also be important to suppress an optimization that the Panel class uses. Made to act as a container control, it limits what it repaints when it gets resized to only the part of the control that got revealed. That can produce unpleasant smear-like effects when what you paint also depends on the size of the panel. Suppress this optimization by setting its ResizeRedraw property to true in the constructor, deriving your own class from Panel is necessary.

Upvotes: 4

Tauseef
Tauseef

Reputation: 2052

g = this.CreateGraphics();//tells compiler that we are going to draw on this very form

You are drawing it on the form, not in panel. either draw it in panel of hide the panel.

g = this.pnlName.CreateGraphics();

To draw inside panel, see Hans Passant answer. Since this is acccepted answer, I am adding that code here too

 e.Graphics.DrawEllipse(myPen, 0, 0, 90, 90);

Upvotes: 4

user2509901
user2509901

Reputation:

You can either delete the panel and use your code or paint it on the panel instead.

private void panel1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g;
        Pen myPen = new Pen(Color.Black, 1);
        Point sp = new Point(5, 5);//starting point sp
        Point ep = new Point(100, 100);//ending point ep
        //Paint it on panel
        g = panel1.CreateGraphics();//tells compiler that we are going to draw on this very form
        g.DrawLine(myPen, sp, ep);

        //Paint it on panel
        g = panel1.CreateGraphics();
        g.DrawEllipse(myPen, 0, 0, 90, 90);
    }

Upvotes: 2

Related Questions