84

I have a program that is essentially like a paint application. However, my program has some flickering issues. I have the following line in my code (which should get rid of flickering - but doesn't):

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

my code(minus the super and sub classes for the shapes is as follows:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Paint
{
    public partial class Paint : Form
    {
        private Point startPoint;
        private Point endPoint;
        private Rectangle rect = new Rectangle();
        private Int32 brushThickness = 0;
        private Boolean drawSPaint = false;
        private List<Shapes> listOfShapes = new List<Shapes>();
        private Color currentColor;
        private Color currentBoarderColor;
        private Boolean IsShapeRectangle = false;
        private Boolean IsShapeCircle = false;
        private Boolean IsShapeLine = false;

        public SPaint()
        {

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

            currentColor = Color.Red;
            currentBoarderColor = Color.DodgerBlue;
            IsShapeRectangle = true; 
        }

        private void panelArea_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = panelArea.CreateGraphics();

            if (drawSPaint == true)
            {

                Pen p = new Pen(Color.Blue);
                p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

                if (IsShapeRectangle == true)
                {
                    g.DrawRectangle(p, rect);
                }
                else if (IsShapeCircle == true)
                {
                    g.DrawEllipse(p, rect);
                }
                else if (IsShapeLine == true)
                {
                    g.DrawLine(p, startPoint, endPoint);
                }
            }
            foreach (Shapes shape in listOfShapes)
            {

                shape.Draw(g);

            }
        }

        private void panelArea_MouseDown(object sender, MouseEventArgs e)
        {

            startPoint.X = e.X;
            startPoint.Y = e.Y;

            drawSPaint = true;
        }

        private void panelArea_MouseMove(object sender, MouseEventArgs e)
        {


            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {

                if (e.X > startPoint.X)
                {
                    rect.X = startPoint.X;
                    rect.Width = e.X - startPoint.X;
                }
                else
                {
                    rect.X = e.X;
                    rect.Width = startPoint.X - e.X;
                }
                if (e.Y > startPoint.Y)
                {
                    rect.Y = startPoint.Y;
                    rect.Height = e.Y - startPoint.Y;
                }
                else
                {
                    rect.Y = e.Y;
                    rect.Height = startPoint.Y - e.Y;
                }


                panelArea.Invalidate();

            }

        }

        private void panelArea_MouseUp(object sender, MouseEventArgs e)
        {

            endPoint.X = e.X;
            endPoint.Y = e.Y;

            drawSPaint = false;

            if (rect.Width > 0 && rect.Height > 0)
            {
                if (IsShapeRectangle == true)
                {
                    listOfShapes.Add(new TheRectangles(rect, currentColor, currentBoarderColor, brushThickness));
                }
                else if (IsShapeCircle == true)
                {
                    listOfShapes.Add(new TheCircles(rect, currentColor, currentBoarderColor, brushThickness));
                }
                else if (IsShapeLine == true)
                {
                    listOfShapes.Add(new TheLines(startPoint, endPoint, currentColor, currentBoarderColor, brushThickness));
                }

                panelArea.Invalidate();
            }
        }


        private void rectangleToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IsShapeRectangle = true;
            IsShapeCircle = false;
            IsShapeLine = false; 
        }

        private void ellipseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IsShapeRectangle = false;
            IsShapeCircle = true;
            IsShapeLine = false; 
        }

        private void lineToolStripMenuItem_Click(object sender, EventArgs e)
        {
            IsShapeCircle = false;
            IsShapeRectangle = false;
            IsShapeLine = true; 
        }

        private void ThicknessLevel0_Click(object sender, EventArgs e)
        {
            brushThickness = 0; 
        }

        private void ThicknessLevel2_Click(object sender, EventArgs e)
        {
            brushThickness = 2; 
        }

        private void ThicknessLevel4_Click(object sender, EventArgs e)
        {
            brushThickness = 4; 
        }

        private void ThicknessLevel6_Click(object sender, EventArgs e)
        {
            brushThickness = 6; 
        }

        private void ThicknessLevel8_Click(object sender, EventArgs e)
        {
            brushThickness = 8; 
        }

        private void ThicknessLevel10_Click(object sender, EventArgs e)
        {
            brushThickness = 10; 
        }

        private void ThicknessLevel12_Click(object sender, EventArgs e)
        {
            brushThickness = 12; 
        }

        private void ThicknessLevel14_Click(object sender, EventArgs e)
        {
            brushThickness = 14; 
        }

        private void FillColour_Click(object sender, EventArgs e)
        {

            ColorDialog fillColourDialog = new ColorDialog();
            fillColourDialog.ShowDialog();
            currentColor = fillColourDialog.Color;
            panelArea.Invalidate(); 
        }

        private void button1_Click(object sender, EventArgs e)
        {

            ColorDialog fillColourDialog = new ColorDialog();
            fillColourDialog.ShowDialog();
            currentBoarderColor = fillColourDialog.Color;
            panelArea.Invalidate(); 
        }


    }
}

