I'm trying to make a simple animated program, which do things whenever i click the button. Whenever that happens though i experience flickering, because the program is drawing a "huge" (1000x700px) bitmap. I want to get rid of that flickering.
I heard that it can be solved using DoubleBuffered, but if i add it, the graphics are not displaying at all. Instead i got my panel will full white background and my single button only.
Could anybody explain me what am i doing wrong?
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 test
{
public partial class Form1 : Form
{
private static Bitmap graf_bufor = new Bitmap(1000, 700); //create huge bitmap
static Image green_one = Image.FromFile("green.png"); //load sprite
static int hero = 0; //create "hero" variable for looping
public Form1()
{
InitializeComponent();
//DoubleBuffered = true;
/**
* Without DoubleBuffered:
* Screen flickers when Refresh();
* With BoudleBuffered:
* The whole panel is white rectangular
**/
}
private void button1_Click(object sender, EventArgs e)
{
using (Graphics gr = Graphics.FromImage(graf_bufor)) //call "gr"
{
gr.Clear(Color.Black); //clear screen
gr.DrawImage(green_one, 0+(hero*32), 0); //draw sprite
}
hero = hero + 1; //adjust "hero" variable. not important.
if (hero == 4)
{
hero = 0;
};
this.Refresh(); //refresh panel
}
private void Form1_Paint(object sender, PaintEventArgs e) //painting event
{
Graphics mr_sanchez = e.Graphics; //call Mr Sanchez
mr_sanchez.DrawImage(graf_bufor, 0, 0); //Mr Sanchez draws bitmap
mr_sanchez.Dispose(); //Well done Mr Sanchez
}
}
}
Designer:
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(1107, 561);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1220, 768);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.Paint += new System.Windows.Forms.PaintEventHandler(this.Form1_Paint);
this.ResumeLayout(false);
}