Visual C# .NET-GDI+与图形编程

上传人:TH****3P 文档编号:122012492 上传时间:2020-02-29 格式:PPT 页数:45 大小:505KB
返回 下载 相关 举报
Visual C# .NET-GDI+与图形编程_第1页
第1页 / 共45页
Visual C# .NET-GDI+与图形编程_第2页
第2页 / 共45页
Visual C# .NET-GDI+与图形编程_第3页
第3页 / 共45页
亲,该文档总共45页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述

《Visual C# .NET-GDI+与图形编程》由会员分享,可在线阅读,更多相关《Visual C# .NET-GDI+与图形编程(45页珍藏版)》请在金锄头文库上搜索。

1、第11章GDI 与图形编程 本章要点 GDI 的基本概念 GDI 的常用对象 包括Graphics Font Brush Pen等对象的创建和使用 常用图形的绘制 Color结构 Point结构和Rectangle结构 11 1循序渐进学理论 11 1 1GDI 与绘图命名空间1 GDI 的概念GDI 是GDI GraphicsDeviceInterface 图形设备接口 的改进产品 2 GDI 的绘图命名空间用户所使有的GDI 函数都保存在System Drawing d11程序集中 其中包括System Drawing System Drawing Text System Drawing

2、Printing System Drawing Imaging System Drawing Drawing2D和System Drawing Design等命名空间 11 1 2Graphics对象 创建Graphics对象的方法 般有三种 1 利用窗体或控件的Paint事件的参数PaintEventArgs创建Graphics对象 利用该方式创建Graphics对象的例子如下 privatevoidForml Paint objectsender PaintEventArgse Graphicsg e Graphics 声明一个Graphics对象g 2 使用窗体或控件的CreateGra

3、phics方法 窗体和控件类都有一个CreateGraphics方法 通过该方法可以在程序中生成此窗体或控件所对应的Graphics对象 这种方法一般应用于对象已经存在的情况下 如下所示 Graphicsg g this CreateGraphics 3 使用Image的派生类创建Graphics对象 使用Image的任何派生类均可以生成相应的Graphics对象 这种方法一般适用于在C 中对图像进行处理的场合 如下 Bitmapb newBitmap Mybmp bmp Graphicsg Graphics FromImage b 11 1 3Pen对象 1 Pen对象的创建Pen类的构造函

4、数有四种 使用方法如下 1 创建某一颜色的Pen对象 publicPen Color 2 创建某一刷子样式的Pen对象 publicPen Brush 3 创建某 刷子样式并具有相应宽度的Pen对象 publicPen Brush float 4 创建某一颜色和相应宽度的Pen对象 publicPen Color float 2 Pen对象的常用属性 1 Alignment属性 用来获取或设置此Pen对象的对齐方式 2 Color属性 用来获取或设置此Pen对象的颜色 3 Width属性 用来获取或设置此Pen对象的宽度 4 DashStyle属性 用来获取或设置通过此Pen对象绘制的虚线的样

5、式 5 DashCap属性 用来指定虚线两端风格 是一个DashCap枚举型的值 6 StartCap属性 用来获取或设置通过此Pen对象绘制的直线起点的帽样式 7 EndCap属性 用来获取或设置通过此Pen对象绘制的直线终点的帽样式 8 PenType属性 用来获取用此Pen对象绘制的直线的样式 11 1 4Font对象 Font对象的常用属性 例如有下列程序代码 privatevoidbutton1 Click objectsender System EventArgse Fontfnt newFont Tahoma 20 FontStyle Bold FontStyle Italic

6、创建字体Graphicsg this CreateGraphics 创建Graphics对象g DrawString GDI 编程世界 fnt newSolidBrush Color Blue 14 10 输出文字 程序的执行结果如图11 2所示 图11 2程序运行结果 11 1 5Brush对象 1 SolidBrush画刷SolidBrush类用来定义单一颜色的Brush 其构造函数如下 publicSolidBrush Color Color 例如 SolidBrushMyBrush newSolidBrush Color Blue 该语句创建了一个名为MyBrush的蓝色画刷 2 Ha

7、tchBrush画刷HatchBrush类的构造函数有两种 分别如下 格式1 publicHatchBrush HatchStyle Color 格式2 publicHatchBrush HatchStyle Color Color HatchBrush画刷具有三个属性 分别如下 1 BackgroundColor属性 获取此HatchBrush对象的背景色 2 ForegroundColor属性 获取此HatchBrush对象的前景色 3 HatchStyle属性 获取此HatchBrush对象的阴影样式 例如 有下列语句 HatchBrushHb newHatchBrush HatchSt

