如何用C#和GDI +绘制草图线

时间:2015-02-01 07:13:23

标签: c# winforms drawing gdi+

是否可以在GDI +和c#中绘制如下所示的线条和数字?

enter image description here

可能有一些方法可以在c#中轻松完成吗?

更新: 我的意思是我需要模仿GDI +中的手绘效果 我想写一些类似的东西:

graphics.DrawHandDrawnLine(Pens.Black, x1, y1, x2, y2);

并看到类似的内容

hand drawn line

2 个答案:

答案 0 :(得分:2)

我相信在“更容易”的情况下,这将很难达到顶峰。部门..:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing.Imaging;
using System.Drawing;
using System.Linq;

using System.Windows.Forms;

namespace Doodle
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    List<Point> curPoints = new List<Point>();
    List<List<Point>> allPoints = new List<List<Point>>();

    private void pnlPaint_MouseDown(object sender, MouseEventArgs e)
    {
        if (curPoints.Count > 1)
        {
            // begin fresh line
            curPoints.Clear();
            // startpoint
            curPoints.Add(e.Location);
        }
    }

    private void pnlPaint_MouseUp(object sender, MouseEventArgs e)
    {
        if (curPoints.Count > 1)
        {
            // ToList creates a copy
            allPoints.Add(curPoints.ToList());
            curPoints.Clear();
        }


    }

    private void pnlPaint_MouseMove(object sender, MouseEventArgs e)
    {
        if (e.Button != MouseButtons.Left) return;
        // here we should check if the distance is more than a minimum!
        curPoints.Add(e.Location);
        // let it show
        pnlPaint.Invalidate();
    }
    private void pnlPaint_Paint(object sender, PaintEventArgs e)
    {
        using (Pen pen = new Pen(Color.Black, 3f))
        {
            // regular edges:
            pen.MiterLimit = 1.5f
            // current lines
            if (curPoints.Count > 1) e.Graphics.DrawCurve(pen, curPoints.ToArray());
            // other lines
            foreach (List<Point> points in allPoints)
                if (points.Count > 1) e.Graphics.DrawCurve(pen, points.ToArray());
        }
    }}

    private void btn_undo_Click(object sender, EventArgs e)
    {
        if (allPoints.Count > 0)
        {
            allPoints.RemoveAt(allPoints.Count - 1);
            pnlPaint.Invalidate();
        }
    }

    private void btn_save_Click(object sender, EventArgs e)
    {
        string fileName = @"d:\sketch.png";
        Bitmap bmp = new Bitmap(pnlPaint.ClientSize.Width, pnlPaint.ClientSize.Width);
        pnlPaint.DrawToBitmap(bmp, pnlPaint.ClientRectangle);
        bmp.Save(fileName, ImageFormat.Png);
    }
  }

  class DrawPanel : Panel 
  {
     public DrawPanel ()
      {
        DoubleBuffered = true;
      }
  }

}

只需添加一个DrawPanel和两个Buttons ..

enter image description here

(我真的应该使用我的Wacom,还有一点空间......)

答案 1 :(得分:0)

我找到了这个解决方案Creating a Hand-Drawn effect using .NET

也许有更简单的东西,例如转化的东西?