C#DoubleBuffered =白屏

时间:2018-11-30 17:02:14

标签: c# doublebuffered

我正在尝试制作一个简单的动画程序,每当我单击该按钮时,它就会执行操作。每当发生这种情况时,尽管我遇到闪烁,因为该程序正在绘制“巨大”(1000x700px)位图。我想摆脱那种闪烁。

我听说可以使用DoubleBuffered解决此问题,但是如果我添加它,图形将根本无法显示。取而代之的是,我的面板将全白,并且只有一个按钮。

有人可以解释我在做什么错吗?

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
    }
}
}

设计师:

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

    }

1 个答案:

答案 0 :(得分:1)

根据我的评论:您不会处理未创建的对象。不要这样做:

mr_sanchez.Dispose();

绘画事件正在将图形对象“传递”给您。您不是在创建它,而是在执行此操作时只是在“引用”它:

Graphics mr_sanchez = e.Graphics;

这与您之前的代码不同,在先前的代码中,您正确处置了在此代码中创建的图形对象:

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
}