8、yle Cross Color Blue 该语句创建一个名为Hb的画刷对象 该画刷的前景色为蓝色 填充样式为十字交叉 3 LinearGradientBrush画刷 LinearGradientBrush类的构造函数有多种格式 最常用的格式如下 publicLinearGradientBrush Point1 Point2 Color1 Color2 该构造函数有四个参数 其中Point1是表示渐变的起始点 Point2是表示渐变的终结点 Color1表示的渐变的起始色 Color2表示的是渐变的终止色 此处的Point1和Point2是Point结构型的变量 Point结构表示一个点 有两个

9、成员x和y 分别表示点的横坐标和纵坐标 例如有下列程序段 privatevoidbutton1 Click objectsender System EventArgse Graphicsg this CreateGraphics 生成图形对象PenMypen newPen Color Green 5 生成画笔LinearGradientBrushMyBrush newLinearGradientBrush newPoint 0 20 newPoint 20 0 Color Yellow Color Blue 生成渐变画刷g FillRectangle MyBrush 0 0 200 100 填

10、充矩形 程序的执行结果如图11 3所示 图11 3线性渐变填充 11 1 6常用图形的绘制方法 1 画直线 格式1 publicvoidDrawLine Penpen intx1 inty1 intx2 inty2 格式2 publicvoidDrawLine Penpen Pointpt1 Pointpt2 例如有下列程序 privatevoidbutton1 Click objectsender System EventArgse Graphicsg this CreateGraphics 生成图形对象 PenMypen newPen Color Blue 5 生成画笔 蓝色 5个像素g

11、DrawLine Mypen 1 1 30 30 画线Pointpt1 newPoint 1 30 生成起点Pointpt2 newPoint 30 1 生成终点g DrawLine Mypen pt1 pt2 画线 程序的执行结果如图11 4所示 图11 4所画直线 2 画椭圆 格式1 publicvoidDrawEllipse Penpen Rectanglerect 格式2 publicvoidDrawEllipse Penpen intx inty intwidth intheight 例如有以下程序 privatevoidForm1 Click objectsender System

12、 EventArgse Graphicsg this CreateGraphics 生成图形对象PenMypen newPen Color Blue 5 生成画笔 蓝色 5个像素g DrawEllipse Mypen 1 1 80 40 画椭圆 Rectanglerect newRectangle 85 1 165 40 生成矩形g DrawEllipse Mypen rect 画椭圆 程序的执行结果如图11 5所示 图11 5绘制的椭圆 3 绘制圆弧 格式1 publicvoidDrawArc Penpen Rectanglerect floatstartAngle floatsweepAn

13、gle 格式2 publicvoidDrawArc Penpen intx inty intwidth intheight intstartAngle intsweepAngle 例如有以下程序 privatevoidForm1 Click objectsender System EventArgse Graphicsg this CreateGraphics 生成图形对象PenMypen newPen Color Blue 5 生成画笔 蓝色 5个像素g DrawArc Mypen 1 1 80 40 90 270 画弧线Rectanglerect newRectangle 85 1 165

14、 40 生成起点g DrawArc Mypen rect 0 90 画弧线 程序的执行结果如图11 6所示 图11 6绘制的弧线 4 画扇形图使用Graphics对象的DrawPie方法可以绘制扇形图 所谓扇形图其实就是把一段圆弧的两个端点与圆心相连 DrawPie方法的格式与DrawArc方法基本一致 例如有以下程序 privatevoidForm1 Click objectsender System EventArgse Graphicsg this CreateGraphics 生成图形对象PenMypen newPen Color Blue 5 生成画笔 蓝色 5个像素 g DrawP

15、ie Mypen 1 1 80 40 90 270 画扇形Rectanglerect newRectangle 85 1 165 40 生成矩形g DrawPie Mypen rect 0 90 画扇形 程序的执行结果如图11 7所示 图11 7绘制的饼形 5 画矩形 格式1 publicvoidDrawRectangle Penpen Rectanglerect 格式2 publicvoidDrawRectangle Penpen intx inty intwidth intheight 例如有以下程序 privatevoidForm1 Click objectsender System E

16、ventArgse Graphicsg this CreateGraphics 生成图形对象PenMypen newPen Color Blue 2 生成画笔 蓝色 2个像素 g DrawRectangle Mypen 5 5 80 40 画矩形Rectanglerect newRectangle 85 15 140 50 生成矩形g DrawRectangle Mypen rect 画矩形 程序的执行结果如图11 8所示 图11 8绘制的矩形 6 画Bezier曲线 格式1 publicvoidDrawBezier Penpen Pointpt1 Pointpt2 Pointpt3 Pointpt4 格式2 publicvoidDrawBezier Penpen floatx1 floaty1 floatx2 floaty2 floatx3 floaty3 floatx4 floaty4 例如有以下程序 privatevoidForm1 Click objectsender System EventArgse Graphicsg this CreateGraphics 生成图形对象P

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 电子/通信 > 电子电气自动化

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号