管理 Graphics 对象的状态

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 状态

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

下面的插图显示这两条剪辑过的直线。

受限剪辑区域

请参见

概念

使用嵌套的 Graphics 容器

其他资源

Windows 窗体中的图形和绘制