【學習筆記】Windows GDI繪圖(六)圖形路徑GraphicsPath詳解(中)

上一篇【學習筆記】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

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

通過點集繪制線段
AddLines

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

AddPath

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,正數,順時針;負數,逆時針
AddPie

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

AddPolygon

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

AddRectangle

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待添加的文本
familyFontFamily字體名稱
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);

AddString

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

CloseAllFigure

【學習筆記】Windows GDI繪圖目錄

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/web/14579.shtml
繁體地址,請注明出處:http://hk.pswp.cn/web/14579.shtml
英文地址,請注明出處:http://en.pswp.cn/web/14579.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

華為校招機試 - 最久最少使用緩存(20240508)

題目描述 無線通信移動性需要在基站上配置鄰區(本端基站的小區 LocalCell 與周邊鄰基站的小區 NeighborCelI 映射)關系, 為了能夠加速無線算法的計算效率,設計一個鄰區關系緩存表,用于快速的通過本小區 LocalCell 查詢到鄰小區 NeighborCell。 但是緩存表有一定的規格限…

代碼隨想錄-Day07

454. 四數相加 II 給你四個整數數組 nums1、nums2、nums3 和 nums4 &#xff0c;數組長度都是 n &#xff0c;請你計算有多少個元組 (i, j, k, l) 能滿足&#xff1a; 0 < i, j, k, l < n nums1[i] nums2[j] nums3[k] nums4[l] 0 示例 1&#xff1a; 輸入&#x…

系統磁盤高級管理、lvm例子、創建pv、創建VG、創建lv、磁盤擴展

LVM&#xff1a; 邏輯卷&#xff0c;動態調整分區大小&#xff0c;擴展性好 創建pv pvcreate &#xff1a; 將實體 partition 創建成為 PV &#xff1b; pvscan &#xff1a; 搜尋目前系統里面任何具有 PV 的磁盤&#xff1b; pvdisplay &#xff1a; 顯示出目前系統上面…

GNSS仿真測試之三種常用坐標系與轉換

作者介紹 在當今的全球導航衛星系統&#xff08;GNSS&#xff09;技術領域&#xff0c;仿真測試是評估和驗證GNSS接收機性能的關鍵環節&#xff0c;全球導航衛星系統&#xff08;GNSS&#xff09;仿真測試是確保GNSS接收機和導航解決方案在實際部署前能夠正確、可靠地工作的關鍵…

【git】學習記錄: 貯藏功能

Git 貯藏修改是一種臨時存儲工作目錄中已經修改但尚未提交的更改的機制。通過貯藏修改&#xff0c;你可以將當前的工作目錄狀態保存起來&#xff0c;以便你可以在之后的時間點重新應用這些更改&#xff0c;或者在不同的分支間切換時避免沖突。 要使用 Git 貯藏修改&#xff0c…

Linux(centos)常用命令

Linux&#xff08;Centos&#xff09;常用命令使用說明文檔 切換到/home目錄下 使用cd命令切換目錄&#xff0c;例如&#xff1a; cd /home列出/home目錄下的所有文件 使用ls命令列出目錄下的文件和子目錄&#xff0c;例如&#xff1a; ls /home新建目錄dir1 使用mkdir命…

頭歌OpenGauss數據庫-I.復雜查詢第1關:獲取前N名成績

本關任務&#xff1a;編寫函數來實現獲取前N名成績的方法。 提示&#xff1a;前面的實驗沒有提供編寫自定義函數的示例&#xff0c;需要參考OpenGauss數據庫文檔學習自定義函數的使用。 score表內容如下&#xff1a; IdScore13.5223.6534.2343.8554.2363.65 --#請在BEGIN - END…

python windows 開發.exe程序筆記

import win32api import win32gui import win32con import time import tkinter as tk## pyinstaller --onefile t4.py 將python 代碼打包為windows可執行文件 .exe ## airtext 大漠 def clickGoogle():hw win32gui.FindWindow("Chrome_WidgetWin_1", "新標…

解決Redis 緩存雪崩(過期時間不一致) 和 緩存穿透(黑名單)

解決Redis 緩存雪崩&#xff08;過期時間不一致&#xff09; 和 緩存穿透&#xff08;黑名單&#xff09; public Product getdetailById(Integer id) {String key "product." id;// 查詢黑名單中是否有該keyBoolean b hashOperations.hasKey(PROODUCT_DETAIL_B…

