C#绘制程序闪烁 [英] C# paint program flickering

查看:181
本文介绍了C#绘制程序闪烁的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在C#中制作一个简单的绘图程序,但是当我绘图时它会一直闪烁,就像我需要某种双缓冲,但我不知道该怎么做。



我正在使用 Panel ,并使用位图以存储图形。



以下是我的代码:

  public partial类Form1:表单
{
私有位图图形;
private bool leftDown = false;
private int prevX;
private int prevY;
private int currentX;
private int currentY;

public Form1()
{
InitializeComponent();

drawing = new Bitmap(panel1.Width,panel1.Height);
}

private void panel1_MouseDown(object sender,MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
leftDown = true;

prevX = e.X;
prevY = e.Y;


$ b $ private void panel1_MouseMove(object sender,MouseEventArgs e)
{
if(leftDown)
{
Graphics g = Graphics.FromImage(drawing);

currentX = e.X;
currentY = e.Y;

g.DrawLine(new Pen(Color.Black),new Point(currentX,currentY),new Point(prevX,prevY));
panel1.Invalidate();

prevX = currentX;
prevY = currentY;

g.Dispose();



private void panel1_MouseUp(object sender,MouseEventArgs e)
{
leftDown = false;
}

private void panel1_MouseLeave(object sender,EventArgs e)
{
leftDown = false;

$ b $ private void panel1_Paint(object sender,PaintEventArgs e)
{
e.Graphics.DrawImageUnscaled(drawing,0,0);



$ div $解析方案

你不仅要将 DoubleBuffered 转换为 true ,还要使用 PictureBox 而不是面板并绘制它。这应该解决您的问题:)。


I'm trying to make a simple paint program in C#, but it keeps flickering when I'm drawing, like I need some kind of double buffering, but I don't know how to do it.

I am drawing on a Panel and I'm using a Bitmap to store the graphics.

Here's my code:

public partial class Form1 : Form
{
    private Bitmap drawing;
    private bool leftDown = false;
    private int prevX;
    private int prevY;
    private int currentX;
    private int currentY;

    public Form1()
    {
        InitializeComponent();

        drawing = new Bitmap(panel1.Width, panel1.Height);
    }

    private void panel1_MouseDown(object sender, MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            leftDown = true;

            prevX = e.X;
            prevY = e.Y;
        }
    }

    private void panel1_MouseMove(object sender, MouseEventArgs e)
    {
        if (leftDown)
        {
            Graphics g = Graphics.FromImage(drawing);

            currentX = e.X;
            currentY = e.Y;

            g.DrawLine(new Pen(Color.Black), new Point(currentX, currentY), new Point(prevX, prevY));
            panel1.Invalidate();

            prevX = currentX;
            prevY = currentY;

            g.Dispose();
        }
    }

    private void panel1_MouseUp(object sender, MouseEventArgs e)
    {
        leftDown = false;
    }

    private void panel1_MouseLeave(object sender, EventArgs e)
    {
        leftDown = false;
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawImageUnscaled(drawing, 0, 0);
    }
}

解决方案

You not only should turn DoubleBuffered to true, but also use PictureBox instead of Panel and draw on it. This should solve your issue :).

这篇关于C#绘制程序闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