在 C# 中,Graphics
类是 System.Drawing
命名空间的一部分,它提供了一组方法和属性,用于在 Windows Forms 应用程序中进行二维绘图。Graphics
对象可以绘制文本、线条、曲线、形状和图像,并可以对它们进行变换和剪辑。
Graphics
类的一些常用功能和方法:
1.绘制线条:
DrawLine(Pen pen, int x1, int y1, int x2, int y2)
:使用指定的 Pen
对象绘制直线。
DrawLines(Pen pen, Point[] points)
:使用指定的 Pen
对象和点数组绘制一系列连续的线条。
public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel2.Paint += Panel2_Paint;
}
//DrawLine(Pen pen, int x1, int y1, int x2, int y2)
private void Panel1_Paint(object sender, PaintEventArgs e)
{//1.创建图形(画布,画板)Graphics g = e.Graphics;//2.设置绘制参数()SetQuality(g);//3.开始绘制Pen pen = new Pen(Color.Red, 10F);Point pt1 = new Point(50, 50);Point pt2 = new Point(100, 50);g.DrawLine(pen, pt1, pt2);//注意:起点的坐标,考虑画笔的宽度Pen pen1 = new Pen(Color.Blue, 10F);Point pt3 = new Point(100 + 5, 50 - 5);Point pt4 = new Point(100 + 5, 100);g.DrawLine(pen1, pt3, pt4);
}
//DrawLines(Pen pen, Point[] points)
private void Panel1_Paint(object sender, PaintEventArgs e)
{Pen pen = new Pen(Color.Red, 10F);Point[] points = new Point[]{new Point(80,150),new Point(80,20),new Point(20,100),new Point(120,100),};g.DrawLines(pen, points);
}
2.绘制形状:
DrawRectangle(Pen pen, int x, int y, int width, int height)
:使用指定的 Pen
对象绘制矩形。
DrawEllipse(Pen pen, int x, int y, int width, int height)
:使用指定的 Pen
对象绘制椭圆。
DrawPolygon(Pen pen, Point[] points)
:使用指定的 Pen
对象和点数组绘制多边形。
public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel2.Paint += Panel2_Paint;this.panel3.Paint += Panel3_Paint;
}
//矩形DrawRectangle(Pen pen, int x, int y, int width, int height)
private void Panel1_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,100,100);g.DrawRectangle(pen1, rect);
}
//椭圆DrawEllipse(Pen pen, int x, int y, int width, int height)
private void Panel2_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,80,100);g.DrawEllipse(pen1, rect);//椭圆
}
//多边形DrawPolygon(Pen pen, Point[] points)
private void Panel3_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Point[] points = new Point[]{new Point(80,20),new Point(20,100),new Point(120,100),};g.DrawPolygon(pen1, points);
}
3.填充形状:
FillRectangle(Brush brush, int x, int y, int width, int height)
:使用指定的 Brush
对象填充矩形。
FillEllipse(Brush brush, int x, int y, int width, int height)
:使用指定的 Brush
对象填充椭圆。
FillPolygon(Brush brush, Point[] points)
:使用指定的 Brush
对象填充多边形。
public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel2.Paint += Panel2_Paint;this.panel3.Paint += Panel3_Paint;
}
//FillRectangle(Brush brush, int x, int y, int width, int height)
private void Panel1_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,100,100);Brush brush = new SolidBrush(Color.Yellow);g.FillRectangle(brush, rect);g.DrawRectangle(pen1, rect);//矩形
}
//FillEllipse(Brush brush, int x, int y, int width, int height)
private void Panel2_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Rectangle rect = new Rectangle(20,20,80,100);Brush brush = new SolidBrush(Color.Yellow);g.FillEllipse(brush, rect);g.DrawEllipse(pen1, rect);//椭圆
}
//FillPolygon(Brush brush, Point[] points)
private void Panel3_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);Pen pen1 = new Pen(Color.Red, 10F);Point[] points = new Point[]{new Point(80,20),new Point(20,100),new Point(120,100),};Brush brush = new SolidBrush(Color.Yellow);g.DrawPolygon(pen1, points);g.FillPolygon(brush,points);
}
4.绘制文本:
DrawString(String s, Font font, Brush brush, float x, float y)
:在指定位置绘制文本字符串。
public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;
}private void Panel1_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string s = "相寻梦里路,飞雨落花中";Font font = new Font("华文琥珀",20F);Brush brush = new SolidBrush(Color.Pink);g.DrawString(s,font,brush,10,10);}
5.图像处理:
DrawImage(Image image, Point point)
:在指定位置绘制图像。
DrawImage(Image image, Rectangle rect)
:在指定矩形区域内绘制图像。
public Form1()
{InitializeComponent();this.panel1.Paint += Panel1_Paint;this.panel1.Paint += Panel2_Paint;
}
private void Panel1_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);Point point = new Point( 0, 0);g.DrawImage(img, point);//在指定位置绘制图像img.Dispose(); // 释放图像资源
}
private void Panel2_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/2.png");Image img = Image.FromFile(path);Rectangle rect = new Rectangle(0,0,300,240);g.DrawImage(img, rect);img.Dispose(); // 释放图像资源
}
6.变换和剪辑:
TranslateTransform(float dx, float dy)
:对当前的坐标系统进行平移变换。
ScaleTransform(float sx, float sy)
:对当前的坐标系统进行缩放变换。
RotateTransform(float angle)
:对当前的坐标系统进行旋转变换。
SetClip(Rectangle rect)
:设置当前的剪辑区域。
代码在下面
1.平移变换 2.缩放变换 3. 旋转变换
7.获取信息:
VisibleClipBounds
:获取当前剪辑区域的边界。
IsVisible(Point point)
:判断一个点是否在可见区域内。
代码
using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.IO;
using System.Windows.Forms;
namespace 图形变换和剪辑
{public partial class Form1 : Form{public Form1(){InitializeComponent();Panel panel1 = new Panel{Size = new Size(400, 400),Location = new Point(50, 50),BackColor = Color.Green,};this.Controls.Add(panel1);panel1.Paint += Panel1_Paint; 平移变换panel1.Paint += Panel2_Paint; // 缩放变换panel1.Paint += Panel3_Paint; // 旋转变换panel1.Paint += Panel4_Paint; // 设置剪辑区域}private void Panel1_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);g.TranslateTransform(100, 0);g.DrawImage(img, new Point(0,0));}private void Panel2_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);g.ScaleTransform(1.5F,1.5F);//缩放g.DrawImage(img, new Point(-100, -100));}private void Panel3_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/1.png");Image img = Image.FromFile(path);g.RotateTransform(45); // 旋转变换g.DrawImage(img, new Point(0, 0));}private void Panel4_Paint(object sender, PaintEventArgs e){Graphics g = e.Graphics;SetQuality(g);string path = Path.Combine(Environment.CurrentDirectory, "../../image/2.png");Image img = Image.FromFile(path);Rectangle clipRect = new Rectangle(50, 50, 300, 300);g.SetClip(clipRect);//设置当前编辑区// 获取信息RectangleF visibleClipBounds = g.VisibleClipBounds;Console.WriteLine($"Visible Clip Bounds: {visibleClipBounds}");Point testPoint = new Point(100, 100);bool isVisible = g.IsVisible(testPoint);Console.WriteLine($"Point {testPoint} is visible: {isVisible}");}private static void SetQuality(Graphics g){g.SmoothingMode = SmoothingMode.AntiAlias;g.CompositingQuality = CompositingQuality.HighQuality;g.InterpolationMode = InterpolationMode.HighQualityBicubic;}}
}
SetQuality()
SetQuality
方法是一个自定义方法,它不是 System.Drawing
命名空间的一部分。这个方法通常用于设置 Graphics
对象的属性,以提高绘制质量,特别是在进行图形变换、绘制文本或图像时。
SetQuality
方法设置了以下几个关键属性:
SmoothingMode
:设置为SmoothingMode.AntiAlias
,以启用抗锯齿,使线条和曲线更平滑。InterpolationMode
:设置为InterpolationMode.HighQualityBicubic
,以在图像缩放时使用高质量的双三次插值算法。PixelOffsetMode
:设置为PixelOffsetMode.HighQuality
,以减少图像旋转和大字体文本时的像素偏移。TextRenderingHint
:设置为TextRenderingHint.AntiAliasGridFit
,以提高文本渲染的质量和清晰度。CompositingQuality
:设置为CompositingQuality.HighQuality
,以确保在合成图像时使用高质量的算法。
using System.Drawing;
using System.Drawing.Drawing2D;
public void SetQuality(Graphics g)
{// 设置高质量渲染模式g.SmoothingMode = SmoothingMode.AntiAlias; // 抗锯齿// 设置高质量的插值模式g.InterpolationMode = InterpolationMode.HighQualityBicubic; // 高质量双三次插值// 设置高质量的像素偏移模式g.PixelOffsetMode = PixelOffsetMode.HighQuality; // 高质量像素偏移// 设置高质量的路径渐变g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; // 文本抗锯齿// 设置图形对象的线性变换和旋转变换的精度g.CompositingQuality = CompositingQuality.HighQuality; // 高质量合成
}// 在 Paint 事件处理程序中使用 SetQuality 方法
private void MyControl_Paint(object sender, PaintEventArgs e)
{Graphics g = e.Graphics;SetQuality(g); // 应用高质量设置// 现在使用 g 绘制文本、线条、形状等g.DrawString("Hello, World!", new Font("Arial", 16), Brushes.Black, new PointF(10, 10));
}