一、画笔
- 创建画笔
Pen pen = new Pen(Color.Red); /*参数为画笔颜色*/
- 画笔宽度
pen.Width = 0.5f; /*设置画笔宽度*/
- 画笔类型
pen.DashStyle = DashStyle.DashDotDot; /*点画线*/
注:使用时要使用名字空间 using System.Drawing.Drawing2D.
- 画笔颜色
pen.Color = Color.Red;
二、画刷
- 创建画刷
Brush brush = new SolidBrush(Color.Red); /*画刷类型为Solid*/
- 标准颜色画刷类
Brushes类集合了所有标准颜色的画刷,其每个成员对象均为Brush类型.使用示例如下:
grp.FillEllipse(Brushes.Aqua,rectf);
- 创建渐变画刷
LinearGradientBrush类为渐变画刷类,使用示例如下:
Brush brush = null;
brush = new LinearGradientBrush(this.panLeft.ClientRectangle,
Color.Blue,Color.Yellow,angle);
参数说明:
panLeft为画布Name,angle为float类型变量,表示渐变角度.
- 创建纹理画刷
TextureBrush类为纹理画刷类,使用示例如下:
Brush brush = null;
brush = new TextureBrush(brushImage);
参数说明:
brushimage为Image类型变量.
三、画布
- 创建画布控件对象
Graphics grp = panShow.CreateGraphics(); /* panShow 为对应的panel控件的名字*/
- 清除画布内容
grp.Clear(panShow.BackColor); /*参数为填充画布的颜色*/
- 绘制直线
使用函数DrawLine绘制直线,示例如下:
grp.DrawLine(pen, startPoint, endPoint); /*grp为Graphics 类型变量*/
参数说明:
第一个参数为画笔,第二个参数为直线起点,第三个参数为直线终点.
- 绘制多条直线
使用函数DrawLines绘制多条直线,示例如下:
List<PointF> points = new List<PointF>(); /*点列表*/
for (int index = 0; index < 10; index++)
{
float x = index * index / 2f;
float y = 2 * x * x + 3 * x + 5;
PointF point = new PointF(x,y);
points.Add(point); /*将点加入点列表中*/
}
Graphics grp = panDraw.CreateGraphics(); /*panDraw为画布名字*/
grp.DrawLines(pen,points.ToArray());
参数说明:
第一个参数为画笔,第二个参数为数组(PointF[]).
- 绘制椭圆(包括圆)
grp.DrawEllipse(Pens.Blue,largeRect); /*第一个参数为画笔,第二个参数为外接矩形*/
- 填充椭圆
grp.FillEllipse(brush,rect); /*第二个参数为椭圆的外接矩形*/
- 获取画布的宽与高
float width = this.panDraw.Width; /* panDraw 为控件名字*/
float height = this.panDraw.Height; /* panDraw 为控件名字*/
- 设置图像质量(光滑度)
可以使用SmoothingMode来设置所绘制图像的质量,使用示例如下:
grp.SmoothingMode = SmoothingMode.HighQuality; /*设置为高度平滑*/
注:使用时候包含using System.Drawing.Drawing2D;
- 绘制字符串
使用函数DrawString进行字符串的绘制,使用示例如下:
grp.DrawString(“C#程序”,font,Brushes.Black,point);
参数说明:
第一个参数为要绘制的字符串;第二个参数为创建的画笔,第三个参数为画刷,最后一个参数为显示字符串的位置.
- 图形双缓冲区
为了避免直接在画布上绘图出现的闪烁现象,可以使用图形双缓冲区。其原理就是先在缓冲区将图形绘制完成,再将其显示出来。使用方式如下:
①定义一个图形双缓冲区对象:
private BufferedGraphics bgrp = null; /*在该窗体对应的类中定义一个成员变量*/
②在窗体加载函数中做如下操作:
Graphics grp = this.CreateGraphics();
this.bgrp = BufferedGraphicsManager.Current.Allocate(grp,this.ClientRectangle);
说明:BufferedGraphicsManager.Current.Allocate函数作用是创建指定大小的缓冲区,第一个参数为目标画布(或者画板).
注:如果窗体大小可变时候,在窗体大小改变响应函数中也要加上上面的语句.
③使用this.bgrp.Graphics对象进行一切相关的绘图操作:
下面给出一些示例:
this.bgrp.Graphics.Clear(this.BackColor); /*清空画布*/
this.bgrp.Graphics.FillEllipse(Brushes.Aqua,rectf); /*填充椭圆*/
this.bgrp.Graphics.DrawLine(pen,startPoint,endPoint); /*绘制直线*/
进行其它绘图操作都是使用this.bgrp.Graphics对象.
④绘图完成后,要在画布上显示图片:
this.bgrp.Render(); /*绘图完成后要显示绘制图形*/
四、常用图形
- Rectangle:矩形
- Point:点
创建示例:
RectangleF rect = new RectangleF();
private PointF centerPoint = PointF.Empty; /*定义一个空的对象*/
五、颜色
- ARGB颜色类(Color)
(a)Color类封装了系统定义的ARGB颜色,使用示例如下:
Pen pen = new Pen(Color.White);
pen.Color = Color.Red;
(b)可以通过成员函数FromArgb生成一个Color类型对象,使用示例如下:
Color color = Color.FromArgb(120,120,0);
六、图像
- Image类
①加载图片:
Image brushImage = null; /*定义变量*/
brushImage = Image.FromFile(Application.StartupPath+"\\Image\\brush.jpg");/*加载图片*/
注:Application.StartupPath为当前应用程序运行目录,Image为新建的目录,btush.jpg为图片名字.
- 字体
Font myFont=new Font("宋体", 40);
FontConverter fc = new FontConverter();
string fontInfo = fc.ConvertToInvariantString(myFont);//将Font转化成字符串
if (!(fontInfo==null || fontInfo.Equals(string.Empty)))
{
myFont = (Font)fc.ConvertFromString(fontInfo);//将字符串转化成Font对象
}