如何做出更好的线条?

How to make better lines?

所以我正在用 C# 制作一个绘图应用程序,它的工作原理是在用户单击绘图面板时标记一个点,然后当用户 his/her 按下鼠标时在鼠标按下时画一条线搬到了新的位置;我正在学习 C#,所以它非常基础。现在一切都很好,直到我调整笔的大小,当我这样做时,线条开始看起来非常奇怪?有谁知道有什么可能的解决方案可以使这条线看起来正常吗?

这是我的代码,我使用的是 windows 表单应用程序:

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 Microsoft.VisualBasic;
using System.Drawing.Drawing2D;

namespace paintApplication
{

    public partial class frmPaintApp : Form
    {
        /// <summary>
        /// variables
        /// </summary>
        bool shouldPaint = false;
        Point prePoint;
        float penSize = 1;
        Graphics g;

        ColorDialog cd = new ColorDialog();

        public frmPaintApp()
        {
            InitializeComponent();
            g = pnlPaintPanel.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
        }

        private void msPensize_Click(object sender, EventArgs e)
        {

            if (float.TryParse(msTxtchoosesize.Text , out penSize))
            {
                msTxtchoosesize.Text = "";
            }
        }

        private void pnlPaintPanel_MouseDown(object sender, MouseEventArgs e)
        {
            shouldPaint = true;
            prePoint = new Point(e.X, e.Y);
        }

        private void pnlPaintPanel_MouseUp(object sender, MouseEventArgs e)
        {
            shouldPaint = false;
        }

        private void pnlPaintPanel_MouseMove(object sender, MouseEventArgs e)
        {
            Pen p = new Pen(cd.Color, penSize);


            if (shouldPaint == true)
            {
                g.DrawLine(p, prePoint, new Point(e.X, e.Y));
            }

            prePoint = new Point(e.X, e.Y);

        }

        private void msChoosecolor_Click(object sender, EventArgs e)
        {
            cd.ShowDialog();
        }

        private void frmPaintApp_ResizeEnd(object sender, EventArgs e)
        {
            g = pnlPaintPanel.CreateGraphics();
            g.SmoothingMode = SmoothingMode.AntiAlias;
        }

        private void msClear_Click(object sender, EventArgs e)
        {
            g.Clear(pnlPaintPanel.BackColor);

        }

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

        private void pnlPaintPanel_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}

除了我关于使用 CreateGraphics 等的评论外,请尝试更改您的 LineCaps(并处理您的笔,您正在泄漏内存):

using (Pen p = new Pen(Color.Black, penSize)) {
  p.StartCap = LineCap.Round;
  p.EndCap = LineCap.Round;
  if (shouldPaint) {
    g.DrawLine(p, prePoint, new Point(e.X, e.Y));
  }
}