HOW TO:設定畫筆寬度和對齊
更新:2007 年 11 月
建立 Pen 時,您可以提供畫筆寬度,做為建構函式的其中一個引數。您也可以使用 Pen 類別的 Width 屬性來變更畫筆寬度。
理論線條 (Theoretical Line) 的寬度為 0。當您繪製寬度為 1 個像素的線條時,像素會對齊理論線條的中央。如果繪製的線條寬度超過 1 個像素,像素會對齊理論上線條的中央,或顯示在理論上線條的其中一側。您可以設定 Pen 的畫筆對齊屬性,以決定該畫筆所繪製的像素相對於理論線條的位置。
下列程式碼範例中所出現的 Center 值、Outset 值和 Inset 值是 PenAlignment 列舉型別的成員。
下列程式碼範例繪製兩次線條:一次是使用寬度為 1 的黑色畫筆,另一次是使用寬度為 10 的綠色畫筆。
若要改變畫筆的寬度
請將 Alignment 屬性值設定為 Center (預設值),以指定綠色畫筆繪製的像素對齊理論線條的中央。下圖顯示的是產生的線條。
下列程式碼範例繪製兩次矩形:一次是使用寬度為 1 的黑色畫筆,另一次是使用寬度為 10 的綠色畫筆。
Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1) Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10) greenPen.Alignment = PenAlignment.Center ' Draw the line with the wide green pen. e.Graphics.DrawLine(greenPen, 10, 100, 100, 50) ' Draw the line with the thin black pen. e.Graphics.DrawLine(blackPen, 10, 100, 100, 50)
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1); Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10); greenPen.Alignment = PenAlignment.Center; // Draw the line with the wide green pen. e.Graphics.DrawLine(greenPen, 10, 100, 100, 50); // Draw the line with the thin black pen. e.Graphics.DrawLine(blackPen, 10, 100, 100, 50);
若要變更畫筆對齊
請將 Alignment 屬性值設定為 Center,以指定綠色畫筆繪製的像素對齊矩形界限的中央。
下圖顯示的是產生的矩形。
Dim blackPen As New Pen(Color.FromArgb(255, 0, 0, 0), 1) Dim greenPen As New Pen(Color.FromArgb(255, 0, 255, 0), 10) greenPen.Alignment = PenAlignment.Center ' Draw the rectangle with the wide green pen. e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50) ' Draw the rectangle with the thin black pen. e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50)
Pen blackPen = new Pen(Color.FromArgb(255, 0, 0, 0), 1); Pen greenPen = new Pen(Color.FromArgb(255, 0, 255, 0), 10); greenPen.Alignment = PenAlignment.Center; // Draw the rectangle with the wide green pen. e.Graphics.DrawRectangle(greenPen, 10, 100, 50, 50); // Draw the rectangle with the thin black pen. e.Graphics.DrawRectangle(blackPen, 10, 100, 50, 50);
若要建立內凹畫筆
依照下列方式修改上述程式碼範例中的第三個陳述式,便可變更綠色畫筆的對齊方式:
greenPen.Alignment = PenAlignment.Inset
greenPen.Alignment = PenAlignment.Inset;
現在,寬的綠色線條中的像素會顯示在矩形的內部,如下圖所示。