Double Buffer で描画する

画面の「チラツキ」を無くす最も良い方法は、Double Buffer を使うことです。 (^_^;)
Back Buffer に次に描画するイメージを生成しておいて、一挙に Front Buffer に転送します。
これで、全く気にならないぐらいに「ちらつき」を無くすことが出来ます。 \(^o^)/
Double Buffer を使う方法は VC++ では結構面倒だったのですが、C# ではパラメータを設定するだけです。

前田稔(Maeda Minoru)の超初心者のプログラム入門

プロジェクトの設定

  1. 空のプロジェクトを作成して、ソースプログラムをプロジェクトに取り込んで下さい。
  2. 大きな画像から Sprite を切り出して、タイマの設定で Sprite を切り替えます。
    画像のロードは 「ちらつき」を無くす-2 を参照して下さい。
  3. Double Buffer の設定は SetStyle() でパラメータを設定するだけです。
            // MyForm
            Name = "MyForm";
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        
  4. Sprite を描画するメソッドは Frame Class の OnPaint() をオーバーライドしています。
    描画は bmp にロードした画像から Sprite を切り出します。
    Double Buffer を使っても描画の方法は何時もと同じで、自動的に BackBuffer にイメージを生成して一挙に FrontBuffer に転送されます。
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.DrawImage(bmp,new Rectangle(80,10,128,216),
                new Rectangle(SP_NO*128,0,128,216),GraphicsUnit.Pixel);
        }
        
  5. CLI の環境で実行可能な完成したプログラムです。
    /***************************************/
    /*★ Double Buffer を使う    前田 稔 ★*/
    /***************************************/
    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    public class MyForm : Form
    {
        private Timer timer1;
        private System.ComponentModel.IContainer components;
        public static Bitmap  bmp;
        int     SP_NO;
    
        public MyForm()
        {
            // timer1
            components = new System.ComponentModel.Container();
            timer1 = new System.Windows.Forms.Timer(this.components);
            SuspendLayout();
            timer1.Interval = 200;
            timer1.Tick += new System.EventHandler(this.timer1_Tick);
            // MyForm
            Name = "MyForm";
            SetStyle(ControlStyles.DoubleBuffer, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            SP_NO = 0;
            timer1.Start();
        }
    
        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            Graphics g = e.Graphics;
            g.DrawImage(bmp,new Rectangle(80,10,128,216),
                new Rectangle(SP_NO*128,0,128,216),GraphicsUnit.Pixel);
        }
    
        private void timer1_Tick(object sender, EventArgs e)
        {
            SP_NO= (SP_NO+1)%7;
            Invalidate();
        }
    }
    
    class anime
    {
        public static void Main()
        {
            // Load Image
            try
            {
                MyForm.bmp= new Bitmap("c:\\data\\test\\girl.gif");
    
            }
            catch
            {   MessageBox.Show("イメージが取得できません", "Error");
                return;
            }
            MyForm mf = new MyForm();
            Application.Run(mf);
        }
    }
    

【演習】

  1. 「チラツキ」を全く感じることなく、アニメーションが出来たでしょうか。 \(^o^)/
  2. bijin16.gif の画像でも試して下さい。

超初心者のプログラム入門(C# Frame Work)