如何:繪製填滿材質的線條

您可以使用紋理繪製線條,而不是使用純色繪製線條。 若要使用紋理繪製線條和曲線,請建立 TextureBrush 物件,並將該 TextureBrush 物件傳遞至建 Pen 構函式。 與紋理筆刷相關聯的點陣圖是用來貼圖平面(可見),而當畫筆繪製線條或曲線時,畫筆筆的筆劃會發現圖格紋理的特定圖元。

範例

下列範例會從 檔案 Texture1.jpg 建立 Bitmap 物件。 該點陣圖用來建構 TextureBrush 物件,而 TextureBrush 物件則用來建構 Pen 物件。 的 DrawImage 呼叫會繪製點陣圖,其左上角位於 (0, 0)。 的呼叫 DrawEllipsePen 使用 物件繪製紋理橢圓形。

下圖顯示點陣圖和紋理橢圓形:

Screenshot that shows the bitmap and the textured ellipse.

Bitmap bitmap = new Bitmap("Texture1.jpg");
TextureBrush tBrush = new TextureBrush(bitmap);
Pen texturedPen = new Pen(tBrush, 30);

e.Graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height);
e.Graphics.DrawEllipse(texturedPen, 100, 20, 200, 100);
Dim bitmap As New Bitmap("Texture1.jpg")
Dim tBrush As New TextureBrush(bitmap)
Dim texturedPen As New Pen(tBrush, 30)

e.Graphics.DrawImage(bitmap, 0, 0, bitmap.Width, bitmap.Height)
e.Graphics.DrawEllipse(texturedPen, 100, 20, 200, 100)

編譯程式碼

建立 Windows Form 並處理表單的事件 Paint 。 將上述程式碼貼到 Paint 事件處理常式中。 將 取代 Texture.jpg 為您系統上有效的映射。

另請參閱