none
Drawing in c#

    Question

  • I have a graph drawn on long panel hold by another small panel. Then I need to have vertical line on this graph and move it through the graph. The problem is how to erase previously drawn line to move it to next location. Other way could be to redraw all but it causes to much flickering. then I have placed a small panel on this giving it shape of line, by changing the location of this panel I have created this effect but could not stop flicker. Is there any efficient way to do this all STOPING FLICKERING.


    abrar
    Friday, October 02, 2009 9:02 PM

Answers

  • This kind of flicker is trivially fixed by using the DoubleBuffered property.  You can use a PictureBox instead, it is double buffered by default.  If you need a Panel because you need scrolling, add a new class to your project and paste the code below.  Compile.  Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Windows.Forms;

    class BufferedPanel : Panel {
      public BufferedPanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
      }
    }

    Hans Passant.
    • Marked as answer by Bin-ze Zhao Wednesday, October 07, 2009 7:01 AM
    Friday, October 02, 2009 9:54 PM

All replies

  • Hm.  A panel is not exactly an animation object bustin out DirectX.  Your probly seeing the effects of the re-paint on the panel object.  If you don't want to go the animation route, perhaps you should use 2 panels and some trickery:


    1.  Start with whatever image you want and display Panel1.  Hide panel2 and make sure its the same location&dimension as panel1.
    2.  Whenever a Repaint event fires...draw it on the HIDDEN panel.
    3.  At the end of the Repaint of the HIDDEN panel set HIDDEN panel to visible and visible panel to hidden (use some properties to keep the state in order).
    4.  Now, you should draw the object on the now hidden panel to keep your stuff in sync.

    Rinse repeat
    :D
    Good coding involves knowing one's logical limits and expanding them as necessary.
    Friday, October 02, 2009 9:45 PM
  • This kind of flicker is trivially fixed by using the DoubleBuffered property.  You can use a PictureBox instead, it is double buffered by default.  If you need a Panel because you need scrolling, add a new class to your project and paste the code below.  Compile.  Drop the new control from the top of the toolbox onto your form.

    using System;
    using System.Windows.Forms;

    class BufferedPanel : Panel {
      public BufferedPanel() {
        this.DoubleBuffered = true;
        this.SetStyle(ControlStyles.ResizeRedraw, true);
      }
    }

    Hans Passant.
    • Marked as answer by Bin-ze Zhao Wednesday, October 07, 2009 7:01 AM
    Friday, October 02, 2009 9:54 PM
  • Hi,

         I am trying to create a GUI  where I am supposed to open an image and mark certain regions. Until now I have used split container but I experienced some flickering when I scroll the image. I am not sure if it occurs in other instances when I draw regions and delete them .But right now my concern is the flickering  along the edges as I scroll the image.

     

    2) Then I thought including a picturebox inside splitcontainer panel would be good. Luckily this removed the flicker while scrolling. The issue I had with picture box was that. If I deleted the graphic such as a polygon I had drawn earlier after which I called a method which redraws all the other polygons. But the other polygons are not visible unless I click on the scroll bar. Then I tried double buffering where in the event of deleting a polygon I drew the graphics onto the bitmap and loaded that image in "picturebox.Image = mybitmap". this worked perfect but I am not sure if I can use markers and move the polygons if I draw on to bitmap.

     

    3) My last resort was to remove all containers(toolstrip container, splitcontainer) and just have two panels and menustrip on the form and draw on one of the panels. But I hit the problem of flickering along the edges as I scroll. I tried the class example you had mentioned. It didnt seem to work in my case.

    Just to give a gist of my code.

    I call a method DrawImage() inside the paint event for panel. Please let me know how can I paste my code in format to this forum.

    void DrawImage()
            {
                if (gr != null)
                {
                    gr.Dispose();
                    //redBrush.Dispose();
                    //blackPen.Dispose();
                }
                gr = this.panel2.CreateGraphics();
                this.panel2.AutoScrollMinSize = new Size(myBitmap.Width, myBitmap.Height);
               
                gr.DrawImage(myBitmap, new RectangleF(this.panel2.AutoScrollPosition.X,   this.panel2.AutoScrollPosition.Y, myBitmap.Width, myBitmap.Height));

     

            }

     

    Thank you.

     

    Thursday, July 15, 2010 2:22 PM
  • Hello,

    I am using doublebuffered panel in my VB.NET project and I find it very useful. The only thing is that when I open the form in Designer I usually get an Errormessage that the doublebufferedpanel can not be found. It usually helps if I run the application once, then it will open up the design-view correctly and after that the Bufferedpanel will appear in Toolbox myProject Componets list, before that it will not show up there. Can you help me with this ?


    RD
    Friday, October 21, 2011 7:12 AM