如何用C#和GDI+;

如何用C#和GDI+;,c#,winforms,drawing,gdi+,C#,Winforms,Drawing,Gdi+,是否可以在GDI+和c#中绘制如下所示的线条和图形 也许在c中有一些简单的方法可以做到这一点 更新: 我的意思是我需要在GDI中模拟手绘效果+ 我想写一些类似于: graphics.DrawHandDrawnLine(Pens.Black, x1, y1, x2, y2); 看到这样的东西了吗 我找到了这个解决方案 也许有更简单的东西,例如转换?我相信在“更简单”部门很难做到这一点 using System; using System.Collections.Generic; using S

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

也许在c中有一些简单的方法可以做到这一点

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

graphics.DrawHandDrawnLine(Pens.Black, x1, y1, x2, y2);
看到这样的东西了吗

我找到了这个解决方案


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

我相信在“更简单”部门很难做到这一点

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;
      }
  }

}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统、绘图、成像;
使用系统图;
使用System.Linq;
使用System.Windows.Forms;
名称空间涂鸦
{
公共部分类Form1:Form
{
公共表格1()
{
初始化组件();
}
List curPoints=新列表();
列出所有点=新建列表();
私有void pnlpoint_MouseDown(对象发送方,MouseEventArgs e)
{
如果(curPoints.Count>1)
{
//重新排队
curPoints.Clear();
//起点
宵禁点。添加(如位置);
}
}
私有void pnlPaint_MouseUp(对象发送方,MouseEventArgs e)
{
如果(curPoints.Count>1)
{
//托利斯特创建了一个副本
allPoints.Add(curPoints.ToList());
curPoints.Clear();
}
}
私有void pnlpoint_MouseMove(对象发送方,MouseEventArgs e)
{
如果(e.Button!=MouseButtons.Left)返回;
//在这里,我们应该检查距离是否超过最小值!
宵禁点。添加(如位置);
//让它表现出来
pnlpoint.Invalidate();
}
私有void pnlPaint_Paint(对象发送方,PaintEventArgs e)
{
使用(钢笔=新钢笔(颜色:黑色,3f))
{
//规则边:
pen.MiterLimit=1.5f
//电流线路
如果(curPoints.Count>1)e.Graphics.DrawCurve(画笔,curPoints.ToArray());
//其他线路
foreach(列出所有点中的点)
如果(points.Count>1)e.Graphics.DrawCurve(画笔,points.ToArray());
}
}}
私有无效btn\u撤消\u单击(对象发送方,事件参数e)
{
如果(allPoints.Count>0)
{
allPoints.RemoveAt(allPoints.Count-1);
pnlpoint.Invalidate();
}
}
私有无效btn\u保存\u单击(对象发送者,事件参数e)
{
字符串文件名=@“d:\sketch.png”;
位图bmp=新位图(pnlPaint.ClientSize.Width,pnlPaint.ClientSize.Width);
pnlPaint.DrawToBitmap(bmp,pnlPaint.ClientRectangle);
保存(文件名,ImageFormat.Png);
}
}
类DrawPanel:Panel
{
公共事务委员会()
{
双缓冲=真;
}
}
}
只需添加一个绘图面板和两个按钮

(我真的应该用我的Wacom,多留点空间.)



更新:代替
面板
,该面板是
容器
控件,并不真正用于绘制。您可以使用
图片盒
标签
(使用
自动大小=false
);两者都具有现成的
DoubleBuffered
属性,并且比
Panels
do更好地支持绘图。

GDI+具有所有API,如DrawRectangle、DrawLine、DrawString等,具体取决于您要绘制的内容和位置。是的,我知道,谢谢。我想我找到了我需要的东西。您正在寻找。我是指Graphics.TransformPoints和Graphics中的类似方法请参阅我上面的评论。当然,除了简单的涂鸦,了解你想要实现什么会有所帮助,因为我们将这些点保留在列表中,我们可以对它们应用各种转换。将其粘贴到
绘制
事件的开头:
矩阵=新矩阵();矩阵缩放((浮点)nud_缩放值/100f,(浮点)nud_缩放值/100f);e、 图形.多重变换(矩阵)
现在添加一个带有
pnlpoint.Invalidate()的
NumericUpDown
ValueChanged
事件中,可以缩放图形。。