C#图形慢

时间:2016-09-29 13:38:02

标签: c# graphics

我一直在Visual Studio 2013中学习C#并遇到了问题 我画了一堆点(现在,20)在屏幕上移动。在刷新之间(每隔ms),我打电话清除图形。但是,这会导致我在清除之后绘制的点被擦除。最终的结果是点似乎在屏幕上闪烁。我是一名Java程序员,我就像在Java中那样接近这个图形。这是错的吗?我该怎么做才能解决我的问题?

我认为错误来自于我的tick方法需要大约9毫秒才能运行。

这是我的代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using System.Diagnostics;

namespace AstroidBlast {
    public partial class Form1 : Form {
        Random r = new Random();
        System.Windows.Forms.Timer gameTimer = new System.Windows.Forms.Timer();
        Particle[] particles = new Particle[20];
        int tick = 0;
        public Form1() {
            InitializeComponent();
            gameTimer.Interval = 1;
            gameTimer.Tick += new EventHandler(gameTimer_Tick);
            gameTimer.Start();
            this.Width = 800;
            this.Height = 600;
            for (int i = 0; i < particles.Length; i++) {
                particles[i] = new Particle(new Velocity(r.Next(0, 360) * (Math.PI / 180), r.NextDouble() *.75 + 0.25), 100, 100, r);
            }
        }
        private void Form1_Load(object sender, EventArgs e) {}
        private void gameTimer_Tick(object sender, EventArgs e) {
            Graphics g = this.CreateGraphics();
            Stopwatch s = new Stopwatch();
            s.Start();
            g.Clear(Color.White);
            for (int i = 0; i < particles.Length; i++)
                particles[i].draw(g, Math.Sqrt(tick++));
            s.Stop();
            Debug.Print(s.ElapsedMilliseconds + "");
        }
    }
    class Particle {
        static SolidBrush brush = new SolidBrush(Color.FromArgb(40, 40, 40));
        Random r;
        Velocity velocity;
        double x, y;
        public Particle(Velocity v, double x, double y, Random r){
            velocity = v;
            this.x = x;
            this.y = y;
            this.r = r;
        }
        public void draw(Graphics g, double t) {
            g.FillEllipse(brush, (int)(velocity.speed * t * Math.Cos(velocity.angle) + x), (int)(velocity.speed * t * Math.Sin(velocity.angle) + y), 8, 8);
        }
    }
    class Velocity {
        public double angle, speed;
        public Velocity(double angle, double speed) {
            this.angle = angle;
            this.speed = speed;
        }
    }
}

1 个答案:

答案 0 :(得分:3)

不,通常这不是用C#绘制的正确方法。

您应该覆盖OnPaint事件,该事件为您提供Graphics对象并绘制到该对象。在您的计时器内,您可以Invalidate要重绘的全部或部分区域

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;
    // draw here
}

private void gameTimer_Tick(object sender, EventArgs e) 
{
   this.Invalidate(); // optionally, provide the area to invalidate for better performance
}

通过告诉您的表单使用DoubleBuffered

,您还可以获得更好的性能/更少的闪烁
public Form1() 
{
    InitializeComponent();
    this.DoubleBuffered = true
}

使用上述更改重现代码不会导致明显的闪烁。