算法 Hw7

Hw 7 Graph Algorithm 1 Edge detection2 Reachability3 Bitonic shortest paths 1 Edge detection 由 Cut Property 可知&#xff1a;如果 e 是從某個集合 S 到補集 V?S 的開銷最小的邊&#xff0c;則 e 一定所有最小生成樹中。 由 Cycle Property 可知&#xff1a;如果 e 是…

Gradle常見問題及總結

使用android studio開發項目&#xff0c;難免遇到gradle相關的錯誤&#xff0c;在此總結。 gradle插件與gradle home版本關系錯誤 參考更新 Gradle Gradle下載太慢 Index of /gradle/ (tencent.com) 是國內下載地址,手動下載對應版本即可 緩存不刷新 問題描述 maven發布…

jenkins插件之xunit

分析測試工具執行的結果&#xff0c;并圖形化&#xff0c;比如phpunit&#xff0c;phpstan,可分析junit格式的結果 安裝jenkins插件 搜索xunit并安裝 項目配置 配置 - Build Steps 您的項目 - 配置 - Build Steps, 新增 Run with timeout 超時時間根據實際情況配置 Build…

Day38 貪心算法part05

LC435無重疊區間(未掌握) 思路&#xff1a;先對數組進行排序&#xff0c;找到非重疊的區間的個數&#xff0c;然后區間的總數減去非重疊區間的個數即是需要移除的區間的個數與LC452用最少數量的箭引爆氣球類似&#xff0c;但是不同的是[1,2]和[2,3]在此題并不是重疊區間但是在…

oracle怎么處理json格式

向數據庫導入json相關jar包 loadjava -r -f -u bsuser/XXXX192.168.10.31/bsorcl json.jar 要刪除的話&#xff0c;刪除指定jar dropjava -u bsuser/XXXX192.168.10.31/bsorcl json.jar select * from user_java_classes 然后我們就可以取到json串中任意節點的值

Linux完整版命令大全(四)

2. linux系統設置命令 alias 功能說明&#xff1a;設置指令的別名。語  法&#xff1a;alias[別名][指令名稱]補充說明&#xff1a;用戶可利用alias&#xff0c;自定指令的別名。若僅輸入alias&#xff0c;則可列出目前所有的別名設置。 alias的效力僅及于該次登入的操作。…

行列視(RCV)部署在互聯網還是部署在企業內部?

行列視&#xff08;RCV&#xff09;的部署方式可以根據企業的具體需求和情況來靈活選擇。它既可以部署在互聯網上&#xff0c;也可以部署在企業內部。 對于希望實現遠程訪問、多地點協同工作或者與第三方服務集成等需求的企業&#xff0c;可以選擇將行列視&#xff08;RCV&…

Postgresql源碼(129)JIT函數中如何使用PG的類型llvmjit_types

0 總結 llvmjit_types文件分三部分 類型定義&#xff1a;llvm通過變量找到對應結構體的定義&#xff0c;在通過結構體內的偏移量宏使用成員變量。模版函數定義&#xff1a; 第一&#xff1a;AttributeTemplate被當做一個函數屬性的模板&#xff08;例如nofree、nosync等clang…

SpringBoot項目中redis序列化和反序列化LocalDateTime失敗

實體類中包含了LocalDateTime 類型的屬性&#xff0c;把實體類數據存入Redis后變成這樣&#xff1a; 此時&#xff0c;存入redis不會報錯&#xff0c;但是從redis獲取的時候&#xff0c;會報錯&#xff1a; com.fasterxml.jackson.databind.exc.InvalidDefinitionException: Ca…

Springboot項目打包:將依賴的jar包輸出到指定目錄

場景 公司要對springboot項目依賴的jar包進行升級&#xff0c;但是遇到一個問題&#xff0c;項目打包之后&#xff0c;沒辦法看到他里面依賴的jar包&#xff0c;版本到底是不是升上去了&#xff0c;沒辦法看到。 下面是項目打的jar包 我們通過反編譯工具jdgui&#xff0c;來…

VUE3和VUE2

VUE3和VUE2 上一篇文章中&#xff0c;我們對VUE3進行了一個初步的認識了解&#xff0c;本篇文章我們來進一步學習一下&#xff0c;順便看一下VUE2的寫法VUE3是否能做到兼容&#x1f600;。 一、新建組件 我們在components中新建一個組件&#xff0c;名稱為Peron&#xff0c;…