如何制作更好的线条?

时间:2017-06-08 19:45:38

标签: c# winforms graphics drawing

所以我在C#中制作一个绘画应用程序,它通过在用户点击绘画面板时标记一个点来工作,然后当用户将鼠标移动到鼠标时用鼠标向下绘制一条线新职位;我学习C#所以它非常基础。现在一切都很好,直到我把笔放大,当我这样做时,线条开始显得超级奇怪?有没有人知道任何可能的解决方案,使线看起来正常?

Weird Lines

这是我的代码,我使用的是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)
        {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

除了我关于使用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));
  }
}