Hopes

Start Here..

 

C# GDI+与图形编程

C# GDI+与图形编程

2011-11-08 13:12 by Mr.Xer, 432 阅读, 1 评论, 收藏编辑

GDI+与图形编程

GDI+的基本概念

GDI+的常用对象,包括Graphics、Font、Brush、Pen等对象的创建和使用

常用图形的绘制

Color结构、Point结构和Rectangle结构

 

1.GDI+的概念

      GDI+是GDI(Graphics Device Interface,图形设备接口)的改进产品。

2.GDI+的绘图命名空间

      用户所使有的GDI+函数都保存在System.Drawing.d11程序集中。其中包括System.Drawing、System.Drawing.Text、System.Drawing.Printing、System.Drawing.Imaging、System.Drawing.Drawing2D和System.Drawing.Design等命名空间。   

 

 

 

Graphics对象

(1)利用窗体或控件的Paint事件的参数PaintEventArgs创建Graphics对象。

(2)使用窗体或控件的CreateGraphics方法

       Graphics  g=this.CreateGraphics();

(3)使用Image的派生类创建Graphics对象。使用Image的任何派生类均可以生成相应的Graphics对象,这种方法一般适用于在C#中对图像进行处理的场合。

              Bitmap b=new Bitmap("Mybmp.bmp");

Graphics g=Graphics.FromImage(b);

 

Pen对象

Pen类的构造函数有四种,使用方法如下。

(1)创建某一颜色的Pen对象:public Pen(Color)

(2)创建某一刷子样式的Pen对象:public Pen(Brush)

(3)创建某—刷子样式并具有相应宽度的Pen对象:public Pen(Brush,float)

(4)创建某一颜色和相应宽度的Pen对象:public Pen(Color,float)

Pen对象的常用属性

(1)Alignment属性:用来获取或设置此Pen对象的对齐方式。

(2)Color属性:用来获取或设置此Pen对象的颜色。

(3)Width属性:用来获取或设置此Pen对象的宽度。

(4)DashStyle属性:用来获取或设置通过此Pen对象绘制的虚线的样式。

(5)DashCap属性:用来指定虚线两端风格,是一个DashCap枚举型的值。

(6)StartCap属性:用来获取或设置通过此Pen对象绘制的直线起点的帽样式。

(7)EndCap属性:用来获取或设置通过此Pen对象绘制的直线终点的帽样式。

(8)PenType属性:用来获取用此Pen对象绘制的直线的样式。 

 

 

Font对象


 

 

Brush对象

1.SolidBrush画刷

   SolidBrush类用来定义单一颜色的Brush,其构造函数如下。

   public SolidBrush(Color.Color)

2.HatchBrush画刷

      HatchBrush类的构造函数有两种,分别如下:  

[格式1]:public HatchBrush(HatchStyle, Color);

[格式2]:public HatchBrush(HatchStyle, Color, Color); HatchBrush画刷具有三个属性,分别如下:

(1)BackgroundColor属性:获取此HatchBrush 对象的背景色。

(2)ForegroundColor属性:获取此HatchBrush 对象的前景色。

(3)HatchStyle属性:获取此HatchBrush 对象的阴影样式。

3.LinearGradientBrush画刷

LinearGradientBrush类的构造函数有多种格式,最常用的格式如下。

    public LinearGradientBrush(Point1, Point2, Color1, Color2);

 

 

常用图形的绘制方法

1.画直线

      [格式1]:public void DrawLine(Pen pen,int x1,int y1,int x2,int y2);

      [格式2]:public void DrawLine(Pen pen,Point pt1,Point pt2);

2.画椭圆

[格式1]:public void DrawEllipse( Pen pen, Rectangle rect);

[格式2]:public void DrawEllipse(Pen pen,int x,int y,int width, int height);

3.绘制圆弧

[格式1]:public void DrawArc(Pen pen,Rectangle rect,float startAngle,float sweepAngle);

[格式2]:public void DrawArc(Pen pen,int x,int y,int width,int height,int startAngle,int sweepAngle);

4.画扇形图

使用Graphics对象的DrawPie方法可以绘制扇形图,所谓扇形图其实就是把一段圆弧的两个端点与圆心相连。DrawPie方法的格式与DrawArc方法基本一致。

5.画矩形

[格式1]: public void DrawRectangle( Pen pen, Rectangle rect);

[格式2]:public void DrawRectangle(Pen pen,int x,int y,int width,int height);

6.画Bezier曲线

[格式1]:public void DrawBezier(Pen pen,Point pt1,Point pt2,Point pt3,Point pt4);

[格式2]:public void DrawBezier(Pen pen,float x1,float y1,float x2,float y2,float x3,float y3,float x4,float y4);

7.画多边形

[格式1]:public void DrawPolygon(Pen pen,  Point[] points);

[格式2]:public void DrawPolygon(Pen pen, PointF[] points);

8.绘制闭合曲线

[格式1]:public void DrawClosedCurve(Pen pen,Point[] points);

[格式2]:public void DrawClosedCurve(Pen pen,Point[] points,float tension,FillMode fillmode);

9.绘制非闭合曲线

[格式]:public void DrawCurve( Pen pen,Point[] points);

10.绘制路径

[格式]:public void DrawPath(Pen pen,GraphicsPath path);

11.填充椭圆

[格式1]:public void FillEllipse(Brush brush, Rectangle rect);

[格式2]:public void DrawEllipse(Brush brush,int x,int y,int width, int height);

12.填充矩形

[格式1]: public void FillRectangle( Brush brush, Rectangle rect);

[格式2]:public void FillRectangle(Brush brush,int x,int y,int width,int height);

13.填充饼图

[格式1]:public void FillPie(Brush brush,Rectangle rect,float startAngle,float sweepAngle)

[格式2]:public void FillPie(Brush brush,int x,int y,int width,int height,int startAngle,int sweepAngle);

分类: C#

posted on 2012-08-19 14:36 ** 阅读(488) 评论(0)  编辑  收藏


只有注册用户登录后才能发表评论。


网站导航:
 

导航

统计

公告

你好!

常用链接

留言簿(2)

随笔档案

文章分类

文章档案

新闻档案

相册

收藏夹

C#学习

友情链接

搜索

最新评论

阅读排行榜

评论排行榜