管理圖形物件的狀態

類別 Graphics 位於 GDI+ 的核心。 若要繪製任何專案,您可以取得 Graphics 物件、設定其屬性,以及呼叫其方法 DrawLineDrawImageDrawString 和 等。

下列範例會呼叫 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物件不只是提供繪圖方法,例如 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 轉換(world 和 page)。 任何仿射轉換都可以儲存在世界轉型中。 Affine 轉換包括縮放、旋轉、反映、扭曲和翻譯。 頁面轉換可用於縮放和變更單位(例如圖元到英吋)。 如需詳細資訊,請參閱 座標系統和轉換

下列範例會設定 物件的世界和頁面轉換 Graphics 。 世界轉型設定為 30 度旋轉。 頁面轉換已設定,以便將傳遞至第二個 DrawEllipse 座標的座標視為公釐,而不是圖元。 程式碼對 方法進行兩個相同的呼叫 DrawEllipse 。 世界轉換會套用至第一個 DrawEllipse 呼叫,而轉換 (world 和 page) 都會套用至第二個 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 公釐。

Illustration that shows two ellipses: rotation and pen width.

裁剪區域

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

下圖顯示裁剪的行:

Diagram that shows the limited clip region.

另請參閱