C#基础GDI绘图

GDI绘图

画笔

画笔使用Pen类表示,主要用于绘制线条,或者线条组合成的其他几何形状。

public Pen (Color color,float width)
属性 说明
Brush 获取或设置Brush,用于确定此Pen的属性
Color 获取或设置此Pen的颜色
CustomEndCap 获取或设置要在通过此Pen绘制的直线终点使用的自定义线帽
CustomStartCap 获取或设置要在通过此Pen绘制的直线起点使用的自定义线帽
DashCap 获取或设置用在短划线终点的线帽样式,这些短划线构成通过此Pen绘制的虚线
DashStyle 获取或设置用于通过此Pen绘制的虚线的样式
EndCap 获取或设置要在通过此Pen绘制的直线终点使用的线帽样式
StartCap 获取或设置在通过此Pen绘制的直线起点使用的线帽样式
Transform 获取或设置此Pen的几何变换的副本
Width 获取或设置此Pen的宽度,以用于绘图的Graphics对象为单位

画刷

画刷使用Brush类表示,主要用于填充几何图形,如将正方形和圆形填充其他颜色等。Brush类是一个抽象基类,不能进行实例化。如果要创建一个画刷对象,需要使用从Brush派生出的类。

Brush mybs = new SolidBrush(Color.Red);
HatchBrush brush = new HatchBrush(HatchStyle.DiagonalBrick,Color.Yellow);
LinearGradientBrush lgb=new LinearGradientBrush(rt,Color.Blue,Color.White, LinearGradientMode.ForwardDiagonal);
TextureBrush texture = new TextureBrush(image1);

派生类 说明
SolidBrush 定义单色画刷
HatchBrush 提供一种特定样式的图形,用来制作填满整个封闭区域的绘图效果,该类位于System.Drawing.Drawing2D命名空间下
LinerGradientBrush 提供一种渐变色彩的特效,填满图形的内部区域,该类位于System.Drawing.Drawing2D命名空间下
TextureBrush 使用图像来填充形状的内部

绘制直线

调用Graphics类中的DrawLine方法,结合Pen对象可以绘制直线。

public void DrawLine (Pen pen,Point pt1,Point pt2)
public void DrawLine (Pen pen,int x1,int y1,int x2,int y2)

绘制矩形

通过Graphics类中的DrawRectangle或者FillRectangle方法可以绘制矩形。

//DrawRectangle方法——绘制矩形
public void DrawRectangle (Pen pen,int x,int y,int width,int height)

//FillRectangle方法——填充矩形
public void FillRectangle(Brush brush,int x,int y,int width,int height)

综合例子

Graphics g = this.CreateGraphics();//创建Graphics对象
int halfWidth = this.Width / 2;
int halfHeight = this.Height / 2;
Pen pen = new Pen(Color.Blue, 2);//创建画笔
AdjustableArrowCap arrow = new AdjustableArrowCap(8, 8, false);//定义画笔线帽
pen.CustomEndCap = arrow;
g.DrawLine(pen, 50, halfHeight-20, Width - 50, halfHeight-20);//画横座标轴
g.DrawLine(pen, halfWidth, Height - 60, halfWidth, 20);//画纵座标轴
int[] saleNum = { 300, 500, 400, 450, 600, 630, 580, 650, 700, 620, 500, 480 };
Graphics g = this.CreateGraphics();//创建Graphics对象
Font font = new Font("Arial", 9, FontStyle.Regular);
Pen mypen = new Pen(Color.Blue, 1);
//绘制横向线条
int x = 100;
for (int i = 0; i < 11; i++)
{
g.DrawLine(mypen, x, 80, x, 366);
x = x + 40;
}
g.DrawLine(mypen, x - 480, 80, x - 480, 366);
//绘制纵向线条
int y = 127;
for (int i = 0; i < 10; i++)
{
g.DrawLine(mypen, 60, y, 540, y);
y = y + 24;
}
g.DrawLine(mypen, 60, y, 540, y);
#region 绘制X轴和Y轴的文字
////x轴
//String[] n = {"  一月", "  二月", "  三月", "  四月", "  五月", "  六月", "  七月",
//         "  八月", "  九月", "  十月", "十一月", "十二月"};
//x = 65;
//for (int i = 0; i < 12; i++)
//{
//    g.DrawString(n[i].ToString(), font, Brushes.Red, x, 374);//设置文字内容及输出位置
//    x = x + 40;
//}
////y轴
//String[] m = {"1000", " 900", " 800", " 700", " 600", " 500", " 400", " 300",
//         " 200", " 100", "  0"};
//y = 120;
//for (int i = 0; i < 11; i++)
//{
//    g.DrawString(m[i].ToString(), font, Brushes.Red, 25, y);//设置文字内容及输出位置
//    y = y + 24;
//}
#endregion
//显示柱状效果
x = 70;
for (int i = 0; i < 12; i++)
{
SolidBrush mybrush = new SolidBrush(Color.YellowGreen);
g.FillRectangle(mybrush, x, 370 - saleNum[i] / 4, 20, saleNum[i] / 4 - 3);
x = x + 40;
}
g.Dispose();

