Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix flicker in a WinForms form?

I am constantly drawing frames, and I need the form to not flicker. How do I accomplish this?

public partial class Form1 : Form
{
    Image[] dude = new Image[3];
    static int renderpoint = 0;
    int lastimage = 0;

    public Form1()
    {
        dude[1] = new Bitmap(@"snipe1.bmp");
        dude[0] = new Bitmap(@"snipe0.bmp");

        InitializeComponent();
    }

    private void Form1_Shown(object sender, EventArgs e)
    {
        MainLoop();
    }

    private void MainLoop()
    {
        double FPS = 10;

        long ticks1 = 0;
        long ticks2 = 0;
        double interval = (double)Stopwatch.Frequency / FPS;

        while (true)
        {
            ticks2 = Stopwatch.GetTimestamp();
            if (ticks2 >= ticks1 + interval)
            {
                ticks1 = Stopwatch.GetTimestamp();

                MoveGraphics();
                this.Refresh(); 
            }

            Thread.Sleep(1); 
        }
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        Graphics g = e.Graphics;
        Rectangle rect = new Rectangle(renderpoint, 0, 100, 100);
        Color lowcolor = Color.FromArgb(0, 128, 64);
        Color highcolor = Color.FromArgb(0, 128, 64);

        ImageAttributes imageAttr = new ImageAttributes();
        imageAttr.SetColorKey(lowcolor, highcolor);

        if (lastimage == 1)
        {
            lastimage = 0;
            g.DrawImage(dude[1], rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr);
        }
        else
        {
            lastimage = 1;
            g.DrawImage(dude[0], rect, 0, 0, 100, 100, GraphicsUnit.Pixel, imageAttr);
        }  
    }

    void MoveGraphics()
    {
        if (renderpoint > 950)
        {
            renderpoint = 0;
        }
        else
        {
            renderpoint += 10;
        }
    }
}

There's the current code. Suggestions?

like image 700
Bloodyaugust Avatar asked Mar 02 '10 01:03

Bloodyaugust


People also ask

How do I stop flickering in C#?

use control. DoubleBuffered property. set it to true. DoubleBuffered allows Control redraws its surface using secondary buffer to avoid flickering.

Is WinForms dying?

Win Form has been used to develop many applications. Because of its high age (born in 2003), WinForm was officially declared dead by Microsoft in 2014. However, Win Form is still alive and well.

Which property of a control is used to reduce flickering when it is drawn?

Double buffering is a technique that attempts to reduce flicker by doing all the drawing operations on an off-screen canvas, and then exposing this canvas all at once. To turn on a control's double buffering, you need to set three styles.

Will WinForms be deprecated?

WinForms won't be deprecated until Win32 is ... which could be quite sometime! WPF on the other hand has few direct dependencies on Win32 so could potentially form the basis of a "fresh start" UI layer on a future version of windows.


2 Answers

Paste this into your Form1 constructor:

this.DoubleBuffered = true;
like image 133
Hans Passant Avatar answered Sep 21 '22 11:09

Hans Passant


  1. Do your rendering in the Paint event handler
  2. Disable automatic erasing of the background.
  3. Enable double buffering either via the Styles or manually.
  4. When you want to repaint, call Invalidate

If you're trying to pull of smooth animation, then may I recommend jumping ship to WPF, OpenGL, or XNA. GDI+ was not designed for animation (the Windows message loop is not a real-time system, so you will always have jittering).

like image 44
Frank Krueger Avatar answered Sep 23 '22 11:09

Frank Krueger