How do i stop the flickering?

*UPDATE:*This code actually works great when i'm drawing directly on the form. However, when i try to draw on the panel, flickering becomes an issue

BigBug
  • 6,202
  • 23
  • 87
  • 138
  • 8
    Have you also set `this.DoubleBuffered = true;` ? – Marc Gravell Nov 08 '11 at 06:19
  • 1
    @ Marc Gravell i just tried adding in this.DoubleBuffered = true; and it's still flickering like crazy :S – BigBug Nov 08 '11 at 06:21
  • Is panelArea full of controls? Invalidate works recursively and therefore might kick each child control in panelArea to repaint itself – Polity Nov 08 '11 at 06:34
  • @ Polity no, panelArea is just a panel i'm using to draw on. It has no controls though.. – BigBug Nov 08 '11 at 06:40
  • Have you set the ClipSiblings style on all your controls? It's possible the flickering is caused by multiple repaints on the controls (if they overlap). – Larry Osterman Nov 08 '11 at 06:57
  • @Larry Osterman Hmm, i just moved everything so nothing is overlapping in the slightest. There is still a lot of flickering going on though :S – BigBug Nov 08 '11 at 07:18

17 Answers17

83

For a "cleaner solution" and in order to keep using the base Panel, you could simply use Reflection to implement the double buffering, by adding this code to the form that holds the panels in which you want to draw in

    typeof(Panel).InvokeMember("DoubleBuffered", 
    BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic, 
    null, DrawingPanel, new object[] { true });

Where "DrawingPanel" is the name of the panel that you want to do the double buffering.

I know quite a lot of time has passed since the question was asked, but this might help somebody in the future.

viper
  • 974
  • 6
  • 13
  • 6
    @musefan I am so happy to have helped you :D I am glad that a solution posted 2 years later than the original question is still able to help people! That's why stack overflow is so good! :) – viper Jun 13 '13 at 12:58
  • 1
    It still flickers for me. Where do I add this code? PS. in this answer it should be said "you need 'using System.Reflection' at the top". – john k Jan 31 '14 at 16:15
  • 2
    You have to call this for the panel that you want to stop the flickering before you start using it. You could call it in the Load event of your main form (or the form that will have the panel). Where are you calling it? – viper Jan 31 '14 at 17:12
  • Thank you. I used this in a completely offensive linq ForEach on the biggest "container" panel on a form and it resolved a completely incessant flickering issue quite nicely – Will Feb 16 '14 at 01:00
  • I usually don't add comments, but its amazing how this was the perfect solution for me too! Many thanks viper, my 2 hours search finally ended with a non flickering paint method ^.^ – Rotary Heart Mar 08 '15 at 08:11
  • 3
    @RotaryHeart please keep in mind that you should not use this in the paint method as it will run every time the control gets repainted, and reflection is expensive. You only need to set this once per control. I am not sure if this is what you have done, from reading your comment it appeared like it might have been your solution. – viper Apr 06 '15 at 11:44
  • @viper - I'm getting a TargetException "Object does not match target type." I'm putting this in the winform Load Event right after I create the Panel at runtime. Do you have any ideas? I also tried this for a panel that I create at design time same exact exception error? what I'm I doing wrong? Using your exact line: typeof(Panel).InvokeMember("DoubleBuffered", BindingFlags.SetProperty | BindingFlags.Instance | BindingFlags.NonPublic, null, "panel1", new object[] { true }); – Neal Davis Oct 31 '17 at 00:17
  • @NealDavis probably the fact you're using "panel1" with quotes. Just use the variable name. Anyhow, are you sure you're using it on a Panel? Check the typeof(your var) to make sure – viper Oct 31 '17 at 16:06
  • this isn't working for me. Instead of flickering the clear() color I selected, it's flickering the default background color, and fills the screen with the default background color after it paints what I wanted it to. – shieldgenerator7 Jul 09 '19 at 02:53
  • Did not work for me. A matter of fact, my panel was just completely blank after putting this in my Form_Load. It appears the image data was getting erased after it was being drawn. When I used the horizontal scroll bars, I could see my image draw on the panel, but quickly erase. I took the code out and my image showed again. This appears to be the same problem that shieldgenerator7 had. I tried user3641393 solution and that worked perfectly. This is on a .NET 6 Winforms application. – Scott Forbes Jan 17 '23 at 16:04
