圖形物件的狀態
Graphics 類別 (Class) 是 GDI+ 的核心。若要繪製任何項目,需要先取得 Graphics 物件、設定它的屬性,然後呼叫它的方法 (DrawLine、DrawImage、DrawString 等)。
下列範例會呼叫 Graphics 物件的 DrawRectangle 方法。傳遞至 DrawRectangle 方法的第一個引數是 Pen 物件。
Dim graphics As Graphics = e.Graphics
Dim pen As New Pen(Color.Blue) ' Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100)
[C#]
Graphics graphics = e.Graphics;
Pen pen = new Pen(Color.Blue); // Opaque blue
graphics.DrawRectangle(pen, 10, 10, 200, 100);
圖形狀態
Graphics 物件不僅會提供繪圖方法,例如 DrawLine 和 DrawRectangle,Graphics 物件也會保留圖形狀態,圖形狀態可分為下列幾個類別:
- 品質設定
- 變換
- 裁剪區域 (Clipping Region)
品質設定
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)
[C#]
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 物件所繪製所有項目的變換 (實際和頁面)。任何仿射變換都可以儲存在自然變換中。仿射變換包括縮放、旋轉、反射、傾斜和轉換。畫面變換可用來縮放和變更單位 (例如,將像素變成英吋)。如需詳細資訊,請參閱座標系統和變換。
下列範例會設定 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)
[C#]
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 公釐。
裁剪區域
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)
[C#]
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);
下圖顯示的是裁剪的線條。