c# - 如何做出更好的线条?

标签 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)
        {

        }
    }
}

最佳答案

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

关于c# - 如何做出更好的线条?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44444353/

相关文章:

c# - 如何添加自定义时间格式?

c# - 为什么计时器滴答事件连续调用该方法两次而不是每次10秒后调用该方法?

ios - 地形光照问题 Unity

java - 在小程序中绘制缓冲图像

c# - 如何加快加载启动画面

Android——几乎完全在 OpenGL 中制作用户界面有什么问题吗?

c# - 创建数组与列表

c# - LINQ 到实体 : Loading One-To-Many Navigation Properties in Strong Typed Projections

c# - 用于搜索日期的 SQL 查询

c# - 在主线程中运行代码