C# GDI+绘图
[toc]
1、坐标系
GDI+使用三个坐标空间:世界、页面、设备;
世界:世界坐标适用于建立特殊图形世界模型的坐标系,也是在.net Framework中传递给方法的坐标系;
页面:页面坐标系是指绘图图面(如窗体或控件)使用的坐标系;
设备坐标系是在其上进行汇总的物理设备(如屏幕或纸张)所使用的坐标系。
2、主要内容
2.1 画笔
使用Pen类表示,主要用于绘制线条,或者线条组合成的其它集合形状;
public Pen(Color color, float width)
2.2 绘制直线
//创建GDI对象
Graphics graphics = this.CreateGraphics();
//创建画笔对象
Pen pen = new Pen(Brushes.Black, 5);
//创建两个点
Point p1 = new Point(20, 20);
Point p2 = new Point(100, 100);
graphics.DrawLine(pen, p1, p2);
2.3 绘制矩形
Graphics graphics = this.CreateGraphics();
Pen pen = new Pen(Brushes.Red, 5);
Rectangle rectangle = new Rectangle(10, 10, 300, 300);
graphics.DrawRectangle(pen, rectangle);
2.4 绘制扇形
Graphics graphics = this.CreateGraphics();
Pen pen = new Pen(Brushes.Red, 5);
RectangleF rectangleF = new RectangleF(10, 10, 300, 300);
graphics.DrawPie(pen, rectangleF, 0, 50);
2.5 绘制文本
Graphics graphics = this.CreateGraphics();
string text = "hell0";
Font font = new Font("宋体",15,FontStyle.Underline);
graphics.DrawString(text,font, Brushes.Blue, new Point(10, 10));
3、画刷
GDI+提供了下列五种画刷。
SolidBrush(实色刷)
HatchBrush(阴影刷)
TextureBrush(纹理刷)
LinearGradientBrush(渐变刷)
PathGradientBrush(路径刷)
在GDI+ for VCL中,各种画刷在原C++类类名基础上加了TGp前缀,均派生于TGpBrush,其中的TGpSolidBrush和TGpHatchBrush相当于VCL中传统的GDI的画刷TBrush。
4、画图效果
4.1 灯光效果
image-20210121221631027
GraphicsPath graphicsPath = new GraphicsPath();
graphicsPath.AddEllipse(new Rectangle(0, 0, 200, 200));
PathGradientBrush pathGradientBrush = new PathGradientBrush(graphicsPath);
pathGradientBrush.CenterColor = Color.FromArgb(255, 232, 3);
pathGradientBrush.CenterPoint = new PointF(100, 100);
pathGradientBrush.SurroundColors = new Color[] { Color.Transparent };
l_graphics.SmoothingMode = SmoothingMode.AntiAlias;
l_graphics.FillEllipse(pathGradientBrush, new Rectangle(0, 0, 200, 200));
graphicsPath.Dispose();
pathGradientBrush.Dispose();