c#教材_GDI+
合集下载
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
13
Developing with .NET for WinForm
13/25
五、利用Graphics 实例的方法来绘制图形
方法名称 描述 Clear 清除绘图表面, 并用指定的背景色进行填充 DrawLine 绘制线条 DrawRectangle 绘制矩形 DrawArc 绘制弧 DrawEllipse 绘制椭圆 DrawImage 绘制图像 DrawString 绘制文本字符串 FillEllipse 用颜色填充椭圆
5
Developing with .NET for WinForm
5/25
二、GDI+ 简介
GDI+ 提供在 WinForms 中实现图形的基本 功能 GDI+ 存在于 System.Drawing.dll 程序集中 使用 System.Drawing.Text、 System.Drawing.Imaging 和 System.Drawing.Drawing2D 等类可以绘制 并填充形状,提供有吸引力的彩色文本, 还可以在不使用任何图片控件的情况下绘 制图像
6
Developing with .NET for WinForm
6/25
三、Graphics 类
属于 System.Drawing 命名空间 不可继承 可以通过多种方式创建 Graphics 对象:
重写 OnPaint 方法并获得对图形的引用 使用窗体的 Paint 事件以获得对图形的引用 GreatGraphics方法 FromImage 方法
10
Developing with .NET for WinForm
10/25
四、形状和图像
1、 Pen 类 属于 System.Drawing 命名空 间 用于指定形状的宽度、样式和 填充样式 无法继承,但可以通过实例化 该类来创建对象
Pen ourpen=new Pen(Color.Blue,5);
7
Developing with .NET for WinForm
7/25
1、得到Graphics对象实例
protected override void OnPaint(PaintEventArgs paintevent) { Graphics graf=paintevent.Graphics; } private void mainForm_Paint(object sender, PaintEventArgs paintevent) { Graphics graf=paintevent.Graphics; }
11
Developing with .NET for WinForm
11/25
2、Brush 类
属于 System.Drawing 命名空 间 用于在形状中填充纯色 是抽象类 可以使用 SolidBrush、 LinearGradientBrush 和 TextureBrush 类来创建 Brush 对象
3
Developing with .NET for WinForm
3/25
我们发现:
一、如果最小化该窗体,再恢复它,绘制好的图形就不见了。 二、如果在该窗体上拖动另一个窗口,使之只显示一部分图形,再把该窗口拖离 这个窗体,临时被挡住的部分就消失了,只剩下一半椭圆或矩形了!
问题原因:
问题发生的原因是,如果窗口的一部分被隐藏了,Windows通常会立即删除与其 中显示的内容相关的所有信息。 一般窗体显示出来(Show),激活(Activate),调整大小(Resize),恢复窗体等时候, 都会自动重画。
9
Developing with .NET for WinForm
9/25
3、FromImage 方法
可 以 从 由 Image 类 派 生 的 任 何 对 象 创 建 Graphics 对象 通过使用 Graphics 类的 FromImage 方法可 以完成此操作 示例
Bitmap bmpimage=new Bitmap(@"C:\cat13.jpg"); Graphics graf=Graphics.FromImage (bmpimage); graf.DrawImage (bmpimage, 20, 20);
一、关于重画
我们创建一个Windows应用程序,首先把窗体的背景色设置为白色。 在构造函数中增加如下代码: //显示本窗体 This.show(); //为本窗体创建一个graphice对象 Graphics dc = this.CreateGraphics(); //定义一个蓝画笔 Pen bluePen = new Pen(Color.Blue, 3); //用蓝色画一个矩形 dc.DrawRectangle(bluePen, 0, 0, 50, 50); //再定义一个红画笔 Pen redPen = new Pen(Color.Red, 2); //用红色画一个椭圆 dc.DrawEllipse(redPen, 0, 50, 80, 60); 结果会显示一个矩形和椭圆。
15
Developing with .NET for WinForm
15/25
DrawLine 方法例子
Graphics g = e.Graphics; Pen blackPen = new Pen(Color.Black, 1); if (ClientRectangle.Height / 10 > 0) { for (int y = 0; y < ClientRectangle.Height; y += ClientRectangle.Height / 10) { g.DrawLine(blackPen, new Point(0, 0), new Point(ClientRectangle.Width, y)); } } blackPen.Dispose();
第六章 GDI+ 编程
1
课程目标
讨论重画与onpaint方法 通过 GDI+ 创建图形图像 探讨 GDI+ 中的对象:Pen、Brush 和 Color 通过 GDI+ 绘制线条、写文本、 显示图像 讨论GDI+中的坐标
2
Developing with .NET for WinForm
2/25
DrawString 方法 的例2
见备注中的代码
20
Developing with .NET for WinForm
20/25
3、DrawImage 方法
用于使用 Image 对象绘制图像。包括GIF、 JPG 和 BMP等图像
函数原型 public public public public public void void void void void DrawImage(Image, DrawImage(Image, DrawImage(Image, DrawImage(Image, DrawImage(Image, Point) Point[]) PointF) PointF[]) Rectangle)
引申话题:
在使用Windows窗体的服务器控件时,不需要知道这些,这 是因为标准控件非常专业,能在Windows需要时重新绘制 它们自己。这是编写控件时不需要担心实际绘图过程的原 因之一。如果要应用程序在屏幕上绘图,还需要在 Windows要求重新绘制窗口的全部或部分时,确保应用程 序会正确响应。
winform252525利用drawimage提高绘图速度见备注中的代码developingwinform2625264drawrectangle方法见备注中的代码developingwinform2725275fillellipse方法见备注中的代码developingwinform2825286fillregion方法见备注中的代码developingwinform2925297fillpolygon方法填充point结构指定的点数组所定义的多边形的内部developingwinform3025308gdi打印创建printdocument类的实例设置描述打印详细信息的属性调用print方法developingwinform312531例子讲解文本编辑器的打印在这里例子中我们在文本编辑器的上增加菜单打印功能并且我们在format菜单下增加选择字体和颜色的功能
16
Developing with .NET for WinForm
16/25
2、DrawPath 方法例子
base.OnPaint(e); GraphicsPath path; path = new GraphicsPath(new Point[]{new Point(10,10), new Point(150,10), new Point(200,150), new Point(10,150), new Point(200,160)}, new byte[] {(byte)PathPointType.Start, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line, (byte)PathPointType.Line}); e.Graphics.DrawPath(Pens.Black, path);
8
Developing with .NET for WinForm
8/25
2、CreateGraphics 方法
CreateGraphics( ) 方法用于创建 Graphics 对象 它是 Control 类的方法 示例
private void PaintMe(Control testcontrol) { Graphics graf=CreateGraphics(); . . . }
解决方法:
如果把这段代码放在paint事件对应的方法中就不会有这种现象了。 Windows会利用Paint事件通知应用程序需要完成一些重新绘制的要求。
4
Developing with .NET for WinForm
4/25
关于onpaint方法:
Paint事件默认处理虚方法OnPaint()的调用,所以使用重写 onpaint方法也能得到同样的效果。
Developing with .NET for WinForm
14
14/25
1、各种绘制方法—DrawLine 方法
G幕上绘制线条
函数原型
public public public public float, void DrawLine(Pen, void DrawLine(Pen, void DrawLine(Pen, void DrawLine(Pen, float); Point, Point); PointF, PointF); int, int, int, int); float, float,
17
Developing with .NET for WinForm
17/25
2、DrawString 方法
在屏幕上显示文本,而不使用任何与 文本相关的控件。
函数原型
public void DrawString(string, public void DrawString(string, RectangleF); public void DrawString(string, StringFormat); public void DrawString(string, RectangleF, StringFormat); public void DrawString(string, float); public void DrawString(string, float, StringFormat); Font, Brush, PointF); Font, Brush, Font, Brush, PointF,
Font, Brush,
Font, Brush, float, Font, Brush, float,
18
Developing with .NET for WinForm
18/25
DrawString 方法 的例1
见备注中的代码
19
Developing with .NET for WinForm
19/25
SolidBrush myBrush = new SolidBrush(Color.Blue);
12
Developing with .NET for WinForm
12/25
3、Color 结构
用于为 GDI+ 中的图形 创建或使用颜色。
Graphics g=e.Graphics; g.Clear(Color.MistyRose);
21
Developing with .NET for WinForm
21/25
绘制 JPG 图像实例
protected override void OnPaint(PaintEventArgs p_event) { int x_coord,y_coord; Image testimage=Image.FromFile (@"c:\boat.jpg"); Graphics graf=p_event.Graphics; x_coord=10; y_coord=10; graf.DrawImage(testimage,x_coord,y_coord); }