绘制椭圆

通过Graphics类中的DrawEllipse方法或者FillEllipse方法可以绘制椭圆。

//DrawEllipse方法——绘制椭圆
public void DrawEllipse (Pen pen,int x,int y,int width,int height)
    
//FillEllipse方法——填充椭圆
public void FillEllipse(Brush brush,int x,int y,int width,int height)

//空心椭圆
Graphics graphics = this.CreateGraphics();//创建Graphics对象
Pen myPen = new Pen(Color.Green, 3);//创建Pen对象
graphics.DrawEllipse(myPen, 50, 10, 120, 80); //绘制空心椭圆

//实心椭圆
Graphics graphics = this.CreateGraphics();//创建Graphics对象
Brush brush = new SolidBrush(Color.Red);//创建画刷对象
graphics.FillEllipse(brush, 210, 10, 120, 80);//绘制实心椭圆

绘制圆弧

通过Graphics类中的DrawArc方法,可以绘制圆弧,该方法用来绘制由一对座标、宽度和高度指定的圆弧。

public void DrawArc (Pen pen,Rectangle rect,float startAngle,float sweepAngle)

Graphics ghs = this.CreateGraphics();//创建Graphics对象
Pen myPen = new Pen(Color.Blue, 3);//创建画笔
Rectangle myRectangle = new Rectangle(70, 20, 100, 60);//定义一个Rectangle结构
//调用Graphics对象的DrawArc方法绘制圆弧
ghs.DrawArc(myPen, myRectangle, 90, 160);

绘制扇形

过Graphics类中的DrawPie方法和FillPie方法可以绘制扇形。

//DrawPie方法——绘制扇形
public void DrawPie (Pen pen,float x,float y,float width,float height,float startAngle,float sweepAngle)

//FillPie方法——填充扇形
public void FillPie(Brush brush,float x,float y,float width,float height,float startAngle,float sweepAngle)

int[] saleNum = { 300, 500, 400 };
//获取总销量和各月分别销量
int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0;
for (int i = 0; i < saleNum.Length; i++)
{
sum += saleNum[i];
if (i == 0)
threeNum = saleNum[0];
else if (i == 1)
fourNum = saleNum[1];
else
fiveNum = saleNum[2];
}
Graphics g = this.CreateGraphics();
//清空背景色
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);//实例化Pen类
//创建4个Brush对象用于设置颜色
Brush brush = new SolidBrush(Color.Black);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Wheat);
Brush brush4 = new SolidBrush(Color.Orange);
//创建Font对象用于设置字体
Font font = new Font("Courier New", 12);
int piex = 30, piey = 30, piew = 200, pieh = 200;
//3月份销量在圆中分配的角度
float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum));
//4月份销量在圆中分配的角度
float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum));
//5月份销量在圆中分配的角度
float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum));
g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1);//绘制3月份销量所占比例
g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2);//绘制4月份销量所占比例
g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3); //绘制5月份销量所占比例

绘制多边形

通过Graphics类中的DrawPolygon方法或者FillPolygon方法可以绘制多边形。多边形是有3条或更多条边的闭合图形。如果要绘制多边形,需要Graphics对象、Pen对象和Point(或PointF)对象数组。

//DrawPolygon方法——绘制多边形
 public void DrawPolygon (Pen pen,PointF[] points)
     
//FillPolygon方法——填充多边形
public void FillPolygon(Brush brush,Point[] points)

颜色

1.系统定义的颜色
系统定义的颜色使用Color结构的属性来表示,例如:

Color myColor = Color.Red;

2.用户定义的颜色
使用Color结构的FromArgb方法,其语法格式如下:

public static Color FromArgb(int red,int green,int blue)

例如,下面代码使用红色的R、G、B值自定义颜色:

Color myColor = Color.FromArgb(255, 0, 0);

3.Alpha混合处理(透明度)
Alpha使用256级灰度来记录图像中的透明度信息,主要用来定义透明、不透明和半透明区域,其中黑表示透明,白表示不透明,灰表示半透明。如果在定义颜色时,需要指定Alpha透明度,则需要使用FromArgb方法的另外一种形式,语法如下:

public static Color FromArgb(int alpha,int red,int green,int blue)

例如,使用红色的R、G、B值自定义颜色,并将透明度设置为大约50%,代码如下:

Color myColor = Color.FromArgb(128, 255, 0, 0);

字体

例如,创建一个Font对象,字体设置为“宋体”,大小设置为16,样式设置为加粗样式,代码如下:

Font myFont = new Font("宋体", 16, FontStyle.Bold);

输出文本

通过Graphics类中的DrawString方法,可以指定位置以指定的Brush和Font对象绘制指定的文本字符串。

public void DrawString(string s,Font font,Brush brush,float x,float y)

string str = "商品销售柱形图";//定义绘制的文本
Font myFont = new Font("宋体", 16, FontStyle.Bold);//创建字体对象
SolidBrush myBrush = new SolidBrush(Color.Black);//创建画刷对象
Graphics myGraphics = this.CreateGraphics();//创建Graphics对象
myGraphics.DrawString(str, myFont, myBrush, 60, 20);//在窗体的指定位置绘制文本

