管理圖形物件的狀態

Graphics 類別是 GDI+ 的核心。 若要繪製任何項目,您需要取得 Graphics 物件、設定其屬性,以及呼叫其方法 (DrawLineDrawImageDrawString 等)。

下列範例會呼叫 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)  
Graphics graphics = e.Graphics;  
Pen pen = new Pen(Color.Blue);  // Opaque blue  
graphics.DrawRectangle(pen, 10, 10, 200, 100);  

圖形狀態

Graphics 物件不只是提供繪圖方法,例如 DrawLineDrawRectangleGraphics 物件也會維護圖形狀態,可分為下列類別:

  • 品質設定

  • 轉換

  • 裁剪區域

品質設定

Graphics 物件有數個屬性,會影響繪製項目的品質。 例如,您可以設定 TextRenderingHint 屬性來指定套用至文字的消除鋸齒類型 (如果有的話)。 影響品質的其他屬性為 SmoothingModeCompositingModeCompositingQualityInterpolationMode

下列範例會繪製兩個橢圓形,一個是將平滑模式設定為 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 物件繪製之所有項目的兩個轉換 (世界和頁面)。 任何仿射轉換都可以儲存在世界轉型中。 仿射轉換包括縮放、旋轉、反射、扭曲和翻譯。 頁面轉換可用於縮放和變更單位 (例如像素到英吋)。 如需詳細資訊,請參閱座標系統和轉換

下列範例會設定 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 公釐。

顯示兩個橢圓形的圖例:旋轉和手寫筆寬度。

裁剪區域

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

下圖顯示裁剪的行:

顯示有限剪輯區域的圖表。

另請參閱