I am writing a graphics editor in C # in WinForms. For drawing shapes using double buffering. Here is the function code:

private void DrawFigure( int x, int y) { Graphics g = PicPole.CreateGraphics(); BufferedGraphicsContext currentContext; BufferedGraphics myBuffer; currentContext = BufferedGraphicsManager.Current; myBuffer = currentContext.Allocate(g, this.DisplayRectangle); myBuffer.Graphics.FillEllipse(new SolidBrush(ob.Paintcolor), ob.X, ob.Y, x - ob.X, y - ob.Y); myBuffer.Render(g); } 

PicPole is a PictureBox. The problem is that each time you call this function (DrawFigure), the entire PictureBox is cleared and filled with black color (that is, only the current ellipse is displayed, and all previous ones are cleared) , but I need the image to not be cleared, but just to draw new ellipse. Tell me, please, how can this be fixed?

    1 answer 1

    Most likely, the problem is that every time you call DrawFigure, you create a new Graphics . Try moving the string g = PicPole.CreateGraphics () to the FormLoad event, pre-declaring g as a form class variable.