【问题标题】:C# DoubleBuffered = white screenC# DoubleBuffered = 白屏
【发布时间】:2019-05-02 20:47:12
【问题描述】:

我正在尝试制作一个简单的动画程序,每当我单击按钮时它都会执行此操作。每当发生这种情况时,虽然我会遇到闪烁,因为程序正在绘制一个“巨大的”(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);

    }

【问题讨论】:

  • 注意:您不会处理不是您创建的对象。不要这样做:mr_sanchez.Dispose();
  • 圣蟹……你真是个天才!这确实有效!谢谢!你不会相信我花了多少时间在谷歌上搜索并尝试自己解决它!天哪……好吧,但很严肃;现在。刚刚发生了什么?我认为处理用过的物品是必须的。我以为我确实在这里创建了一个对象,不是吗? “图形”类的新对象。
  • 最好只画运动的部分而不是大图..1
  • 谢谢 TaW :) 我更新了我的代码来实现它。

标签: c# doublebuffered


【解决方案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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-12
    • 1970-01-01
    • 2016-07-05
    • 2021-02-07
    • 2014-04-15
    • 1970-01-01
    相关资源
    最近更新 更多