Graphics 類別是 GDI+ 的核心。 若要繪製任何項目,您需要取得 Graphics 物件、設定其屬性,以及呼叫其方法 (DrawLine、DrawImage、DrawString 等)。
下列範例會呼叫 DrawRectangle 物件的 Graphics 方法。 傳遞至 DrawRectangle 方法的第一個引數是 Pen 物件。
Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue) ' Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue); // Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100);
圖形狀態
Graphics 物件不只是提供繪圖方法,例如 DrawLine 和 DrawRectangle。 Graphics 物件也會維護圖形狀態,可分為下列類別:
質量設定
變化
裁剪區域
質量設定
Graphics 物件有數個屬性,會影響繪製項目的品質。 例如,您可以設定 TextRenderingHint 屬性來指定套用至文字的消除鋸齒類型 (如果有的話)。 影響品質的其他屬性為 SmoothingMode、CompositingMode、CompositingQuality 和 InterpolationMode。
下列範例會繪製兩個橢圓形,一個是將平滑模式設定為 AntiAlias,另一個是將平滑模式設定為 HighSpeed:
Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue)
graphics.SmoothingMode = SmoothingMode.AntiAlias
graphics.DrawEllipse(pen, 0, 0, 200, 100)
graphics.SmoothingMode = SmoothingMode.HighSpeed
graphics.DrawEllipse(pen, 0, 150, 200, 100)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue);
graphics.SmoothingMode = SmoothingMode.AntiAlias;
graphics.DrawEllipse(pen, 0, 0, 200, 100);
graphics.SmoothingMode = SmoothingMode.HighSpeed;
graphics.DrawEllipse(pen, 0, 150, 200, 100);
變化
Graphics 物件會維護套用至該 Graphics 物件繪製之所有項目的兩個轉換 (世界和頁面)。 任何仿射轉換都可以儲存在世界轉換中。 Affine 轉換包括縮放、旋轉、反映、扭曲和翻譯。 頁面轉換可用於縮放和變更單位(例如圖元到英吋)。 如需詳細資訊,請參閱座標系統和轉換。
下列範例會設定 Graphics 物件的世界和頁面轉換。 世界轉型設定為 30 度旋轉。 頁面轉換已設定,以便將傳遞至第二個 DrawEllipse 的座標視為公厘,而不是像素。 程式碼對 DrawEllipse 方法進行兩次相同的呼叫。 世界轉換會套用至第一個 DrawEllipse 呼叫,而兩個轉換 (世界和頁面) 會套用至第二個 DrawEllipse 呼叫。
Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Red)
graphics.ResetTransform()
graphics.RotateTransform(30) ' world transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50)
graphics.PageUnit = GraphicsUnit.Millimeter ' page transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50)
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Red);
graphics.ResetTransform();
graphics.RotateTransform(30); // world transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50);
graphics.PageUnit = GraphicsUnit.Millimeter; // page transformation
graphics.DrawEllipse(pen, 0, 0, 100, 50);
下圖顯示兩個橢圓。 請注意,30 度旋轉是關於座標系統的原點(工作區左上角),而不是橢圓形的中心。 另請注意,筆觸寬度為 1 的時候,第一個橢圓形的筆觸為 1 像素,第二個橢圓形的筆觸則為 1 mm。
裁剪區域
Graphics 物件會維護套用至該 Graphics 物件所繪製之所有項目的裁剪區域。 您可以呼叫 SetClip 方法來設定裁剪區域。
下列範例會透過將兩個矩形合併,建立十字形區域。 該區域會指定為 Graphics 物件的裁剪區域。 然後,程式碼會繪製兩條線,確保其位於裁剪區域的內部。
Dim graphics As Graphics = e.Graphics
' Opaque red, width 5
Dim pen As New Pen(Color.Red, 5)
' Opaque aqua
Dim brush As New SolidBrush(Color.FromArgb(255, 180, 255, 255))
' Create a plus-shaped region by forming the union of two rectangles.
Dim [region] As New [Region](New Rectangle(50, 0, 50, 150))
[region].Union(New Rectangle(0, 50, 150, 50))
graphics.FillRegion(brush, [region])
' Set the clipping region.
graphics.SetClip([region], CombineMode.Replace)
' Draw two clipped lines.
graphics.DrawLine(pen, 0, 30, 150, 160)
graphics.DrawLine(pen, 40, 20, 190, 150)
Graphics graphics = e.Graphics;
// Opaque red, width 5
Pen pen = new Pen(Color.Red, 5);
// Opaque aqua
SolidBrush brush = new SolidBrush(Color.FromArgb(255, 180, 255, 255));
// Create a plus-shaped region by forming the union of two rectangles.
Region region = new Region(new Rectangle(50, 0, 50, 150));
region.Union(new Rectangle(0, 50, 150, 50));
graphics.FillRegion(brush, region);
// Set the clipping region.
graphics.SetClip(region, CombineMode.Replace);
// Draw two clipped lines.
graphics.DrawLine(pen, 0, 30, 150, 160);
graphics.DrawLine(pen, 40, 20, 190, 150);
下圖顯示裁剪的行: