3

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

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

这是代码:

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());
        //}

我明白了:

在此处输入图像描述

4

3 回答 3

4

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

另外值得注意的是,您的绘画事件处理程序的签名错误,因此可能没有连接到 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());
            }   
        }

    }

}
于 2012-11-09T16:48:27.127 回答
2

我不清楚差距发生在哪里,但你不应该在 MouseDown 事件中添加你的第一点吗?这能解释你看到的差距类型吗?

为什么要在 MouseDown 事件中填充一个矩形?

否则,也许是这些差距的截图。

于 2012-11-09T16:31:19.490 回答
2

Mousemove 事件将跳过 - 鼠标实际上可以移动得非常快,比事件更快,并且您的应用程序可以跟上。结果,您不会得到一个连续的鼠标移动流,每个像素一个。

您需要做的是跟踪您在上一次鼠标移动中获得的上一个位置,然后从上一个位置到当前位置绘制一条线,而不是一个点。除非用户疯狂地快速移动鼠标,否则这将近似鼠标移动得足够好,以至于您不会注意到它并没有准确地跟踪每个像素。

于 2012-11-09T16:48:10.960 回答