[英]How to draw sketch lines with C# and GDI+
Is it possible to draw lines and figures like shown below in GDI+ and c#? 是否可以在GDI +和c#中绘制如下所示的线条和图形?
May be there is some way to do that in easy way in c#? 也许有一些方法可以在C#中以简单的方式做到这一点?
Update: I mean that I need to imitate Hand-Drawing effect in GDI+ I would like to write something like: 更新:我的意思是我需要模仿GDI +中的手绘效果,我想写一些类似的东西:
graphics.DrawHandDrawnLine(Pens.Black, x1, y1, x2, y2);
and see something like this 看到这样的东西
I believe this will be hard to top in the 'more easy' department..: 我相信这在“更简单”的部门中很难实现。
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;
}
}
}
Just add one DrawPanel and two Buttons.. 只需添加一个DrawPanel和两个Button ..
(I really should have used my Wacom, and a little more space..) (我真的应该用我的Wacom,还有更多空间。。)
Update: Instead of a Panel
, which is a Container
control and not really meant to draw onto you can use a Picturebox
or a Label
(with Autosize=false
); 更新:可以使用
Picturebox
或Label
(具有Autosize=false
)来代替Panel
,它不是Container
控件,并不是真正要绘图的Picturebox
; both have the DoubleBuffered
property turned on out of the box and support drawing better than Panels
do. 两者都具有
DoubleBuffered
即用的DoubleBuffered
属性,并且比Panels
更好地支持绘图。
I found this solution Creating a Hand-Drawn effect using .NET 我找到了使用.NET创建手绘效果的解决方案
Maybe there is something more easy, for example something with transformations? 也许有些更容易的事情,例如带有转换的事情?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.