C# 保持钢笔大小?

C# 保持钢笔大小?,c#,graphics,C#,Graphics,所以我正在制作一个绘画应用程序,我想知道如何保持我画的线条的厚度。因此,我的应用程序使用所有绘制线的点列表,并在用户每次绘制新线时再次绘制所有点。现在我有一个问题,当我改变画笔的大小时,所有线条的大小都会改变,因为它们都会被重新绘制 我的代码: //Create new pen Pen p = new Pen(Color.Black, penSize); //Set linecaps for start and end to round

所以我正在制作一个绘画应用程序,我想知道如何保持我画的线条的厚度。因此,我的应用程序使用所有绘制线的点列表,并在用户每次绘制新线时再次绘制所有点。现在我有一个问题,当我改变画笔的大小时,所有线条的大小都会改变,因为它们都会被重新绘制

我的代码:

        //Create new pen
        Pen p = new Pen(Color.Black, penSize);
        //Set linecaps for start and end to round
        p.StartCap = LineCap.Round;
        p.EndCap = LineCap.Round;
        //Turn on AntiAlias
        e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
        //For each list of line coords, draw all lines
        foreach (List<Point> lstP in previousPoints)
        { 
            e.Graphics.DrawLine(p, lstP[0], lstP[1]);
        }
        p.Dispose();
//创建新笔
钢笔p=新钢笔(颜色为黑色,蓝色);
//将“起点”和“终点”的线帽设置为“圆形”
p、 StartCap=线头圆形;
p、 EndCap=线头。圆形;
//启用反别名
e、 Graphics.SmoothingMode=SmoothingMode.AntiAlias;
//对于每个线坐标列表,绘制所有线
foreach(在以前的点中列出lstP)
{ 
e、 绘图线(p,lstP[0],lstP[1]);
}
p、 处置();
我知道在循环过程中可以使用Pen.Width()来更改笔的大小,但如何保留线宽?

请编写一个具有
列表和笔宽度的类,并使用该类的列表,而不是
列表和笔宽度。我们也会加上颜色,但你可以省略

public class MyPointList {
    public List<Point> Points { get; set; }
    public float PenWidth { get; set; }
    public Color Color { get; set; }
}
using
块处理笔

正如Kyle在评论中指出的那样,您也可以给
MyPointList
一个绘制图形的方法

事实上,您可以使用抽象或虚拟的
Draw(Graphics g)
方法编写基类:

public abstract class MyDrawingThing {
    public abstract void Draw(Graphics g);
}

public class MyPointList : MyDrawingThing {
    public List<Point> Points { get; set; }
    public float PenWidth { get; set; }
    public Color Color { get; set; }

    public override void Draw(Graphics g) {
        using (var p = new Pen(Color, PenWidth)) {
            g.DrawLine(p, Points[0], Points[1]);
        }
    }
}
公共抽象类MyDrawingThing{
公开摘要作废图(图g);
}
公共类MyPointList:MyDrawingThing{
公共列表点{get;set;}
公共浮点PenWidth{get;set;}
公共颜色{get;set;}
公共覆盖无效绘制(图形g){
使用(var p=新笔(颜色、笔宽)){
g、 抽绳(p,点[0],点[1]);
}
}
}
…并像这样使用:

private List<MyDrawingThing> previousPoints;

foreach (MyDrawingThing thing in previousPoints) {
    thing.Draw(e.Graphics);
}
private列出以前的点;
foreach(我在前面几点中画的东西){
画画(如图形);
}

写一打不同的子类来画圆、弧、棒棒糖等等

你真是个天才!谢谢。事实上,你甚至可以在这个自定义类中添加一个
Draw
方法来处理用正确的
Pen
@Kyle来画线。谢谢,我添加了这个。你可能想学习一下
public abstract class MyDrawingThing {
    public abstract void Draw(Graphics g);
}

public class MyPointList : MyDrawingThing {
    public List<Point> Points { get; set; }
    public float PenWidth { get; set; }
    public Color Color { get; set; }

    public override void Draw(Graphics g) {
        using (var p = new Pen(Color, PenWidth)) {
            g.DrawLine(p, Points[0], Points[1]);
        }
    }
}
private List<MyDrawingThing> previousPoints;

foreach (MyDrawingThing thing in previousPoints) {
    thing.Draw(e.Graphics);
}