绘制图像

//public void DrawImage(Image image,int x,int y,int width,int height)

Image myImage = Image.FromFile("logo.jpg");//创建Image对象
Graphics myGraphics = this.CreateGraphics();//创建Graphics对象
myGraphics.DrawImage(myImage, 50, 20,90,92);//绘制图像

刷新图像

前面介绍的绘制图像的实例,都是使用窗体或者控件的CreateGraphics方法创建的Graphics绘图对象,这导致绘制的图像都是暂时的,如果当前窗体被切换或者被其他窗口覆盖,这些图像就会消失,为了使图像永久显示,可以通过在窗体或者控件的Bitmap对象上绘制图像来实现。
Bitmap对象用来封装GDI+位图,此位图由图形图像及其特性的像素数据组成,它是用于处理由像素数据定义的图像的对象。使用Bitmap对象绘制图像时,可以先创建一个Bitmap对象,并在其上绘制图像,然后再将其赋值给窗体或者控件的Bitmap对象,这样绘制出的图像就可以自动刷新,不用再使用程序来重绘图像,具体步骤如下:
(1)创建Bitmap对象时,需要使用Bitmap类的构造函数,代码如下:

Bitmap bmp = new Bitmap(120, 80);

(2)创建完Bitmap对象之后,使用创建的Bitmap对象生成Graphics绘图对象,然后调用Graphics绘图对象的相关方法绘制图像,代码如下:

Graphics g = Graphics.FromImage(bmp);
Pen myPen = new Pen(Color.Green, 3);
g.DrawEllipse(myPen, 50, 10, 120, 80);

(3)最后将Bitmap对象指定给窗体或者控件的Bitmap对象,例如,下面代码将Bitmap对象指定给窗体的BackgroundImage属性,代码如下:

this.BackgroundImage = bmp;

通过以上步骤绘制出的图像就可以自动刷新,并永久显示。

综合例子

int[] saleNum = { 300, 500, 400 };
//获取总销量和各月分别销量
int sum = 0, threeNum = 0, fourNum = 0, fiveNum = 0;
for (int i = 0; i < saleNum.Length; i++)
{
    sum += saleNum[i];
    if (i == 0)
        threeNum = saleNum[0];
    else if (i == 1)
        fourNum = saleNum[1];
    else
        fiveNum = saleNum[2];
}
//创建画图对象
int width = 400, height = 450;
Bitmap bitmap = new Bitmap(width, height);
Graphics g = Graphics.FromImage(bitmap);
//清空背景色
g.Clear(Color.White);
Pen pen1 = new Pen(Color.Red);					//实例化Pen类
//创建4个Brush对象用于设置颜色
Brush brush1 = new SolidBrush(Color.PowderBlue);
Brush brush2 = new SolidBrush(Color.Blue);
Brush brush3 = new SolidBrush(Color.Wheat);
Brush brush4 = new SolidBrush(Color.Orange);
//创建两个Font对象用于设置字体
Font font1 = new Font("Courier New", 16, FontStyle.Bold);
Font font2 = new Font("Courier New", 10);
//绘制背景图
g.FillRectangle(brush1, 0, 0, width, height);
g.DrawString("每月商品销量占比饼形图", font1, brush2, new Point(70, 20));//书写标题
int piex = 100, piey = 60, piew = 200, pieh = 200;
//3月份销量在圆中分配的角度
float angle1 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(threeNum));
//4月份销量在圆中分配的角度
float angle2 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fourNum));
//5月份销量在圆中分配的角度
float angle3 = Convert.ToSingle((360 / Convert.ToSingle(sum)) * Convert.ToSingle(fiveNum));
g.FillPie(brush2, piex, piey, piew, pieh, 0, angle1);		//绘制3月份销量所占比例
g.FillPie(brush3, piex, piey, piew, pieh, angle1, angle2);//绘制4月份销量所占比例
//绘制5月份销量所占比例
g.FillPie(brush4, piex, piey, piew, pieh, angle1 + angle2, angle3);
//绘制标识
g.DrawRectangle(pen1, 50, 300, 310, 130);				//绘制范围框
g.FillRectangle(brush2, 90, 320, 20, 10);				//绘制小矩形
g.DrawString(string.Format("3月份销量占比:{0:P2}", Convert.ToSingle(threeNum) / Convert.ToSingle(sum)), font2, brush2, 120, 320);
g.FillRectangle(brush3, 90, 360, 20, 10);
g.DrawString(string.Format("4月份销量占比:{0:P2}", Convert.ToSingle(fourNum) / Convert.ToSingle(sum)),font2, brush2, 120, 360);
g.FillRectangle(brush4, 90, 400, 20, 10);
g.DrawString(string.Format("5月份销量占比:{0:P2}", Convert.ToSingle(fiveNum) / Convert.ToSingle(sum)), font2, brush2, 120, 400);
this.BackgroundImage = bitmap;
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章