GDI、GDI+相应的API

目录

一、GDI+重要命令空间?

二、GDI+重要类?创建图形的3种主流方法?

三、创建图形的3种主流方法:

四、API规律:

五、什么是GUI?GDI和GUI区别?

六、画东西的基本步骤?

一、创建图形对象(画布、画板)

二、设置绘制参数(抗锯齿,平滑等)

三、开始绘制(使用相关的图形方法,属性,其他对象)

绘制图片: 

绘制路径:

绘制文字:

绘制圆:

绘制五角星:


一、GDI+重要命令空间?

System.Drawing
System.Drawing.Text
System.Drawing.Design
System.Drawing.Drawing2D
System.Drawing.Imaging
System.Drawing.Printing

二、GDI+重要类?创建图形的3种主流方法?

Graphics类
画图方法都被包括在Graphics类中,在画任何对象时,我们首先要创建一个Graphics类实例,这个实例相当于建立了一块画布,有了画布才可以用各种画图方法进行绘图。

三、创建图形的3种主流方法:

1.Graphics g = e.Graphics;  注意:e的类型是PaintEventArgs
2.Graphics g = this.CreateGraphics();或Graphics pg = this.panel1.CreateGraphics();  注意:this是窗体对象
3.Graphics g = Graphics.FromImage(img); 注意:参数类型是Image或BitImage;

四、API规律:

1.第一参数:画笔(空心,DrawXXX,配合Pen),画刷(实心,填充,FillXXX,配合Brush)
2.区域:a.坐标配合宽高  b.矩形
3.顺序:建议顺时针,点(左上,右上,右下,左下),角度(X轴正方向为0,Y轴下方为90)

五、什么是GUI?GDI和GUI区别?

GUI:是图形用户接口,英文全称:Graphics User Interface。指用户界面,如窗体,控件。
GDI:是图形设备接口,包含图形编程相关的对象和方法。
 

六、画东西的基本步骤?

a. 创建Graphics对象;才有了绘制图形的能力。
b. 绘画的时机?Paint事件
c. 使用Graphics对象提供的相关方法,进行画图。画图时会依赖其他对象,如:Pen,Brush,Font,Color等

一、创建图形对象(画布、画板)

创建一个路径实例(就是在画板Graphics划分一个区域)

Graphics类,可以理解成画板,其实此类提供绘制图形的能力(子对象,属性,方法,俗称的接口。)

Graphics g = e.Graphics;

二、设置绘制参数(抗锯齿,平滑等)

g.SmoothingMode = SmoothingMode.AntiAlias;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.HighQualityBicubic;

三、开始绘制(使用相关的图形方法,属性,其他对象)

注意:起点的坐标要考虑画笔的宽度

Pen pen = new Pen(Color.Red, 20F);
Point pt1 = new Point(50, 50);
Point pt2 = new Point(100, 50);
g.DrawLine(pen, pt1, pt2);

DrawLines不一定是闭合的

Pen pen = new Pen(Color.Red, 20F);
Point[] points = new Point[] {
    new Point(20,20),
    new Point(100,20),
    new Point(55,100),
    new Point(20,20),
    new Point(100,20),
};
g.DrawLines(pen, points);

绘制图片: 

参数1:绘制的原始图片。

参数2:把原始图片绘制的多大的一个矩形中(目标矩形),如果图片过大,会自动缩放到此矩形中。

参数3:原始图片的所占的矩形大小。

参数4:参数3所用的单位

 string path = Path.Combine(Environment.CurrentDirectory, "../../images/1.png");
 Image img = Image.FromFile(path);

 Rectangle destRect = new Rectangle(0, 0, this.panel3.Width, this.panel3.Height);
 Rectangle srcRect = new Rectangle(0, 0, img.Width, img.Height);
 g.DrawImage(img, destRect, srcRect, GraphicsUnit.Pixel);

绘制路径:

GraphicsPath path = new GraphicsPath();
path.StartFigure(); // 路径开始
path.AddLine(30, 30, 100, 30);
path.AddLine(100, 30, 100, 130);
path.CloseFigure(); // 路径结束 

path.StartFigure(); // 路径开始
path.AddEllipse(110, 0, 30, 30);
path.CloseFigure(); // 路径结束

g.FillPath(Brushes.Red, path);

绘制文字:

Brush brush = new SolidBrush(Color.Red);
Point point = new Point(10, 10);
g.DrawString("yoyo21ktime",new Font("苹方",10),brush,point);

绘制圆:

GraphicsPath path = new GraphicsPath();
path.StartFigure();
//path.AddLine(10F, 10F, 60F, 60F);
//path.AddLine(60F, 60F, 60F, 60F);
path.AddArc( rect, 100F, 360F);

绘制五角星:

GraphicsPath path = new GraphicsPath();
path.StartFigure();
path.AddLine(new Point(0, 76), new Point(80, 76));
path.AddLine(new Point(80, 76), new Point(106, 0));
path.AddLine(new Point(106, 0), new Point(130, 76));
path.AddLine(new Point(130, 76), new Point(210, 76));
path.AddLine(new Point(210, 76), new Point(146, 124));
path.AddLine(new Point(146, 124), new Point(170, 200));
path.AddLine(new Point(170, 200), new Point(106, 152));
path.AddLine(new Point(106, 152), new Point(40, 200));
path.AddLine(new Point(40, 200), new Point(66, 124));
path.CloseFigure();

g.FillPath(new SolidBrush(Color.Blue), path);

  • 14
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值