上一篇【學習筆記】Windows GDI繪圖(五)圖形路徑GraphicsPath詳解(上)介紹了GraphicsPath類的構造函數、屬性和方法AddArc添加橢圓弧、AddBezier添加貝賽爾曲線、AddClosedCurve添加封閉基數樣條曲線、AddCurve添加開放基數樣條曲線、基數樣條如何轉Bezier、AddEllipse添加橢圓、AddLine添加線段。
革命尚未成功,同志仍需努力!
文章目錄
- GraphicsPath方法
- AddLines添加線段
- AddPath附加路徑
- AddPie添加餅形
- AddPolygon添加多邊形
- AddRectangle和AddRectangles 添加矩形
- AddString添加字符串
- SetMarkers設置標記
- ClearMarkers清空標記
- StartFigure開始新的圖形
- CloseAllFigures閉合所有圖形、CloseFigure閉合當前圖形

GraphicsPath方法
AddLines添加線段
原型:
public void AddLines (params System.Drawing.Point[] points);
public void AddLines (params System.Drawing.PointF[] points);
添加一系列的線段到GraphicsPath中。
// 定義三角形的頂點
Point[] points ={new Point(150,150),new Point(300,300),new Point(0,300),new Point(150,150)};using (var myPath = new GraphicsPath())
{myPath.AddLines(points);e.Graphics.DrawPath(penRed, myPath);
}
通過點集繪制線段
AddPath附加路徑
原型:
public void AddPath (System.Drawing.Drawing2D.GraphicsPath addingPath, bool connect);
connect,當前路徑與附加的路徑是否相連
將指定的GraphicsPath附加到當前Path中
Point[] myArray ={new Point(120,120),new Point(240,240),new Point(0,240),new Point(120,120)};
GraphicsPath myPath = new GraphicsPath();
myPath.AddLines(myArray);Point[] myArray2 ={new Point(120,100),new Point(20,20),new Point(220,20),new Point(120,100)};
GraphicsPath myPath2 = new GraphicsPath();
myPath2.AddLines(myArray2);// Add the second path to the first path.
myPath.AddPath(myPath2, false);//各自獨立// Draw the combined path to the screen.
e.Graphics.DrawPath(penRed, myPath);myPath.Reset();
myPath.AddLines(myArray);
myPath.AddPath(myPath2, true);//相連myPath.Transform(new Matrix(1, 0, 0, 1, 400, 0));
e.Graphics.DrawPath(penLightGreen, myPath);
AddPie添加餅形
原型:
public void AddPie (System.Drawing.Rectangle rect, float startAngle, float sweepAngle);
public void AddPie (int x, int y, int width, int height, float startAngle, float sweepAngle);
public void AddPie (float x, float y, float width, float height, float startAngle, float sweepAngle);
通過一個矩形、起始角度和掃描角度來角度一個餅形,與橢圓參數類似。
var rect = new Rectangle(100, 100, 200, 100);
using (var path = new GraphicsPath())
{//添加餅形 30°至150°path.AddPie(rect, 30, 120);e.Graphics.DrawPath(penRed, path);path.Reset();//150°至270°path.AddPie(rect, 30 + 120, 120);e.Graphics.DrawPath(penLightGreen, path);path.Reset();//30°到 270°(逆時針)path.AddPie(rect, 30, -120);e.Graphics.DrawPath(Pens.Chocolate, path);
}
sweepAngle,正數,順時針;負數,逆時針
AddPolygon添加多邊形
原型:
public void AddPolygon (System.Drawing.Point[] points);
public void AddPolygon (System.Drawing.PointF[] points);
定義點集,形成多邊形
// 多邊形頂點
Point[] myArray ={new Point(230, 200),new Point(400, 100),new Point(570, 200),new Point(500, 400),new Point(300, 400)};using (var myPath = new GraphicsPath())
{//添加多邊形myPath.AddPolygon(myArray);e.Graphics.DrawPath(penRed, myPath);
}
AddRectangle和AddRectangles 添加矩形
原型:
public void AddRectangle (System.Drawing.RectangleF rect);
public void AddRectangle (System.Drawing.Rectangle rect);
public void AddRectangles (System.Drawing.Rectangle[] rects);
public void AddRectangles (params System.Drawing.RectangleF[] rects);
定義矩形和矩形集,添加到路徑中。
var rect = new Rectangle(30, 50, 100, 80);var rects = new RectangleF[]
{new RectangleF(150,50,80,60),new RectangleF(200,80,100,80)
};using (var myPath = new GraphicsPath())
{ myPath.AddRectangle(rect);myPath.AddRectangles(rects);e.Graphics.DrawPath(penRed, myPath);
}
AddString添加字符串
原型:
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.Point origin, System.Drawing.StringFormat? format);
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.PointF origin, System.Drawing.StringFormat? format);
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.Rectangle layoutRect, System.Drawing.StringFormat? format);
public void AddString (string s, System.Drawing.FontFamily family, int style, float emSize, System.Drawing.RectangleF layoutRect, System.Drawing.StringFormat? format);
參數 | 說明 |
---|---|
s | 待添加的文本 |
family | FontFamily字體名稱 |
style | 文本樣式,Bold-1,Italic-2,Regular-0,Strikeout-8,Underline-4 |
emSize | 文本高度,單位:像素 |
origin | 文本起始點,默認是左對齊時,是左上角 |
format | 指定文本格式信息,例如行距和對齊方式 |
這里先隨便給個示例吧,估計關于繪制文本,可以另起一篇。
// Create a GraphicsPath object.
GraphicsPath myPath = new GraphicsPath();// Set up all the string parameters.
string stringText = "我在學習GDI+";
FontFamily family = new FontFamily("Arial");
int fontStyle = (int)FontStyle.Italic;
int emSize = 38;//文本高度,像素
Point origin = new Point(200, 100);//文本開始繪制的左上角點StringFormat format = new StringFormat(StringFormatFlags.NoWrap);
format.Alignment = StringAlignment.Center;//水平居中
format.LineAlignment = StringAlignment.Center; // 垂直居中// Add the string to the path.
myPath.AddString(stringText,family,fontStyle,emSize,origin,format);e.Graphics.FillPath(Brushes.Green, myPath);//文本定位點
e.Graphics.FillEllipse(Brushes.Red, origin.X - 3, origin.Y - 3, 6, 6);
SetMarkers設置標記
原型:
public void SetMarkers ();
使用 SetMarkers 方法在 GraphicsPath 中的當前位置創建標記。使用 NextMarker 方法迭代路徑中的現有標記。
標記用于分隔子路徑組。兩個標記之間可以包含一個或多個子路徑。
[System.ComponentModel.Description("GraphicsPath的SetMarkers/ClearMarkers方法")]
public void Demo06_07(PaintEventArgs e)
{// Create a path and set two markers.GraphicsPath myPath = new GraphicsPath();myPath.AddLine(new Point(0, 0), new Point(50, 50));myPath.SetMarkers();Rectangle rect = new Rectangle(50, 50, 50, 50);myPath.AddRectangle(rect);myPath.SetMarkers();myPath.AddEllipse(100, 100, 100, 50);// Draw the path to screen.e.Graphics.DrawPath(new Pen(Color.Black, 2), myPath);var pathIterator =new GraphicsPathIterator(myPath);pathIterator.Rewind();var potins = myPath.PathPoints;var types = myPath.PathTypes;var height = 20;var markerIndex = 0;int startIndex;int endIndex;while(true){var resultCount = pathIterator.NextMarker(out startIndex, out endIndex);if (resultCount == 0) break;//Marker信息e.Graphics.DrawString($"Marker {markerIndex}: Start: {startIndex} End: {endIndex}",Font,Brushes.Red,200,height);height += 20;//每段Marker的點與類型信息for (int i = startIndex; i <= endIndex; i++){e.Graphics.DrawString($"point {i}: ({potins[i].X},{potins[i].Y}) Type:{(int)types[i]}:{GetPathTypes((int)types[i])}",Font,Brushes.Black,250,height);height += 20;}markerIndex++;}myPath.ClearMarkers();pathIterator = new GraphicsPathIterator(myPath);pathIterator.Rewind();var count= pathIterator.NextMarker(out startIndex, out endIndex);//這里合成一個,0至19
}
/// <summary>
/// PathType轉字符串
/// </summary>
/// <param name="pathType"></param>
/// <returns></returns>
private string GetPathTypes(int pathType)
{if (pathType == 0) return "0(起點)";List<string> typeStrs = new List<string>();while(true){if(pathType >= 0x80){typeStrs.Add("128(終點)");pathType -= 0x80;}else if (pathType >= 0x20){typeStrs.Add("32(標記)");pathType -= 0x20;}else if (pathType >=0x7){typeStrs.Add("7(屏蔽)");pathType -= 0x7;}else if(pathType >= 0x3){typeStrs.Add("3(Bezier)");pathType -= 0x3;}else if (pathType >= 1){typeStrs.Add("1(Line)");pathType -= 0x1;}if (pathType <= 0) break;}return string.Join("+",typeStrs.ToArray());
}
ClearMarkers清空標記
原型:
public void ClearMarkers ();
清除所有標記(Marker),合并為一個。
StartFigure開始新的圖形
原型:
public void StartFigure ();
在不封閉當前圖形(路徑)下,新開一個圖形,后續增加的路徑將在此圖形中。
CloseAllFigures閉合所有圖形、CloseFigure閉合當前圖形
原型:
public void CloseAllFigures ();
public void CloseFigure ();
CloseAllFigures :閉合該路徑中所有開放的圖形并開始一個新圖形。它通過從端點到起點連接一條線來閉合每個開放圖形。
CloseFigure:閉合當前圖形并開始新圖形。
// 創建含多個開放路徑的圖形
GraphicsPath myPath = new GraphicsPath();
myPath.StartFigure();
myPath.AddLine(new Point(10, 10), new Point(150, 10));
myPath.AddLine(new Point(150, 10), new Point(10, 150));
myPath.StartFigure();
myPath.AddArc(200, 200, 100, 100, 0, 90);
myPath.StartFigure();
Point point1 = new Point(300, 300);
Point point2 = new Point(400, 325);
Point point3 = new Point(400, 375);
Point point4 = new Point(300, 400);
Point[] points = { point1, point2, point3, point4 };
myPath.AddCurve(points);// 繪制非封閉路徑
e.Graphics.DrawPath(new Pen(Color.Green, 10), myPath);// 封閉所有路徑.
myPath.CloseAllFigures();// 繪制封閉后路徑
e.Graphics.DrawPath(new Pen(Color.Red, 1), myPath);myPath = new GraphicsPath();
myPath.StartFigure();
myPath.AddLine(new Point(200, 10), new Point(400, 10));
myPath.AddLine(new Point(400, 10), new Point(400, 200));
myPath.CloseFigure();e.Graphics.DrawPath(penRed, myPath);
【學習筆記】Windows GDI繪圖目錄