0

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);

    }
Liam
  • 27,717
  • 28
  • 128
  • 190
NastyKhan
  • 11
  • 2
  • 2
    Note: you don't dispose objects you didn't create. Don't do this: `mr_sanchez.Dispose();` – LarsTech Nov 30 '18 at 17:07
  • Holy Crab... You're a genius! That actually worked! Thank you! You will not believe how many hours I spent Googling and trying to solve it myself! Gosh... Ok but serious;y now. What just happened? I thought disposing used object is a must. And i thought i DO create an object here, am i not? New object of "Graphic" class. – NastyKhan Nov 30 '18 at 17:19
  • Better draw only the moving parts not a huge image..1 – TaW Nov 30 '18 at 18:00
  • Thanks TaW :) I updated my code to implement that too. – NastyKhan Dec 01 '18 at 00:40

1 Answers1

1

From my comment: you don't dispose objects you didn't create. Don't do this:

mr_sanchez.Dispose();

A paint event is "passing" a graphic object to you. You aren't creating it, you are simply "referencing" it when you do this:

Graphics mr_sanchez = e.Graphics;

This is different from your earlier code, where you correctly dispose the graphic object you created in this code:

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
}
LarsTech
  • 80,625
  • 14
  • 153
  • 225