c# - 在 C# 中绘画有间隙

标签 c# events paint

我正在尝试用 C# 编写一个小的绘图程序。到目前为止一切正常,唯一的问题是当我足够快地移动鼠标时,应该是实线的地方会出现间隙。我已经尝试了从双缓冲到减少 mouse_move 事件间隔的所有方法(我实际上没有找到任何方法来做到这一点,我认为这对系统上的其他进程也不利^^)

你能给我指出正确的方向吗?我尝试覆盖面板的 paint 方法,但是当我尝试这个时,似乎什么也没有发生。

代码如下:

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 Paint
{
    public partial class Form1 : Form
    {

        bool paint;
        SolidBrush color;
        //size of brush
        int pinselGröße;
        List<Point> pointListe;

        public Form1 ()
        {
            InitializeComponent ();
            pointListe = new List<Point>();
            paint = false;
            color = new SolidBrush ( Color.Black );
            //get brush size from combobox 
            pinselGröße = Convert.ToInt32 ( nudBrushSize.Value );
        }

        private void btnExit_Click ( object sender, EventArgs e )
        {
            this.Close ();
        }

        private void btnClear_Click ( object sender, EventArgs e )
        {
            Graphics gfx = pnlCanvas.CreateGraphics ();
            gfx.Clear ( pnlCanvas.BackColor );
        }

        private void pnlCanvas_MouseDown ( object sender, MouseEventArgs e )
        {
            paint = true;
            Graphics grfx = pnlCanvas.CreateGraphics ();
            //draw a rectangle with brush "color" and pinselGröße as the brush size
            grfx.FillRectangle ( color, e.X, e.Y, pinselGröße, pinselGröße );  
        }

        private void pnlCanvas_MouseMove ( object sender, MouseEventArgs e )
        {
            if ( paint )
            {
                //Graphics grfx = pnlCanvas.CreateGraphics();  
                ////put old position of mouse into variable
                //int altePosX = e.X;
                //int altePosY = e.Y;
                ////grfx.FillEllipse ( color, e.X, e.Y, pinselGröße, pinselGröße );
                //grfx.FillRectangle(color, e.X, e.Y, pinselGröße, pinselGröße);
                //grfx.Dispose();
                pointListe.Add(e.Location);
                pnlCanvas.Invalidate();
            }
        }

        private void pnlCanvas_Paint(PaintEventArgs e)
        {

            e.Graphics.DrawLines(new Pen(color), pointListe.ToArray());
        }

        private void pnlCanvas_MouseUp ( object sender, MouseEventArgs e )
        {
            paint = false;
        }

        private void nudBrushSize_ValueChanged ( object sender, EventArgs e )

            //when value of combobox changes, read value into brush size variable
            pinselGröße = Convert.ToInt32 ( nudBrushSize.Value );
        }

        private void cmbColor_SelectedIndexChanged ( object sender, EventArgs e )
        {            
            int index = cmbColor.SelectedIndex;
            color.Dispose ();
            switch ( index )
            {
                case 0:
                    {
                        color = new SolidBrush ( Color.Black );
                        break;
                    }
                case 1:
                    {
                        Console.WriteLine ( "Geht" );
                        color = new SolidBrush ( Color.Red );
                        break;
                    }
                case 2:
                    {
                        color = new SolidBrush ( Color.Blue );
                        break;
                    }
                case 3:
                    {
                        color = new SolidBrush ( Color.Green );
                        break;
                    }
            }
        }


    }
}

当我这样做时:

private void pnlCanvas_MouseMove ( object sender, MouseEventArgs e )
        {
            if ( paint )
            {
                Graphics grfx = pnlCanvas.CreateGraphics();
                ////put old position of mouse into variable
                int altePosX = e.X;
                int altePosY = e.Y;
                //grfx.FillEllipse ( color, e.X, e.Y, pinselGröße, pinselGröße );
                grfx.FillRectangle(color, e.X, e.Y, pinselGröße, pinselGröße);
                grfx.Dispose();
                //pointListe.Add(e.Location);
                //pnlCanvas.Invalidate();
            }
        }

        //private void pnlCanvas_Paint(PaintEventArgs e)
        //{
        //    Console.Write("mjsda2");
        //    e.Graphics.DrawLines(new Pen(color), pointListe.ToArray());
        //}

我明白了:

enter image description here

最佳答案

我不确定我们要使用哪种绘图模式,所以这里有两个版本:

同样值得注意的是,您的绘画事件处理程序有错误的签名,因此可能没有连接到 pnlCanvas。

在执行绘图代码时,您应该(几乎)永远不需要调用 CreateGraphics - 这通常是“您做错了”的标志。

这将让您通过点击点画线:

public partial class Form1 : Form
{

    SolidBrush color;
    List<Point> pointListe;
    Point _mousePoint;

    public Form1()
    {
        InitializeComponent();
        pointListe = new List<Point>();
        color = new SolidBrush(Color.Black);
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        pointListe.Clear();
        pnlCanvas.Invalidate();
    }

    private void pnlCanvas_MouseDown(object sender, MouseEventArgs e)
    {
        pointListe.Add(e.Location);
    }

    private void pnlCanvas_MouseMove(object sender, MouseEventArgs e)
    {
        _mousePoint = e.Location;
        pnlCanvas.Invalidate();
    }

    private void pnlCanvas_Paint(object sender, PaintEventArgs e)
    {
        if (pointListe.Count > 1)
        {
            e.Graphics.DrawLines(new Pen(color), pointListe.ToArray());
        }

        if (pointListe.Any())
        {
            e.Graphics.DrawLine(new Pen(color), pointListe.Last(), _mousePoint);
        }
    }

}

这将绘制一条连续的线:

public partial class Form1 : Form
{

    SolidBrush color;
    List<List<Point>> _lines;
    Boolean _mouseDown;

    public Form1()
    {
        InitializeComponent();
        _lines = new List<List<Point>>();
        color = new SolidBrush(Color.Black);
        _mouseDown = false;
    }

    private void btnClear_Click(object sender, EventArgs e)
    {
        _lines.Clear();
        pnlCanvas.Invalidate();
    }

    private void pnlCanvas_MouseDown(object sender, MouseEventArgs e)
    {
        _mouseDown = true;
        _lines.Add(new List<Point>());
    }

    private void pnlCanvas_MouseMove(object sender, MouseEventArgs e)
    {
        if (_mouseDown)
        {
            _lines.Last().Add(e.Location);
            pnlCanvas.Invalidate();
        }
    }

    private void pnlCanvas_MouseUp(object sender, MouseEventArgs e)
    {
        _mouseDown = false;
    }

    private void pnlCanvas_Paint(object sender, PaintEventArgs e)
    {
        foreach (var lineSet in _lines)
        {
            if (lineSet.Count > 1)
            {
                e.Graphics.DrawLines(new Pen(color), lineSet .ToArray());
            }   
        }

    }

}

关于c# - 在 C# 中绘画有间隙,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13311993/

相关文章:

java - 为什么我的 UI 元素在执行 Paint(); 后消失了?

Java Canvas 绘图

c# - new 关键字对 C# 方法的返回类型有什么影响?

c# - 如何对使用数据库访问的静态方法进行单元测试?

c# - 在 ConcurrentDictionary AddOrUpdate 中为更新部分添加什么

java - 在Android上获取当前手指位置

c# - ShowDialog 退出时如何防止焦点改变?

javascript - 为什么我的输入焦点指令不再起作用?

Android:如何创建 MotionEvent?

Java GUI 类按钮面板