Problem with PictureBox flickering.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Ryan Chavez

    Problem with PictureBox flickering.

    Hello,

    I'm working on a project that basically draws lines onto a PictureBox control. Now I'm using a Timer with an interval of 100 to draw the information. Now my code looks something like this:

    Private Sub Draw()
    m_pPictBox.Refr esh()
    ' Draw etc...
    End Sub
    Private Sub m_pTimer_Tick(B yVal sender As Object, ByVal e As System.EventArg s) Handles m_pTimer.Tick
    Draw()
    End Sub
    Now, everytime the Draw function gets called, I see the drawing "flicker" (ie flash on/off) in the PictureBox. I've tried using m_pPictBoxGraph ics.Clear(Color .Gray) (or any other color) but it still causes it to flicker.
    Any suggestions would be most helpful. Thank you.
    Ryan
  • Armin Zingler

    #2
    Re: Problem with PictureBox flickering.

    "Ryan Chavez" <rtchavez@hotma il.com> schrieb[color=blue]
    > Hello,
    >
    > I'm working on a project that basically draws lines onto a PictureBox
    > control. Now I'm using a Timer with an interval of 100 to draw the
    > information. Now my code looks something like this:
    >
    > Private Sub Draw()
    > m_pPictBox.Refr esh()
    > ' Draw etc...[/color]

    Why do you call Refresh before drawing?
    [color=blue]
    > End Sub
    > Private Sub m_pTimer_Tick(B yVal sender As Object, ByVal e As
    > System.EventArg s) Handles m_pTimer.Tick
    > Draw()
    > End Sub
    > Now, everytime the Draw function gets called, I see the drawing
    > "flicker" (ie flash on/off) in the PictureBox. I've tried using
    > m_pPictBoxGraph ics.Clear(Color .Gray) (or any other color) but it
    > still causes it to flicker. Any suggestions would be most helpful.
    > Thank you.
    > Ryan[/color]

    You might consider inheriting from the picturebox and call the SetStyle
    method to enable double buffering:

    In the constructor:

    setstyle( _
    ControlStyles.A llPaintingInWmP aint _
    Or ControlStyles.D oubleBuffer _
    Or ControlStyles.U serPaint, _
    True _
    )

    I'd also suggest to override the OnPaintBackgrou nd and OnPaint methods and
    paint everything there. You can leave OnPaintBackgrou nd empty and also clear
    the background in OnPaint, so you have all the rendering in one place. In
    the Form hosting the new control, you can call it's Invalidate method
    whenever you want it to be repainted.

    Armin

    Comment

    • Ryan Chavez

      #3
      Re: Problem with PictureBox flickering.

      Thank you Armin. I created a class that inherited the PictureBox and did
      like you suggested with the SetStyle in the constructure and it worked
      really good. I still get a small amout of flicker but not nearly as much as
      before. I also don't use m_pPictureBox.R efresh() anymore since I went back
      to using m_pPictBoxGraph ics.Clear(m_pPi ctBoxGraphics.B ackColor). I'm gonna
      look into overridding the OnPaintBackgrou nd and OnPaint events to see if
      that will help. Thanks again.

      Ryan


      "Armin Zingler" <az.nospam@free net.de> wrote in message
      news:e8R19OqWFH A.3280@TK2MSFTN GP09.phx.gbl...[color=blue]
      > "Ryan Chavez" <rtchavez@hotma il.com> schrieb[color=green]
      >> Hello,
      >>
      >> I'm working on a project that basically draws lines onto a PictureBox
      >> control. Now I'm using a Timer with an interval of 100 to draw the
      >> information. Now my code looks something like this:
      >>
      >> Private Sub Draw()
      >> m_pPictBox.Refr esh()
      >> ' Draw etc...[/color]
      >
      > Why do you call Refresh before drawing?
      >[color=green]
      >> End Sub
      >> Private Sub m_pTimer_Tick(B yVal sender As Object, ByVal e As
      >> System.EventArg s) Handles m_pTimer.Tick
      >> Draw()
      >> End Sub
      >> Now, everytime the Draw function gets called, I see the drawing
      >> "flicker" (ie flash on/off) in the PictureBox. I've tried using
      >> m_pPictBoxGraph ics.Clear(Color .Gray) (or any other color) but it
      >> still causes it to flicker. Any suggestions would be most helpful.
      >> Thank you.
      >> Ryan[/color]
      >
      > You might consider inheriting from the picturebox and call the SetStyle
      > method to enable double buffering:
      >
      > In the constructor:
      >
      > setstyle( _
      > ControlStyles.A llPaintingInWmP aint _
      > Or ControlStyles.D oubleBuffer _
      > Or ControlStyles.U serPaint, _
      > True _
      > )
      >
      > I'd also suggest to override the OnPaintBackgrou nd and OnPaint methods and
      > paint everything there. You can leave OnPaintBackgrou nd empty and also
      > clear the background in OnPaint, so you have all the rendering in one
      > place. In the Form hosting the new control, you can call it's Invalidate
      > method whenever you want it to be repainted.
      >
      > Armin[/color]


      Comment

      • Sarika

        #4
        Re: Problem with PictureBox flickering.

        Hello,

        I am facing a similar problem. I have a MDI form with another form as the
        MDI Client. The client has a Panel control on which I draw stuff. I noticed
        that there is a lot of flikering when I draw anything.

        As per the earlier suggestion I added the following code to the MDI client's
        code. However the problem still persists.
        ///
        InitializeCompo nent()

        Me.SetStyle(Con trolStyles.Doub leBuffer Or _
        ControlStyles.A llPaintingInWmP aint Or _
        ControlStyles.U serPaint Or _
        ControlStyles.O paque, True)

        Me.UpdateStyles ()

        \\\

        Any suggestions??

        TIA

        Comment

        • Armin Zingler

          #5
          Re: Problem with PictureBox flickering.

          "Sarika" <Sarika@discuss ions.microsoft. com> schrieb[color=blue]
          > Hello,
          >
          > I am facing a similar problem. I have a MDI form with another form as
          > the MDI Client. The client has a Panel control on which I draw
          > stuff. I noticed that there is a lot of flikering when I draw
          > anything.
          >
          > As per the earlier suggestion I added the following code to the MDI
          > client's code. However the problem still persists.
          > ///
          > InitializeCompo nent()
          >
          > Me.SetStyle(Con trolStyles.Doub leBuffer Or _
          > ControlStyles.A llPaintingInWmP aint Or _
          > ControlStyles.U serPaint Or _
          > ControlStyles.O paque, True)
          >
          > Me.UpdateStyles ()
          >
          > \\\
          >
          > Any suggestions??
          >
          > TIA[/color]


          If the panel is flickering the style of the panel must be changed. As
          SetStyle is protected, it has to be done in a control inherited from Panel.


          Armin

          Comment

          • Sarika

            #6
            Re: Problem with PictureBox flickering.

            Hi Armin,

            Well I tried that too. I tried the SetStyle method for the Panel instead of
            the form and it did not work.

            However I am not sure I understand you when you said
            "As SetStyle is protected, it has to be done in a control inherited from
            Panel"

            I don't have any control that inherits from the Panel. The Panel acts as the
            drawing area and a container for a textbox


            Comment

            • Sarika

              #7
              Re: Problem with PictureBox flickering.

              Armin,

              In my earlier post I mentioned that I tred the double buffering w/ the
              Panel. Well I actually just tried with the form and did not realise it
              because of the syntax was for the form and hence it did not raise a flag.

              Anyways. So regarding your suggestion....

              I need to create a class which inherits the Panel class and set the
              properties there. I then use this class to create a Panel on which I will do
              my drawing.

              Did I get you right?

              Just an observation

              I draw lines, text, boxes and also drag and drop some images from my
              treeview control. I noticed that drawing the Lines causes NO flicker whereas
              drawing any other object or moving it causes some flicker.

              Maybe you will be able to shed some light on this.

              Thanks again for your reponse.


              Comment

              • Armin Zingler

                #8
                Re: Problem with PictureBox flickering.

                "Sarika" <Sarika@discuss ions.microsoft. com> schrieb[color=blue]
                > Armin,
                >
                > In my earlier post I mentioned that I tred the double buffering w/
                > the Panel. Well I actually just tried with the form and did not
                > realise it because of the syntax was for the form and hence it did
                > not raise a flag.
                >
                > Anyways. So regarding your suggestion....
                >
                > I need to create a class which inherits the Panel class and set the
                > properties there. I then use this class to create a Panel on which I
                > will do
                > my drawing.
                >
                > Did I get you right?[/color]

                I don't understand "I then use this class...". The class *is* a panel.
                Replace the current panel by an instance of the new class. Unfortunatelly,
                the IDE doesn't add it automatically to the toolbox like it does with
                Usercontrols.
                [color=blue]
                > Just an observation
                >
                > I draw lines, text, boxes and also drag and drop some images from my
                > treeview control. I noticed that drawing the Lines causes NO flicker
                > whereas
                > drawing any other object or moving it causes some flicker.
                >
                > Maybe you will be able to shed some light on this.
                >
                > Thanks again for your reponse.[/color]


                Do you draw inside the Panel's OnPaint method?



                Armin

                Comment

                • Sarika

                  #9
                  Re: Problem with PictureBox flickering.

                  Okay I think I understand what you are saying with the inheritance.

                  I call a DrawAll method inside the Panel's Paint event. This DrawAll method
                  in turn calls the ClassPaint method of each of the classes whose objects need
                  to be drawn on the screen.


                  Comment

                  Working...