该 Graphics 类是 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)
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和CompositingQualityInterpolationMode。
以下示例绘制了两个椭圆,一个的平滑模式设置为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);
下图显示了被切割的线条: