1 / 86

第 9 章 二维图形图像处理

第 9 章 二维图形图像处理. 9.1 GDI+ 概述 9.2 绘制基本图形 9.3 图像处理. 9.1 GDI+ 概述.  GDI+ : Graphics Device Interface ( 图形设备接口 ) ,它提供了高级图形图像处理功能  在 C# 中,通过一套部署为托管代码的类来展现提供的图 形图像处理功能,这套类被称为 GDI+ 的托管类。利用 GDI+ 可以轻松实现颜色渐变、透明处理、纹理处理、拉伸和缩放等多种高级功能。 . GDI+ 主要提供了三类服务: 1. 二维矢量图形 2. 图像处理 3. 文字显示.

hong
Télécharger la présentation

第 9 章 二维图形图像处理

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第9章 二维图形图像处理 9.1 GDI+概述 9.2 绘制基本图形 9.3 图像处理

  2. 9.1 GDI+概述  GDI+:Graphics Device Interface (图形设备接口),它提供了高级图形图像处理功能  在C#中,通过一套部署为托管代码的类来展现提供的图 形图像处理功能,这套类被称为GDI+的托管类。利用 GDI+可以轻松实现颜色渐变、透明处理、纹理处理、拉伸和缩放等多种高级功能。 

  3. GDI+主要提供了三类服务: 1.二维矢量图形 2.图像处理 3.文字显示

  4. 9.1 GDI+概述(续)  在C#中,所有图形图像处理功能都包含在以下名称空间下: 1.System.Drawing名称空间 提供了对GDI+基本图形功能的访问,主要有Graphics类、Bitmap类、从Brush类继承的类、Font类、Icon类、Image类、Pen类、Color类等。

  5. 2.System.Drawing.Drawing2D名称空间 提供了高级的二维和矢量图形功能。主要有:梯度型画刷、Matrix类(用于定义几何变换)和GraphicsPath类等。

  6. 3.System.Drawing.Imaging名称空间 提供了高级图像处理功能。 4.System.Drawing.Text名称空间 提供了高级 字体和文本排版功能

  7. 9.1 .1 GDI+使用的坐标系 1. GDI+坐标系中的基本结构 Point表示某个特定位置相对于原点的水平和垂直距离。例如: Point p = new Point (1,1); Size也有两个整型属性来表示水平和垂直距离——Width和Height。例如: Size s = new Size (5,5); Rectangle是用来指定矩形的坐标的,它由一个Point和一个Size 组成,其中Point表示矩形左上角,Size表示矩形大小。

  8. 9.1 .1 GDI+使用的坐标系(续) 例如: 1) 在构造函数中分别指定x坐标、y坐标、宽度和高度。 Rectangle r1 = new Rectangle (1,2,5,6); 2) 在构造函数中指定Point位置和Size结构。 Point p = new Point (1,2); Size s = new Size (5,6); Rectangle r2 = new Rectangle (p, s);

  9. 9.1 .1 GDI+使用的坐标系(续) 2.GDI+中坐标系的分类 世界坐标系 设备坐标系 页面坐标系

  10. 9.1 .1 GDI+使用的坐标系(续) 【例】不同坐标系之间的转换示例 private void Form1_Paint(object sender, PaintEventArgs e) { Graphics myGraphics = e.Graphics; myGraphics.TranslateTransform(0.5F, 0.5F); myGraphics.PageUnit = GraphicsUnit.Inch; Pen myPen = new Pen(Color.Black, 1 / myGraphics.DpiX); myGraphics.DrawLine(myPen, 0, 0, 2, 2); }

  11. 9.1.2 Graphics类 Graphics类包含在System.Drawing名称空间下,它提供了 简单的图形图像处理功能。在绘制图形图像前,首先必须创建 Graphics对象,然后利用这个对象绘制直线、曲线、椭圆等图 形图像。

  12. 有三种常见的创建Graphics对象的方法。 1.在窗体或控件的Paint事件中获取Graphics对象。例如: private void Form1_Paint(object sender, System.Windows.Forms.PaintEventArgs e) { Graphics g = e.Graphics; }

  13. 9.1.2 Graphics类(续) 2.通过当前窗体的CreateGraphics方法,把当前窗体的画笔、字体、颜色作为默认值,获取对Graphics对象的引用。 例如: Graphics g; g = this.CreateGraphics(); ……

  14. 3.从继承自图像的任何对象创建Graphics对象, 例如: Bitmap myBitmap = new Bitmap(@"C:\mytest1\Pics\myPic.bmp"); Graphics g = Graphics.FromImage(myBitmap);

  15. 9.1.3 颜色 颜色是进行图形操作的基本要素,它封装在 System.Drawing.Color结构中。人眼可以分辨的任何颜色都 是由一定的红、绿、蓝三色光组成。任何一种颜色都可以有 四个分量,分别是:R、G、B、A。其中: • R:红色,取值范围0~255,0表示没有红色成分,255为饱和红色; • G:绿色,取值范围0~255,0表示没有绿色成分,255为饱和绿色; • B:蓝色,取值范围0~255,0表示没有蓝色成分,255为饱和蓝色; • A:Alpha值,即透明度,取值范围0~255,0表示完全透明,255表示完全不透明。

  16. 9.1.3 颜色(续) 在代码中声明颜色的方式常用有两种: 1.调用静态方法Color.FromArgb()指定任意颜色,这种方法有两种常用形式。 第一种形式为直接指定三种颜色,方法原型为: Public static Color FromArgb (int red,int green,int blue); 三个参数分别表示R、G、B三色,Alpha值使用默认值255,即完全不透明。例如: Color red = Color.FromArgb(255, 0, 0); 第二种形式为四个参数,方法原型为: Public static Color FromArgb (int alpha,int red,int green, int blue); 四个参数分别表示透明度、R、G、B三色值。

  17. 9.1.3 颜色(续) 2.系统预定义颜色 System.Drawing.Color结构中提供了许多静态性, 每个属性返回一个命名颜色,在Color结构中已经预定 义了141种颜色,可以直接使用。例如: this.BackColor = Color.White;

  18. 9.1.4 画笔和画刷 在GDI+中,可以使用画笔和画刷绘制或填充图形、文本和图像。笔是Pen类的实例,用于绘制各种直线、空心图形和实心图形等。画刷是从Brush类派生的任何类的实例,用于填充形状或绘制文本。

  19. 1. 画笔(Pen) 画笔可用于绘制绘制具有指定宽度和样式的直线、曲线或轮廓形状。 下面的示例说明如何创建一支基本的蓝色画笔: Pen myPen = new Pen(Color.Blue); Pen myPen = new Pen(Color.Blue, 10.5f); 也可以从画刷对象创建画笔对象,例如: SolidBrush myBrush = new SolidBrush(Color.Red); Pen myPen = new Pen(myBrush); Pen myPen = new Pen(myBrush, 5);

  20. 9.1.4 画笔和画刷(续) 【例】画笔(Pen)的用法演示示例。 添加名称空间引用:using System.Drawing.Drawing2D, 并用如下代码添加Form1_Paint事件。 Graphics g = e.Graphics; //创建Graphics对象 Pen blackpen = new Pen(Color.Black, 10.0f); //创建一支黑色的画笔 //绘制字符串 g.DrawString("黑色,宽度为10.0", this.Font, Brushes.Black, 5, 5); //绘制宽度为10.0f的黑色直线 g.DrawLine(blackpen, new Point(110, 12), new Point(400, 12)); //创建一支红色的画笔 Pen redpen = new Pen(Color.Red, 5.0f); //绘制字符串 g.DrawString("红色,宽度为5", this.Font, Brushes.Black, 5, 25); //绘制宽度为5的红色直线 g.DrawLine(redpen, new Point(110, 30), new Point(400, 30));

  21. 9.1.4 画笔和画刷(续) //创建一支蓝色的笔 Pen bluepen = new Pen(Color.Blue,6); //定义直线的样式 bluepen.StartCap = LineCap.Flat; bluepen.EndCap = LineCap.ArrowAnchor; //绘制字符串 g.DrawString(“蓝色箭头线”, this.Font, Brushes.Black, 5, 45); //绘制宽度为6的蓝色箭头线 g.DrawLine(bluepen, new Point(110, 50), new Point(400, 50)); //再创建一支黑色的画笔 Pen iblackpen = new Pen(Color.Black, 2.0f) //定义直线的样式 iblackpen.DashStyle = DashStyle.Custom; iblackpen.DashPattern = new float[] { 4, 4 }; iblackpen.EndCap = LineCap.NoAnchor; //绘制字符串 g.DrawString("自定义虚线", this.Font, Brushes.Black, 5, 65);

  22. 9.1.4 画笔和画刷(续) //绘制宽度为2的自定义虚线 g.DrawLine(iblackpen, new Point(110, 70), new Point(400, 70)); //再创建一支红色的笔 Pen iredpen = new Pen(Color.Red, 2.0f); //定义直线的样式 iredpen.DashStyle = DashStyle.Dot; //绘制字符串 g.DrawString("点划线", this.Font, Brushes.Black, 5, 85); //绘制宽度为2的点划线 g.DrawLine(iredpen, new Point(110, 90), new Point(400, 90));

  23. 9.1.4 画笔和画刷(续) 2.画刷(Brush) 画刷是可以与Graphics对象一起使用来创建实心形状和 呈现文本的对象。可以用来填充各种图形形状,如矩形、椭 圆、饼型图和多边形等。

  24. 9.1.4 画笔和画刷(续) 1)使用SolidBrush类定义单色画刷 SolidBrush类用于定义单色画刷。该类可以填充图形, 比如矩形、椭圆、多边形和路径。 【例】单色画刷演示示例。 添加名称空间引用:using System.Drawing.Drawing2D; 并 用如下代码添加Form1_Paint事件。 Graphics g = e.Graphics; SolidBrush mySolidBrush = new SolidBrush(Color.Red); g.FillEllipse(mySolidBrush, 30,30, 130, 100);

  25. 9.1.4 画笔和画刷(续) 2)使用HatchBrush类填充简单图案 HatchBrush类提供的画刷可以用各种图案填充图形。通过 Hatch类型可以设置影线样式。在创建影线的画刷时,能设定前 景色、背景色和影线样式。有56种不同的影线样式枚举。 【例】填充简单图案示例。 添加名称空间引用:using System.Drawing.Drawing2D;并用 如下代码添加Form1_paint事件。 Graphics g = e.Graphics; HatchBrush myHatchBrush =new HatchBrush( HatchStyle.Cross, Color.White ,Color.Red ); g.FillEllipse(myHatchBrush, this.ClientRectangle);

  26. 9.1.4 画笔和画刷(续) 3)使用TextureBrush类填充复杂图像 TextureBrush类使用图像作为填充的样式,它可以使用例如.bmp、.jpg、.png等格式的图像。初始化一个新的TextureBrush对象需要指定填充的图像。

  27. 9.1.4 画笔和画刷(续) 【例】创建TextureBrush示例。 将所用图像添加到项目中,并修改图片的【复制到输出目 录】属性为“总是复制”,用如下代码添加Form1_Paint事件。 //创建Graphics对象 Graphics g = e.Graphics; //根据文件名创建原始大小的Bitmap对象 Bitmap bitmap = new Bitmap("xue.jpg"); //将其缩放到当前窗体大小 bitmap = new Bitmap(bitmap, this.ClientRectangle.Size); TextureBrush myBrush = new TextureBrush(bitmap); g.FillEllipse(myBrush, this.ClientRectangle);

  28. 4)使用LinearGradientBrush类定义线性渐变

  29. 9.1.4 画笔和画刷(续) LinearGradientBrush对象用颜色线性渐变填充图形。提供 了以下三种构造函数: (1) 提供两个点和两种颜色。 Public LinearGradientBrush ( Point point1, Point point2, Color color1, Color color2) (2) 提供一个矩形和一个角度。 Public LinearGradientBrush (Rectangle rect ,Color color1, Color color2,float angle) (3) 指定渐变的模式。 Public LinearGradientBrush (Rectangle rect ,Color color1, Color color2, LinearGradientMode linearGradientMode)

  30. 9.1.4 画笔和画刷(续) 【例】使用点描述线性渐变。 添加名称空间引用:using System.Drawing.Drawing2D; 并用如下代码添加Form1_Paint事件。 Graphics g = e.Graphics; LinearGradientBrush myBrush = new LinearGradientBrush( new Point(0, 0),new Point(30, 30), Color.White,Color.Blue); g.FillRectangle(myBrush, this.ClientRectangle); myBrush.Dispose();

  31. 9.1.4 画笔和画刷(续) 【例】使用矩形描述线性渐变。 添加名称空间引用:using System.Drawing.Drawing2D; 并用如下代码添加Form1_Paint事件。 Graphics myGraphics = e.Graphics; LinearGradientBrush lgb2 = new LinearGradientBrush( new Rectangle(0, 0, 70, 70), Color.White,Color.Black,30f); e.Graphics.FillRectangle(lgb2, this.ClientRectangle); lgb2.Dispose();

  32. 9.1.4 画笔和画刷(续) 5)使用PathGradientBrush类实现彩色渐变 在GDI+中,把一个或多个图形组成的形体称为路径。可 以使用GraphicsPath类定义路径,使用PathGradientBrush类 定义路径内部的渐变色画刷。渐变色从路径的内部中心点逐渐 过渡到路径的外边界边缘。 PathGradientBrush类提供了三种重载的构造函数,其中 常用的是通过指定路径实现彩色渐变。常用形式如下: Public PathGradientBrush ( GraphicsPath path) 【例】路径和路径画刷的使用示例。

  33. 9.1.4 画笔和画刷(续) Graphics g = e.Graphics; Point centerPoint = new Point(80, 80); int R = 60; GraphicsPath path = new GraphicsPath(); path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R); PathGradientBrush brush = new PathGradientBrush(path); //指定路径中心点 brush.CenterPoint = centerPoint; //指定路径中心点的颜色 brush.CenterColor = Color.White; //Color类型的数组指定与路径上每个顶点对应的颜色 brush.SurroundColors = new Color[] { Color.Red}; g.FillEllipse(brush, centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R); centerPoint = new Point(220, 80); R = 20; path = new GraphicsPath(); path.AddEllipse(centerPoint.X - R, centerPoint.Y - R, 2 * R, 2 * R);

  34. 9.1.4 画笔和画刷(续) path.AddEllipse(centerPoint.X - 2 * R, centerPoint.Y - 2 * R, 4 * R, 4 * R); path.AddEllipse(centerPoint.X - 3 * R, centerPoint.Y - 3 * R, 6 * R, 6 * R); brush = new PathGradientBrush(path); brush.CenterPoint = centerPoint; brush.CenterColor = Color.White ; brush.SurroundColors = new Color[]{ Color.Black,Color.Blue,Color.Green }; g.FillPath(brush, path);

  35. 9.2 绘制基本图形 所有绘制图形的方法都位于Graphics中。 9.2.1 直线 在GDI+中绘制直线首先需要创建一个Graphics对象和一个 Pen对象。Graphics对象提供了绘图的方法,只有创建了 Graphics对象才能进行绘图操作;Pen对象可以指定所绘制直线 的一些属性,例如:线的颜色、宽度和类型等。 绘制直线用到Graphics对象中的两种方法:DrawLine方法 和DrawLines方法。DrawLine用于绘制一条直线,DrawLines用 于绘制多条直线。

  36. 9.2.1 直线(续) 1.DrawLine方法常用形式 1) 绘制一条连接指定两个Point结构的线。 public void DrawLine (Pen pen,Point pt1, Point pt2) 其中,Pen对象确定线条的颜色、宽度和样式; Point结构确定起点和终点。 2) 绘制一条由坐标对指定的两个点的线。 public void DrawLine (Pen pen,int x1, int y1,int x2,int y2) 其中,Pen对象确定线条的颜色、宽度和样式; x1,y1为起点坐标,x2,y2为终点坐标。

  37. 9.2.1 直线(续) 2.DrawLines方法常用形式 绘制一系列点组成的线。 public void DrawLines (Pen pen,Point[] points) 其中,Pen对象确定线条的颜色、宽度和样 式;数组中的第一个点指定起始点,后面的每个点 都以相临的前一个点为起始点组成线段。

  38. 9.2.1 直线(续) 【例】使用DrawLine方法和DrawLines方法绘制直线。 用如下代码添加Fror1_Paint事件。 Graphics g = e.Graphics; //创建Graphics对象 Pen blackPen = new Pen(Color.Black, 3); // 创建一支黑色的画笔 Point point1 = new Point(200,50); // 定义直线的两个点 Point point2 = new Point(400,50); g.DrawLine(blackPen, point1, point2); // 绘制直线 Pen redPen = new Pen(Color.Red, 3); // 创建一支红色的画笔 int x1 = 200; // 定义起点和终点坐标 int y1 = 100; int x2 = 400; int y2 = 130; g.DrawLine(redPen, x1, y1, x2, y2); // 绘制直线

  39. 9.2.1 直线(续) Pen pen = new Pen(Color.Blue , 3); // 创建一支蓝色的画笔 Point[] points = { new Point(200,180), new Point(200,200), new Point(400,200), new Point(400,180) }; // 定义一系列点 //绘制直线 g.DrawLines(pen, points);

  40. 9.2.2 矩形 1. 使用DrawRectangle方法绘制矩形常用形式 1)指定绘制矩形的结构 public void DrawRectangle (Pen pen, Rectangle rect) 其中rect表示要绘制的矩形的结构。 2)指定矩形左上角的坐标及宽和高 public void DrawRectangle (Pen pen, int x, int y, int width, int height) 其中x,y为矩形左上角的坐标值。 2. 使用DrawRectangles方法绘制多个矩形常用形式 public void DrawRectangles (Pen pen, Rectangle[] rects) 其中rects表示要绘制的多个矩形的结构。

  41. 9.2.2 矩形(续) 3. 使用FillRectangle方法填充矩形常用形式 1) 指定填充画刷和矩形的结构 public void FillRectangle ( Brush brush, Rectangle rect) 其中brush 指定填充矩形的画刷,rect表示要绘制的矩形的结构。 2) 指定填充画刷和矩形左上角的坐标及宽和高 public void FillRectangle (Brush brush, int x,int y,int width,int height ) 其中brush 指定填充矩形的画刷,x,y表示矩形左上角的坐标,width和height表示矩形的宽和高。

  42. 4. 使用FillRectangles方法填充多个矩形常用形式 public void FillRectangles ( Brush brush, Rectangle[] rects )

  43. 9.2.2 矩形(续) 【例】绘制矩形方法示例。 用以下代码添加Form1_Paint事件。 Graphics g = e.Graphics; // 创建Graphics对象 Pen blackPen = new Pen(Color.Black, 3); // 创建一支黑色的画笔 Rectangle rect = new Rectangle(300, 21, 200, 50); // 指定矩形的结构 g.DrawRectangle(blackPen, rect); // 绘制矩形 Pen redPen= new Pen(Color.Red, 3); // 创建一支红色的画笔 // 指定矩形左上角的坐标及宽和高 int x = 300;//矩形左上角点的x坐标分量 int y = 90;//矩形左上角点的y坐标分量 int width = 200;//矩形的宽 int height = 50;//矩形的高 g.DrawRectangle(redPen, x, y, width, height); // 绘制矩形 Pen bluePen = new Pen(Color.Blue, 3); // 创建一支蓝色的画笔

  44. 9.2.2 矩形(续) Rectangle[] rects = { new Rectangle (300,160,90,50), new Rectangle (350,170,90,50), new Rectangle (400,160,90,50) }; //指定多个矩形的结构 g.DrawRectangles(bluePen, rects); //绘制多个矩形 //创建一支黑色的画刷 SolidBrush blackBrush = new SolidBrush(Color.Black); Rectangle rect1 = new Rectangle(300, 240, 200, 50); // 指定矩形的结构 g.FillRectangle(blackBrush, rect1); // 填充矩形 SolidBrush redBrush = new SolidBrush(Color.Red); //创建一支黑色的画刷 // 指定矩形左上角的坐标及宽和高 int ix = 300; //矩形左上角点的x坐标分量 int iy = 310; //矩形左上角点的y坐标分量 int iwidth = 200; //矩形的宽 int iheight = 50; //矩形的高 g.FillRectangle(redBrush, ix, iy, iwidth, iheight); // 填充矩形

  45. 9.2.2 矩形(续) //创建一支蓝色的画刷 SolidBrush blueBrush = new SolidBrush(Color.Blue); Rectangle[] irects = { new Rectangle (300,380,90,50), new Rectangle (350,390,90,50), new Rectangle (400,380,90,50) }; //指定多个矩形的结构 //填充多个矩形 g.FillRectangles(blueBrush, irects);

  46. 9.2.3 多边形 1. DrawPolygon方法 DrawPolygon方法用来绘制多边形的轮廓,一 个多边形是由数组中指明的节点连接起来而组成 的。DrawPolygon方法常用形式如下: public void DrawPolygon (Pen pen, Point[] points) 2. FillPolygon方法 FillPolygon方法用来填充多边形的封闭区域。 FillPolygon方法常用形式如下: public void FillPolygon (Brush brush, Point[] points)

  47. 9.2.3 多边形(续) 【例】绘制多边形示例。 添加名称空间引用using System.Drawing.Drawing2D; 并用如下代码添加窗体的Paint事件。 Graphics g = e.Graphics; //创建Graphics对象 Pen pen = new Pen(Color.Red); // 创建一支红色的画笔 Point[] points = { new Point (50,50), new Point (100,50), new Point (130,90), new Point (130,140), new Point (100,180), new Point (50,180), new Point (20,140), new Point (20,90) }; // 定义多边形的点

  48. 9.2.3 多边形(续) g.DrawPolygon(pen, points); //绘制多边形轮廓 Point[] points1 = { new Point (250,50), new Point (300,50), new Point (330,90), new Point (330,140), new Point (300,180), new Point (250,180), new Point (220,140), new Point (220,90) }; //定义第二个多边形的点 //填充多边形封闭区域 g.FillPolygon(new SolidBrush(Color.Red), points1);

  49. 9.2.4 曲线 1. DrawCurve方法 这个方法用光滑的曲线把给定的点连接起来,常用形式有: 1) public void DrawCurve (Pen pen ,Point[] points ) 其中,Point结构类型的数组中指明各节点,默认弯曲强度为0.5。注意,数组中至少要有3个元素。 2) public void DrawCurve (Pen pen ,Point[] points,float tension ) 其中,tension指定弯曲强度,该值范围为0.0f~1.0f,超出此范围会产生异常。当弯曲强度为零时,就是直线。

  50. 9.2.4 曲线(续) 2. DrawClosedCurve方法 这个方法是通过连接数组中节点画一个平滑的曲线,此方法会自动把首尾节点连接起来构成封闭曲线。注意数组中的节点至少要有3个点组成,默认弯曲强度为0.5。 1) public void DrawClosedCurve (Pen pen ,Point[] points ) 其中,Point结构类型的数组中指明各节点。 2) public void DrawClosedCurve (Pen pen ,Point[] points,float tension FillMode fillmode) 其中,fillmode指明曲线封闭区域内以何种方式填充。

More Related