首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C# DoubleBuffered =白屏

C# DoubleBuffered =白屏
EN

Stack Overflow用户
提问于 2018-12-01 01:02:14
回答 1查看 87关注 0票数 0

我正在试着做一个简单的动画程序,当我点击按钮时,它就会做一些事情。每当发生这种情况时,我都会遇到闪烁,因为程序正在绘制一个“巨大的”(1000x700px)位图。我想摆脱那些闪烁的东西。

我听说可以用DoubleBuffered解决这个问题,但是如果我添加它,图形根本不会显示。相反,我得到了我的面板将全白背景和我的单一按钮。

谁能给我解释一下我做错了什么?

代码语言:javascript
复制
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
    }
}
}

设计者:

代码语言:javascript
复制
    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);

    }
EN

回答 1

Stack Overflow用户

发布于 2018-12-01 01:24:00

我的评论是:你不能丢弃不是你创建的对象。不要这样做:

代码语言:javascript
复制
mr_sanchez.Dispose();

paint事件正在向您“传递”一个图形对象。你不是在创建它,当你这样做的时候,你只是在“引用”它:

代码语言:javascript
复制
Graphics mr_sanchez = e.Graphics;

这与前面的代码不同,在前面的代码中,您可以正确地处理在此代码中创建的图形对象:

代码语言:javascript
复制
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
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53561887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档