Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C#
  4. Why do my graphics still flicker?

Why do my graphics still flicker?

Scheduled Pinned Locked Moved C#
graphicsdata-structuresquestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    adonisv
    wrote on last edited by
    #1

    Why won't this double buffer? :sigh: I'm drawing this graph that allows you to either move value sliders or input values and redraw the graph. I've looked at tons of double buffering examples and they all seem to be doing the same or similar type of code. But my graph still flickers. BAH! :(( [code]public void DrawGraph() { this.UpdateGraphVariables(); Point[] pointarray=new Point[5]; Pen newpen = new Pen(Color.Black); this.m_bmImageOffScreen = new Bitmap(this.Width, this.Height); this.m_gOffScreen = Graphics.FromImage(this.m_bmImageOffScreen); this.m_gBuffer=Graphics.FromHwnd(this.Handle); //computational stuff //not interesting I do all my drawing on the bitmap... //Set point of origin to be 50,50 on the form this.m_gOffScreen.TranslateTransform(50,130); this.m_gOffScreen.DrawLine(newpen,1,1,1,150); this.m_gOffScreen.DrawLine(newpen,1,150,150,150); //Draw X and y axis this.m_gOffScreen.DrawLine(newpen,-3,30,3,30); this.m_gOffScreen.DrawLine(newpen,-3,60,3,60); this.m_gOffScreen.DrawLine(newpen,-3,90,3,90); ... //Join the points pointarray[0]=new Point(30,Convert.ToInt32(a)); pointarray[1]=new Point(60,Convert.ToInt32(b)); pointarray[2]=new Point(90,Convert.ToInt32(c)); pointarray[3]=new Point(120,Convert.ToInt32(d)); pointarray[4]=new Point(150,Convert.ToInt32(f)); this.m_gOffScreen.DrawLines(new Pen(Color.Yellow),pointarray); Then I attempt to double buffer! :doh: if (this.m_bmOldBitmap != null) { this.m_bmOldBitmap.Dispose(); } this.m_bmOldBitmap = this.m_bmImageOffScreen; this.m_gBuffer.Clear(this.BackColor); this.m_gBuffer.DrawImage(this.m_bmOldBitmap,30,50); }[/code] It still flickers! X|

    R B A 3 Replies Last reply
    0
    • A adonisv

      Why won't this double buffer? :sigh: I'm drawing this graph that allows you to either move value sliders or input values and redraw the graph. I've looked at tons of double buffering examples and they all seem to be doing the same or similar type of code. But my graph still flickers. BAH! :(( [code]public void DrawGraph() { this.UpdateGraphVariables(); Point[] pointarray=new Point[5]; Pen newpen = new Pen(Color.Black); this.m_bmImageOffScreen = new Bitmap(this.Width, this.Height); this.m_gOffScreen = Graphics.FromImage(this.m_bmImageOffScreen); this.m_gBuffer=Graphics.FromHwnd(this.Handle); //computational stuff //not interesting I do all my drawing on the bitmap... //Set point of origin to be 50,50 on the form this.m_gOffScreen.TranslateTransform(50,130); this.m_gOffScreen.DrawLine(newpen,1,1,1,150); this.m_gOffScreen.DrawLine(newpen,1,150,150,150); //Draw X and y axis this.m_gOffScreen.DrawLine(newpen,-3,30,3,30); this.m_gOffScreen.DrawLine(newpen,-3,60,3,60); this.m_gOffScreen.DrawLine(newpen,-3,90,3,90); ... //Join the points pointarray[0]=new Point(30,Convert.ToInt32(a)); pointarray[1]=new Point(60,Convert.ToInt32(b)); pointarray[2]=new Point(90,Convert.ToInt32(c)); pointarray[3]=new Point(120,Convert.ToInt32(d)); pointarray[4]=new Point(150,Convert.ToInt32(f)); this.m_gOffScreen.DrawLines(new Pen(Color.Yellow),pointarray); Then I attempt to double buffer! :doh: if (this.m_bmOldBitmap != null) { this.m_bmOldBitmap.Dispose(); } this.m_bmOldBitmap = this.m_bmImageOffScreen; this.m_gBuffer.Clear(this.BackColor); this.m_gBuffer.DrawImage(this.m_bmOldBitmap,30,50); }[/code] It still flickers! X|

      R Offline
      R Offline
      Roman Rodov
      wrote on last edited by
      #2

      In InitializeComponent put SetStyle(ControlStyles.DoubleBuffer);

      A 1 Reply Last reply
      0
      • R Roman Rodov

        In InitializeComponent put SetStyle(ControlStyles.DoubleBuffer);

        A Offline
        A Offline
        adonisv
        wrote on last edited by
        #3

        I've got that! SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.DoubleBuffer, true);

        H 1 Reply Last reply
        0
        • A adonisv

          Why won't this double buffer? :sigh: I'm drawing this graph that allows you to either move value sliders or input values and redraw the graph. I've looked at tons of double buffering examples and they all seem to be doing the same or similar type of code. But my graph still flickers. BAH! :(( [code]public void DrawGraph() { this.UpdateGraphVariables(); Point[] pointarray=new Point[5]; Pen newpen = new Pen(Color.Black); this.m_bmImageOffScreen = new Bitmap(this.Width, this.Height); this.m_gOffScreen = Graphics.FromImage(this.m_bmImageOffScreen); this.m_gBuffer=Graphics.FromHwnd(this.Handle); //computational stuff //not interesting I do all my drawing on the bitmap... //Set point of origin to be 50,50 on the form this.m_gOffScreen.TranslateTransform(50,130); this.m_gOffScreen.DrawLine(newpen,1,1,1,150); this.m_gOffScreen.DrawLine(newpen,1,150,150,150); //Draw X and y axis this.m_gOffScreen.DrawLine(newpen,-3,30,3,30); this.m_gOffScreen.DrawLine(newpen,-3,60,3,60); this.m_gOffScreen.DrawLine(newpen,-3,90,3,90); ... //Join the points pointarray[0]=new Point(30,Convert.ToInt32(a)); pointarray[1]=new Point(60,Convert.ToInt32(b)); pointarray[2]=new Point(90,Convert.ToInt32(c)); pointarray[3]=new Point(120,Convert.ToInt32(d)); pointarray[4]=new Point(150,Convert.ToInt32(f)); this.m_gOffScreen.DrawLines(new Pen(Color.Yellow),pointarray); Then I attempt to double buffer! :doh: if (this.m_bmOldBitmap != null) { this.m_bmOldBitmap.Dispose(); } this.m_bmOldBitmap = this.m_bmImageOffScreen; this.m_gBuffer.Clear(this.BackColor); this.m_gBuffer.DrawImage(this.m_bmOldBitmap,30,50); }[/code] It still flickers! X|

          B Offline
          B Offline
          blackthunder001
          wrote on last edited by
          #4

          Have you enabled double buffering in your form? Do this in your initialisation (I put it in my Form constructor after the IntializeComonent() call. this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles();

          A 1 Reply Last reply
          0
          • B blackthunder001

            Have you enabled double buffering in your form? Do this in your initialisation (I put it in my Form constructor after the IntializeComonent() call. this.SetStyle(ControlStyles.DoubleBuffer | ControlStyles.UserPaint | ControlStyles.AllPaintingInWmPaint, true); this.UpdateStyles();

            A Offline
            A Offline
            adonisv
            wrote on last edited by
            #5

            Nope, that didn't help. :sigh:

            1 Reply Last reply
            0
            • A adonisv

              I've got that! SetStyle(ControlStyles.UserPaint, true); SetStyle(ControlStyles.DoubleBuffer, true);

              H Offline
              H Offline
              Heath Stewart
              wrote on last edited by
              #6

              You also need ControlStyles.AllPaintingInWmPaint, which is documented in the ControlStyles enumeration documentation. Also, that enum is attributed with the FlagsAttribute, which means you can save your code some time and just use:

              SetStyle(ControlStyles.AllPaintingInWmPaint |
              ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true);

              This gets compiled as a constant value so SetStyle is only called once.

              Microsoft MVP, Visual C# My Articles

              A 1 Reply Last reply
              0
              • A adonisv

                Why won't this double buffer? :sigh: I'm drawing this graph that allows you to either move value sliders or input values and redraw the graph. I've looked at tons of double buffering examples and they all seem to be doing the same or similar type of code. But my graph still flickers. BAH! :(( [code]public void DrawGraph() { this.UpdateGraphVariables(); Point[] pointarray=new Point[5]; Pen newpen = new Pen(Color.Black); this.m_bmImageOffScreen = new Bitmap(this.Width, this.Height); this.m_gOffScreen = Graphics.FromImage(this.m_bmImageOffScreen); this.m_gBuffer=Graphics.FromHwnd(this.Handle); //computational stuff //not interesting I do all my drawing on the bitmap... //Set point of origin to be 50,50 on the form this.m_gOffScreen.TranslateTransform(50,130); this.m_gOffScreen.DrawLine(newpen,1,1,1,150); this.m_gOffScreen.DrawLine(newpen,1,150,150,150); //Draw X and y axis this.m_gOffScreen.DrawLine(newpen,-3,30,3,30); this.m_gOffScreen.DrawLine(newpen,-3,60,3,60); this.m_gOffScreen.DrawLine(newpen,-3,90,3,90); ... //Join the points pointarray[0]=new Point(30,Convert.ToInt32(a)); pointarray[1]=new Point(60,Convert.ToInt32(b)); pointarray[2]=new Point(90,Convert.ToInt32(c)); pointarray[3]=new Point(120,Convert.ToInt32(d)); pointarray[4]=new Point(150,Convert.ToInt32(f)); this.m_gOffScreen.DrawLines(new Pen(Color.Yellow),pointarray); Then I attempt to double buffer! :doh: if (this.m_bmOldBitmap != null) { this.m_bmOldBitmap.Dispose(); } this.m_bmOldBitmap = this.m_bmImageOffScreen; this.m_gBuffer.Clear(this.BackColor); this.m_gBuffer.DrawImage(this.m_bmOldBitmap,30,50); }[/code] It still flickers! X|

                A Offline
                A Offline
                Anonymous
                wrote on last edited by
                #7

                How do you call your DrawGraph( ) function?

                A 1 Reply Last reply
                0
                • H Heath Stewart

                  You also need ControlStyles.AllPaintingInWmPaint, which is documented in the ControlStyles enumeration documentation. Also, that enum is attributed with the FlagsAttribute, which means you can save your code some time and just use:

                  SetStyle(ControlStyles.AllPaintingInWmPaint |
                  ControlStyles.DoubleBuffer | ControlStyles.UserPaint, true);

                  This gets compiled as a constant value so SetStyle is only called once.

                  Microsoft MVP, Visual C# My Articles

                  A Offline
                  A Offline
                  adonisv
                  wrote on last edited by
                  #8

                  Do I do one or the other or do I set the styles AND do the manual double buffering witht the bitmaps and the two graphics objects?

                  1 Reply Last reply
                  0
                  • A Anonymous

                    How do you call your DrawGraph( ) function?

                    A Offline
                    A Offline
                    adonisv
                    wrote on last edited by
                    #9

                    The DrawGraph() function is called from OnPaint() Invalidate is called every time the draw graph button is clicked or a track bar is moved changing one of the values in the graph. protected override void OnPaint(PaintEventArgs e) { if(this.m_bmImageOffScreen==null) this.m_bmImageOffScreen=new Bitmap(this.ClientSize.Width,this.ClientSize.Height); this.m_gOffScreen =Graphics.FromImage(this.m_bmImageOffScreen); //this.m_gBuffer.Clear(this.BackColor); if(this.m_bgraphDrawn) this.DrawGraph(); else { this.textBox1.Text = "0"; this.textBox2.Text = "0"; this.textBox3.Text = "0"; this.textBox4.Text = "0"; this.textBox5.Text = "0"; this.m_bgraphDrawn = true; this.DrawGraph(); } this.m_gOffScreen.Dispose(); e.Graphics.DrawImageUnscaled(this.m_bmImageOffScreen,30,50); } private void trackBar5_Scroll(object sender, System.EventArgs e) { // Display the trackbar value in the text box. textBox5.Text = "" + trackBar5.Value; value5 += trackBar1.Value; //this.redrawChart(); //this.DrawGraph(); Invalidate(); }

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups