C# GDI+技术

GDI+概述

        GDI+是GDI(即Windows早期版本号中附带的Graphics Device Interface)的后继者。它是一种构成Windows XP操作系统的子系统的应用程序编程接口(API)。
GDI+基类的主要命名空间及说明:
  • System.Drawing--包括与基本画图功能相关的大多数类、结构、枚举和托付。
  • System.Drawing.Drawing2D--为大多数高级2D和矢量画图操作提供了支持,包括消除锯齿、几何转换和图形路径。

  • System.Drawing.Imaging--帮助处理图像(位图和GIF文件等)的各种类。
  • System.Drawing.Printing--把打印机或打印预览窗体作为输出设备时使用的类。
  • System.Drawing.Design--一些提前定义的对话框、属性表和其它用户界面元素,与在设计期间扩展用户界面相关。
  • System.Drawing.Text--对字体和字体系列运行更高级操作的类。

基本图形绘制

        Graphics类是GDI+的核心。Graphics对象表示GDI+画图表面,提供了对象绘制到显示设备的方法。

Graphics类封装了绘制直线、曲线、图形、图像和文本的方法,是GDI+实现绘制直线、曲线、图形、图像和文本的类。是进行一切GDI+操作的基础类。

绘制直线

        Graphics类中的DrawLine方法,可重载,主要用来绘制一条连接由坐标对指定的两个点的线条。
(1)绘制一条连接两个Point结构的线。
public void DrawLine(Pen pen, Point pt1,Point pt2)
  • pen:Pen对象,确定线条颜色、宽度和样式。

  • pt1:Point结构,表示要连接的第一个点。
  • pt2:Point结构,表示要连接的第二个点。

(2)绘制一条连接由坐标对指定的两个点的线条。
Public void DrawLine(Pen pen,int x1,int y1,int x2,int y2)

绘制直线的演示样例代码:

        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 2);
graphics.DrawLine(myPen, 50, 30, 170, 30);
}

绘制矩形

        Graphics类的DrawRectangle方法。可重载。

(1)绘制由Rectangle结构指定的矩形。
public void DrawRectangle(Pen pen,Rectangle rect)
  • pen:Pen对象,确定线条颜色、宽度和样式。

  • rect:表示要绘制矩形的Rectangle结构。

        比如:
Rectangle rect = new Rectangle(0, 0, 80, 50);

(2)绘制由坐标对、宽度和高度指定的矩形。

public void DrawRectangle(Pen pen, int x, int y, int width, int height)
  • pen:Pen对象,确定线条颜色、宽度和样式。
  • x:要绘制矩形的左上角x坐标。
  • y:要绘制矩形的左上角y坐标。

  • width和height分别表示宽度和高度。
        绘制矩形的演示样例代码:
        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 2);
graphics.DrawRectangle(myPen, 70, 20, 80, 50);
}

绘制椭圆

        Graphics类中的DrawEllipse方法,可重载。

主要用来绘制边界由Rectangle结构指定的椭圆。

(1)绘制边界由Rectangle结构指定的椭圆。

public void DrawEllipse(Pen pen, Rectangle rect)

(2)绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆。

public void DrawEllipse(Pen pen, int x, int y, int width, int height)

绘制椭圆的演示样例代码:

        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 3);
Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
graphics.DrawEllipse(myPen, myRectangle);
}

绘制圆弧

        Graphics类中的DrawArc方法,可重载。

(1)绘制一段弧线,它表示由Rectangle结构指定的椭圆的一部分。
public void DrawArc(Pen pen, Rectangle rect, float startAngle, float sweepAngle)
  • pen:Pen对象,确定线条颜色、宽度和样式。
  • rect:Rectangle结构,定义椭圆边界。
  • startAngle:从x轴到弧线的起始点沿顺时针方向度量的角(以度为单位)。
  • sweepAngle:从startAngle參数到弧线的结束点沿顺时针方向度量的角(以度为单位)。
(2)绘制一段弧线。它表示由一对坐标、宽度和高度指定的椭圆部分。
public void DrawArc(Pen pen, int x, int y, int width, int height, int startAngle, int sweepAngle)

绘制圆弧的实例代码:

        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Blue, 5);
Rectangle myRectangle = new Rectangle(70, 20, 100, 60);
graphics.DrawArc(myPen, myRectangle,210,120);
}

绘制多边形

       须要Graphics对象、Pen对象和Point(或PointF)对象数组。

Graphics类提供DrawPolygon方法,Pen对象存储用于呈现多边形的线条属性,如宽度和颜色等,Point(或PointF)对象数组存储多边形的各个顶点。

可重载。

(1)绘制由一组Point结构定义的多边形。
public void DrawPolygon(Pen pen, Point[] pints)

(2)绘制由一组PointF结构定义的多边形。

public void DrawPolygon(Pen pen, PointF[] pints)

绘制多边形演示样例代码:

        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Red, 5);
Point point1 = new Point(80, 20);
Point point2 = new Point(40, 50);
Point point3 = new Point(80, 80);
Point point4 = new Point(160, 80);
Point point5 = new Point(200, 50);
Point point6 = new Point(160, 20);
Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
graphics.DrawPolygon(myPen, myPoints);
}

绘制基数样条

        基数样条是一连串单独的曲线,连接起来组成较大的曲线。由点的数组和张力參数指定,样条平滑地经过数组的每一个点。曲线的陡度上没有尖角和突然的变化。
(1)绘制经过一组指定Point结构的基数样条。

public void DrawCurve(Pen pen, Point[] points)

(2)使用指定的张力,绘制经过一组指定Point结构的基数样条。

public void DrawCurve(Pen pen, Point[] points, float tension)
        tension:大于或等于0.0F的值,指定曲线的张力。

(3)从相对于数组開始位置的偏移量開始。绘制经过一组指定PointF结构的基数样条。
public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments)
  • offset:从points參数数组中的第一个元素到曲线中起始点的偏移量。
  • numberOfSegments:起始点之后要包括在曲线中的段数。

(4)使用指定张力,绘制经过一组指定Point结构的基数样条。

public void DrawCurve(Pen pen, Point[] points, int offset, int numberOfSegments, float tension)

绘制基数样条演示样例代码:

        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Red, 5);
Point point1 = new Point(50, 20);
Point point2 = new Point(60, 30);
Point point3 = new Point(70, 25);
Point point4 = new Point(100, 50);
Point point5 = new Point(130, 30);
Point point6 = new Point(150, 45);
Point[] myPoints = { point1, point2, point3, point4, point5, point6 };
graphics.DrawCurve(myPen, myPoints, 1.0F);
}

绘制贝赛尔样条

        贝塞尔样条是由4个点指定的曲线:两个端点(p1,p2)和两个控制点(c1,c2)。曲线開始于p1,结束于p2。曲线不经过控制点。可是控制点像磁铁一样,在某些方向上拉伸曲线并影响曲线弯曲的方式。
        调用Graphics类的DrawBezier方法。可重载。

(1)绘制由4个Point结构定义的贝塞尔样条。

public void DrawBezier(Pen pen, Point pt1, Point pt2, Point pt3, Point pt4)

4个Point点分别表示起始点、第一个控制点、第二个控制点和结束点。

(2)绘制由4个表示点的有序坐标对定义的贝塞尔样条。

public void DrawBezier(Pen pen, float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)

x2,y2及x3,y3分别表示第1个、第2个控制点对应坐标。顺序和第一种方法类似。

        绘制贝塞尔样条演示样例代码:

        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
Pen myPen = new Pen(Color.Red, 5);
float startX = 50.0F;
float startY = 80.0F;
float controlX1 = 150.0F;
float controlY1 = 20.0F;
float controlX2 = 230.0F;
float controlY2 = 50.0F;
float endX = 190.0F;
float endY = 80.0F;
graphics.DrawBezier(myPen, startX, startY, controlX1, controlY1, controlX2, controlY2, endX, endY);
}

绘制图形路径

        路径是通过组合直线、矩形和简单的曲线而形成的。在GDI+中,GraphicsPath对象同意将基本构造块收集到一个单元中,调用一次Graphics类的DrawPath方法。就能够绘制出整个单元的直线、矩形、多边形和曲线。

public void DrawPath(Pen pen, GraphicsPath path)
  • pen:Pen对象。确定线条颜色、宽度和样式。
  • path:要绘制的GraphicsPath图形路径。
        PS:注意要引用System.Drawing.Drawing2D命名空间。

        绘制图形路径演示样例代码:
        private void button1_Click(object sender, EventArgs e)
{
Graphics graphics = this.CreateGraphics();
GraphicsPath myGraphicsPath = new GraphicsPath();
Pen myPen = new Pen(Color.Blue, 1);
Point[] myPoints = { new Point(15, 30), new Point(30, 40), new Point(50, 30) };
myGraphicsPath.AddArc(15, 20, 80, 50, 210, 120);
myGraphicsPath.StartFigure();
myGraphicsPath.AddCurve(myPoints);
myGraphicsPath.AddString("图形路径", new FontFamily("华文行楷"), (int)FontStyle.Underline, 50, new PointF(20, 50), new StringFormat());
myGraphicsPath.AddPie(180,20,80,50,210,120);
graphics.DrawPath(myPen, myGraphicsPath);
}

C# GDI+技术的更多相关文章

  1. MVC的验证(模型注解和非侵入式脚本的结合使用) .Net中初探Redis .net通过代码发送邮件 Log4net (Log for .net) 使用GDI技术创建ASP.NET验证码 Razor模板引擎 (RazorEngine) .Net程序员应该掌握的正则表达式

    MVC的验证(模型注解和非侵入式脚本的结合使用)   @HtmlHrlper方式创建的标签,会自动生成一些属性,其中一些属性就是关于验证 如图示例: 模型注解 通过模型注解后,MVC的验证,包括前台客 ...

  2. C# :GDI+技术生成复杂型彩色验证码(转载)

    该类是生成一个验证码的类,集合了网上大部分的C#关于GDI+的文章进行多次改进,现在已经形成了可在生产环节中使用的验证码. 该验证码加入了背景噪点,背景噪点曲线和直线,背景噪点文字以及扭曲,调暗,模糊 ...

  3. GDI+技术

    GDI+是GDI的后继者,它是一种构成 Windows XP 操作系统的子系统的应用程序编程接口. 一般来说有3种基本类型的绘图界面,分别为Windows 窗体上的控件.要发送给打印机的页面和内存中的 ...

  4. 使用GDI技术创建ASP.NET验证码

    using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Dr ...

  5. C#-gdi绘图,双缓冲绘图,Paint事件的触发

    一. 画面闪烁问题与双缓冲技术 1.1 导致画面闪烁的关键原因分析: 1  绘制窗口由于大小位置状态改变进行重绘操作时 绘图窗口内容或大小每改变一次,都要调用Paint事件进行重绘操作,该操作会使画面 ...

  6. C#-gdi画图,双缓冲画图,Paint事件的触发---ShinePans

    在使用gdi技术画图时,有时会发现图形线条不够流畅,或者在改变窗口大小时会闪烁不断的现象.(Use DoubleBuffer to solve it!)                         ...

  7. Excel阅读模式/单元格行列指示/聚光灯开发 技术要点再分享

    1. 引言 文题中所谓技术要点再分享,本意是想在大神Charltsing Liu的博文“简单介绍Excel单元格行列指示的实现原理(俗称聚光灯功能)”的基础上写一点个人开发体会.写本文的初衷有三点,一 ...

  8. WPF GDI+字符串绘制成图片(二)

    原文:WPF GDI+字符串绘制成图片(二) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...

  9. WPF GDI+字符串绘制成图片(一)

    原文:WPF GDI+字符串绘制成图片(一) 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/BYH371256/article/details/83 ...

随机推荐

  1. 使用gSOAP工具生成onvif框架代码

    <工具产生背景>          由于SOAP是一种基于xml的文件,手动编写SOAP文件太机械耗时,在这种背景下产生了gSAOP 这个工具,用于生成各种类型的代码,目前支持C/C++, ...

  2. luoguP4208 [JSOI2008]最小生成树计数 矩阵树定理

    题目大意: 求最小生成树的数量 曾今的我感觉这题十分的不可做 然而今天看了看,好像是个类模板的题.... 我们十分容易知道,记能出现在最小生成树中的边的集合为\(S\) 那么,只要是\(S\)中的边构 ...

  3. 【干货】PHP常见危险函数

    passthru() 功能描述:允许执行一个外部程序并回显输出,类似于 exec(). 危险等级:高 exec() 功能描述:允许执行一个外部程序(如 UNIX Shell 或 CMD 命令等). 危 ...

  4. hash课堂测试补分博客

    题目要求: 开放地址法: 概念: 所谓的开放定址法就是一旦发生了冲突,就去寻找下一个空的散列地址,只要散列表足够大,空的散列地址总能找到,并将记录存入. 它的公式为: 解题过程(在下图中): 拉链法: ...

  5. bzoj 1934 最小割

    收获: 1.流量为0的边可以不加入. 2.最小割方案要与决策方案对应. #include <cstdio> #include <cmath> #include <cstr ...

  6. BZOJ 3876: [Ahoi2014]支线剧情 带下界的费用流

    3876: [Ahoi2014]支线剧情 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=3876 Description [故事背景] 宅 ...

  7. ProtoBuffer使用笔记

    ProtoBuffer是由谷歌研发的对象序列化和反序列化的开源工具,ProtoBuffer和Xml类似,都是数据描述工具,后者使用更为广泛,前者Google内部使用且具有更高的效率.该工具安装和使用都 ...

  8. 无法完成你的itunes store 请求发生未知错误50

    装上itunes登陆itunes store时遂发现"无法完成您的itunes store的请求,发生未知错误(-50)"跃入眼帘,卸载重装数次还是不见效果,难道是WIN7和itu ...

  9. Visual Studio中Debug和Release的区别

    在Visual Studio中,生成应用程序的时候有2种模式:Debug和Release.两者之间如何取舍呢? 假设有这么简单的一段代码,在主程序中调用方法M1,M1方法调用M2方法,M2方法调用M3 ...

  10. [翻译] Working with NSURLSession: AFNetworking 2.0

    Working with NSURLSession: AFNetworking 2.0   简单翻译,有很多错误,看官无法理解处请英文原文对照. http://code.tutsplus.com/tu ...