69

Finally solved the flickering. Since I was drawing on a panel instead of the form the line of code below will not solve the flickering:

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

SetStyle must be of type 'YourProject.YourProject' (or derived from it) hence, you have to create a class as such (so that you can use MyPanel which will be derived from SPaint.SPaint and hence allowing you to use doublebuffering directly for the panel - rather than the form):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SPaint; 

namespace YourProject
{
    public class MyPanel : System.Windows.Forms.Panel
    {
        public MyPanel()
        {
            this.SetStyle(
                System.Windows.Forms.ControlStyles.UserPaint | 
                System.Windows.Forms.ControlStyles.AllPaintingInWmPaint | 
                System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, 
                true);
        }
    }
}

After you've done this(although you should really never edit the designer code unless you truly know what you're doing) you'll have to edit the Form.Designer.cs. Inside this file you will find code that looks like this:

this.panelArea = new YourProject.MyPanel();

The above line needs to be changed to:

this.panelArea = new MyPanel(); 

After I completed these steps, my paint program no longer flickers.

For anyone else having the same issue, the problem is finally solved.

Enjoy!

Masoud
  • 8,020
  • 12
  • 62
  • 123
BigBug
  • 6,202
  • 23
  • 87
  • 138
  • But it doesn't appear in the designer anymore. Anyway to fix it? – wingerse Jul 13 '15 at 19:49
  • @WingerSendon - to avoid designer issues, use a "code" solution, such as [viper's](http://stackoverflow.com/a/15815254/199364), that sets the double-buffering at runtime. – ToolmakerSteve Oct 09 '16 at 13:29
55

Copy and paste this into your project

protected override CreateParams CreateParams
{
    get
    {
        CreateParams handleParam = base.CreateParams;
        handleParam.ExStyle |= 0x02000000;   // WS_EX_COMPOSITED       
        return handleParam;
    }
}

This enables double-buffering for all controls from the form level down, otherwise double buffering needs to be individually enabled for each one... you may want to fine tune double bufferring after this because blanketed double buffering may give unwanted side effects.

M. Schena
  • 2,039
  • 1
  • 21
  • 29
user3641393
  • 551
  • 4
  • 2
16

I have had the same problem. I was never able to 100% rid myself of the flicker (see point 2), but I used this

protected override void OnPaint(PaintEventArgs e) {}

as well as

this.DoubleBuffered = true;

The main issue for flickering is making sure you

  1. paint things it the right order!
  2. make sure your draw function is < about 1/60th of a second

winforms invokes the OnPaint method each time the form needs to be redrawn. There are many ways it can be devalidated, including moving a mouse cursor over the form can sometimes invoke a redraw event.

And important note about OnPaint, is you don't start from scratch each time, you instead start from where you were, if you flood fill the background color, you are likely going to get flickering.

Finally your gfx object. Inside OnPaint you will need to recreate the graphics object, but ONLY if the screen size has changed. recreating the object is very expensive, and it needs to be disposed before it is recreated (garbage collection doesn't 100% handle it correctly or so says documentation). I created a class variable

protected Graphics gfx = null;

and then used it locally in OnPaint like so, but this was because I needed to use the gfx object in other locations in my class. Otherwise DO NOT DO THIS. If you are only painting in OnPaint, then please use e.Graphics!!

// clean up old graphics object
gfx.Dispose();

// recreate graphics object (dont use e.Graphics, because we need to use it 
// in other functions)
gfx = this.CreateGraphics();

Hope this helps.

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
ohmusama
  • 4,159
  • 5
  • 24
  • 44
  • this hasn't solved my issue :( .. this code actually works great when i'm drawing directly on the form. However, when i try to draw on the panel, flickering becomes an issue... – BigBug Nov 08 '11 at 06:44
  • @BlueMonster than try to Panel.DoubleBuffered = true; – Rosmarine Popcorn Nov 08 '11 at 08:10
  • I recommend making a child object of the panel class, inheriting from the panel, then override that child's `protected override void OnPaint(PaintEventArgs e) {}` The new object will show up on your list of items to drop into your form. Also you can hand edit the code to change the reference type, if you already have the panel created and don't have the easy option of just recreating it. – ohmusama Nov 13 '11 at 03:00
  • Using `e.Graphics` helps, along with `this.DoubleBuffered = true`. By using created `Graphics`, double buffer does not only increase the flickering effect but also make the `Control` (or `Form`) white during most of the time. – Meow Cat 2012 Oct 24 '19 at 03:30
4

I know this is really old question but maybe someone will find it useful.
I'd like to make little enhancement to viper's answer.

You can make simple extension to Panel class and hide setting property through reflection.

public static class MyExtensions {

    public static void SetDoubleBuffered(this Panel panel) {
        typeof(Panel).InvokeMember(
           "DoubleBuffered",
           BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty,
           null,
           panel,
           new object[] { true });
    }
}

If your Panel variable's name is myPanel you can just call
myPanel.SetDoubleBuffered();
and that's it. Code looks much cleaner.

thorgil
  • 49
  • 1
  • 5
4

Double buffering is not going to be of much help here I'm afraid. I ran into this a while ago and ended up adding a separate panel in a rather clumsy way but it worked for my application.

Make the original panel that you have ( panelArea ) a transparent area, and put it on top of a 2nd panel, which you call panelDraw for example. Make sure to have panelArea in front. I whipped this up and it got rid of the flickering, but left the shape that was being drawn smeared out so it's not a full solution either.

A transparent panel can be made by overriding some paint actions from the original panel:

public class ClearPanel : Panel
{
    public ClearPanel(){}

    protected override CreateParams CreateParams
    {
        get
        {
            CreateParams createParams = base.CreateParams;
            createParams.ExStyle |= 0x00000020;
            return createParams;
        }
    }

    protected override void OnPaintBackground(PaintEventArgs e){}
}

The idea is to handle drawing the temporary shape during the MouseMove event of the 'panelArea' and ONLY repaint the 'panelDraw' on the MouseUp Event.

// Use the panelDraw paint event to draw shapes that are done
void panelDraw_Paint(object sender, PaintEventArgs e)
{
    Graphics g = panelDraw.CreateGraphics();

    foreach (Rectangle shape in listOfShapes)
    {
        shape.Draw(g);
    }
}

// Use the panelArea_paint event to update the new shape-dragging...
private void panelArea_Paint(object sender, PaintEventArgs e)
{
    Graphics g = panelArea.CreateGraphics();

    if (drawSETPaint == true)
    {
        Pen p = new Pen(Color.Blue);
        p.DashStyle = System.Drawing.Drawing2D.DashStyle.Dash;

        if (IsShapeRectangle == true)
        {
            g.DrawRectangle(p, rect);
        }
        else if (IsShapeCircle == true)
        {
            g.DrawEllipse(p, rect);
        }
        else if (IsShapeLine == true)
        {
            g.DrawLine(p, startPoint, endPoint);
        }
    }
}

private void panelArea_MouseUp(object sender, MouseEventArgs e)
{

    endPoint.X = e.X;
    endPoint.Y = e.Y;

    drawSETPaint = false;

    if (rect.Width > 0 && rect.Height > 0)
    {
        if (IsShapeRectangle == true)
        {
            listOfShapes.Add(new TheRectangles(rect, currentColor, currentBoarderColor, brushThickness));
        }
        else if (IsShapeCircle == true)
        {
            listOfShapes.Add(new TheCircles(rect, currentColor, currentBoarderColor, brushThickness));
        }
        else if (IsShapeLine == true)
        {
            listOfShapes.Add(new TheLines(startPoint, endPoint, currentColor, currentBoarderColor, brushThickness));
        }

        panelArea.Invalidate();
    }

    panelDraw.Invalidate();
}
Tom
  • 539
  • 4
  • 7
3

In this condition you have to enable double buffer . Open current form and go to form properties and apply double buffer true; or you can also write this code .

this.DoubleBuffered = true;     

In form load.

3

It's a bit of and old question, but just to be complete: There is yet another solution, that worked for me where the double-buffering did not.

As it turns out Microsoft offers the BufferedGraphics class as a solution. Nice thing about this class is that it enables you to copy one Graphics object to another, so except from setting up a temporary Graphics object and eventually copying it to the final destination, one can use pretty much the same code that one would have done when the flickering should not have been a problem:

private void Indicator_Paint(object sender, PaintEventArgs e)
{
    Control pbIndicator = (Control)sender;
    Rectangle targetRect = pbIndicator.ClientRectangle;

    Image img = Bitmap.FromFile("bitmap.bmp");

    BufferedGraphicsContext ctx = new BufferedGraphicsContext();
    BufferedGraphics bg = ctx.Allocate(e.Graphics, targetRect);

    // Do the graphic stuff 
    bg.Graphics.Clear(this.BackColor);
    bg.Graphics.DrawImage(img, 0, 0);
    // etcetera

    bg.Render(e.Graphics);
    bg.Dispose();
    ctx.Dispose();
}

Downside of this solution that it might clutter your code. Furthermore I'm not sure whether it is a good idea to setup the context each time, or whether it would suffice to create one in advance and keep using that context.

For more information see https://learn.microsoft.com/en-us/dotnet/api/system.drawing.bufferedgraphicscontext?view=dotnet-plat-ext-3.1

3

I'd advise overriding OnPaintBackground and handling the background erase yourself. If you know you are painting the whole control you can just do nothing in OnPaintBackground (don't call the base method) and it will prevent the background colour being painted first

Matt
  • 2,984
  • 1
  • 24
  • 31
  • 1
    after re-reading your question, this probably won't help... other than to suggest maybe subclassing panel and allowing you to override OnPaint and OnPaintBackground, and do your drawing there... – Matt Nov 08 '11 at 06:48
2

if all of the above doesn't work you can always create your own double buffer link to Microsofts tutorial: https://learn.microsoft.com/en-us/dotnet/framework/winforms/advanced/how-to-reduce-graphics-flicker-with-double-buffering-for-forms-and-controls

hopes it works for you

1

here is the program of moving circle in .net, that doesn't flicker.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Threading;
namespace CircleMove
{
    /// <summary>
    /// Description of MainForm.
    /// </summary>
    public partial class MainForm : Form
    {
        int x=0,y=0;
        Thread t;

        public MainForm()
        {

            //
            // The InitializeComponent() call is required for Windows Forms designer support.
            //
            InitializeComponent();

            //
            // TODO: Add constructor code after the InitializeComponent() call.
            //
        }
        void MainFormPaint(object sender, PaintEventArgs e)
        {
            Graphics g=e.Graphics;
            Pen p=new Pen(Color.Orange);
            Brush b=new SolidBrush(Color.Red);
        //  g.FillRectangle(b,0,0,100,100);
            g.FillEllipse(b,x,y,100,100);
        }
        void MainFormLoad(object sender, EventArgs e)
        {
            t=new Thread(  new ThreadStart(

                ()=>{
                    while(true)
                    {
                        Thread.Sleep(10);
                        x++;y++;
                        this.Invoke(new Action(
                            ()=>{

                                this.Refresh();
                                this.Invalidate();
                                this.DoubleBuffered=true;
                                }
                                            )
                                        );
                    }
                    }
                                            )

                        );

            t.Start();
        }
    }
}
sanjay
  • 11
  • 1
0

If memory is tight (so you don't want the memory cost of double-buffering), one possible way to REDUCE, though not eliminate, flicker, is to set background color to the dominant color in your current scene.

Why this helps: flicker is a momentary flash of the background color, which the OS draws before drawing child controls or your custom drawing code. If that flash is a color that is closer to the final color to be displayed, it will be less noticeable.

If you are not sure what color to start with, start with 50% gray, because this is an average of black and white, so will be closer to most colors in your scene.

myFormOrControl.BackColor = Color.Gray;
ToolmakerSteve
  • 18,547
  • 14
  • 94
  • 196
0

Try to insert drawing logic in current form's

protected override void OnPaint(PaintEventArgs e)
{
    base.OnPaint(e);
}

method. In this case you should use parameter e to get Graphics object. Use e.Graphics property. Then you should invoke Invalidate() method for this form whenever form must be redrawn. PS: DoubleBuffered must be set to true.

DeeDee
  • 1
  • 1
  • 1
0

Drawing onto a Label instead of a Panel, solved the problem for me.

No need to use DoubleBuffering or anything either.

You can remove the text from the label, set AutoSize to false, then Dock it or set the Size and use it as for the Panel.

Best wishes,

  • Note: An ArgumentException is raised after Dispose() is called on the Graphics object from the PaintEventArgs. This happens when the Label is the first (highest order) Control in the parent. – ThrowNewRandomException May 31 '20 at 16:08
0

Quite late into the game... but for me WS_EX_COMPOSITED worked fine for a while and after some time of application start, layout started behaving very weird. I use TabControl and some TabPages on addition/removal started displaying titles of its siblings, so be careful with using it. Without WS_EX_COMPOSITED all worked fine, but I still had the flickering issue on new TabPage add.

None solution here really worked for me so I started looking at my code. I have a functionality where I create RichTextBox, then this RichTextBox is added to newly created TabPage and finally TabPage is added to TabControl. I decided to change order of that action - firstly I create TabPage, then add it to TabControl, and when TabPage is already added to TabControl, I add RichTextBox to it. When I did that, no more flickering and all works perfectly without any 'hacks'.

The moral of the story is that if nothing really works for you, try to (if possible and applicable in your case) play around with order in which you add controls.

prf
  • 75
  • 1
  • 11
-2

Can you try using a timer and boolean to check if mouse is down, and paint in that spot, using a variable again check if user has moved his mouse, if moved paint that spot too etc.

Or just check if mouse down(via boolean that sets true when mouse is down) using a timer and paint it considering you are probably trying to just paint one pixel, not like you have shadow etc. Instead of using actual mousedown. So you check every 1 second instead of 0.0001 and it wont flicker. Or vice-versa, try it with your own times.

  • 1
    Flicker occurs when the background color (typically white) of a form or control is drawn, and then your custom code runs to redraw the affected area. The "flicker" is a momentary flash of background color. Solving flicker requires double-buffering, so that the system has a place from which it can get the desired contents to restore, rather than drawing the background color. Putting your drawing code on a timer is worse than doing nothing (expending cpu cycles to no benefit). Rely on the OS to tell you when you need to draw, don't try to outsmart it. – ToolmakerSteve Oct 09 '16 at 13:23
-3

just do this.Refresh() when shown the form.

nick
  • 1,090
  • 1
  • 11
  • 24
Jam
  • 1