-3

i have a windows form application and in a recursive function i draw some shapes in that form using "create graphics" method ,the function call itself many times and in each call the shape get more complete but when it finishes calling and return to event (button click event ) i have no shape,it is gone ,it is gone!i have tried every thing that came up to my mind,i have initialized the graphic object in click event or form load event but did not get the appropriate result,still i have no shape.

ali najimi
  • 23
  • 3
  • 1
    This is expected behavior, but without your code, I can't tell you how to fix it. You need to show how you draw. Probably the best thing to do is instead of drawing directly on the control, draw to a bitmap and use a picturebox or panel to display it. – Ron Beyer Apr 26 '15 at 16:05
  • Looks like you have to learn the basics about drawing in winforms. Many many places to do so.. Here is a little about [what a Grahpics object isn't](http://stackoverflow.com/questions/29772266/convert-graphics-object-to-bitmap/29777131#29777131) namely a container for graphics and here is an [example of more advanced drawing](http://stackoverflow.com/questions/28714411/update-a-drawing-without-deleting-the-previous-one/28716887?s=17|0.0487#28716887).. – TaW Apr 26 '15 at 16:07
  • See [here](http://stackoverflow.com/questions/29874732/bitmap-from-drawing-not-render-outside-of-load-form/29879685#29879685) for an example of what Gusman suggests! – TaW Apr 26 '15 at 17:08
  • @ron beyer thanks alot......can i have your email and email the code to you? – ali najimi Apr 26 '15 at 17:36

1 Answers1

0

Instead of drawing to your form directly use a bitmap as buffer, then set it as background to the form.

Whenever a form is drawn, each invalidated area (dirty areas which needs redraw) will be fully redrawn, so if you don't redraw your shape on each OnPaint call it will be lost.

As I said, to avoid that set the form's background picture to an empty bitmap and instead of drawing to the form just draw to your bitmap, it will act as a buffer for your drawings.

Gusman
  • 14,905
  • 2
  • 34
  • 50
  • i am not really familiar with painting in windows form ...could u give me an example? – ali najimi Apr 26 '15 at 17:44
  • `bmp = new System.Drawing.Bitmap(800, 1000); g = Graphics.FromImage(bmp); mypen = new System.Drawing.Pen(Brushes.Black); this.AutoScroll = true; List items=new List { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10,11,12,13,14,15,16 }; for (int i = 0; i – ali najimi Apr 26 '15 at 18:06
  • What is `draw` ? Try changing the call to `draw(g, items, items, 0, 10); ` and include the `Graphics g` in that method header and use it to draw in the method `draw` you seem to have! – TaW Apr 26 '15 at 19:52