C#图形编程GDI+基础

    很长一段时间没有更新技术博客了,最近在用C#开发Kinect的程序。所以跟大家分享一下自己一些方面学习和认知。

 

    编写图形程序时需要使用GDI(Graphics Device Interface,图形设备接口),从程序设计的角度看,GDI包括两部分:一部分是GDI对象,另一部分是GDI函数。GDI对象定义了GDI函数使用的工具和环境变量,而GDI函数使用GDI对象绘制各种图形,在C#中,进行图形程序编写时用到的是GDI+(Graphics Device Interface Plus图形设备接口)版本,GDI+是GDI的进一步扩展,它使我们编程更加方便。

 

    C#中的GDI+就相当于java中的Swing控件,是编写图形界面必不可缺的一个接口。GDI+绘图最大的方便得益于C#的可视化编程,所有的控件只需要自己Drag,然后Place,最后Cilck添加监听方法。真的是too young too simple。

 

    绘图的时候要用到Graphics类,所以首先介绍一下Graphics画布

 

    我们首先要创建一个Graphics类实例,这个实例相当于建立了一块画布,有了画布才可以用各种画图方法进行绘图。这个过程其实与java中Swing控件获取画布形式上差不多。

 

    绘图程序的设计过程一般分为两个步骤:(一)创建Graphics对象;(二)使用Graphics对象的方法绘图、显示文本或处理图像。

 

通常我们使用下述三种方法来创建一个Graphics对象。

 

  方法一、利用控件或窗体的Paint事件中的PainEventArgs 

在窗体或控件的Paint事件中接收对图形对象的引用,作为PaintEventArgs(PaintEventArgs指定绘制控件所用的Graphics)的一部分,在为控件创建绘制代码时,通常会使用此方法来获取对图形对象的引用。

 

例如: 

//窗体的Paint事件的响应方法
private void form1_Paint(object sender, PaintEventArgs e)
{
    Graphics g = e.Graphics;
}

 

 

 

也可以直接重载控件或窗体的OnPaint方法,具体代码如下所示: 

 

protected override void OnPaint(PaintEventArgs e)
{
    Graphics g = e.Graphics;
}

  

Paint事件在重绘控件时发生。

 

 

方法二、调用某控件或窗体的CreateGraphics方法 

调用某控件或窗体的CreateGraphics方法以获取对Graphics对象的引用,该对象表示该控件或窗体的绘图图面。如果想在已存在的窗体或控件上绘图,通常会使用此方法。

 

例如: 

 

Graphics g = this.CreateGraphics();

 

 

方法三、调用Graphics类的FromImage静态方法 

由从Image继承的任何对象创建Graphics对象。在需要更改已存在的图像时,通常会使用此方法。

 

例如: 

//名为“g1.jpg”的图片位于当前路径下
Image img = Image.FromFile("g1.jpg");//建立Image对象
Graphics g = Graphics.FromImage(img);//创建Graphics对象
    

 

一般来说,个人第二种方法用的比较多。

 

再就是常用的画图对象

Pen:用来用patterns、colors或者bitmaps进行填充。

Color:用来画线和多边形,包括矩形、圆和饼形。

Font:用来给文字设置字体格式。

Brush:用来描述颜色。

Rectangle:矩形结构通常用来在窗体上画矩形。

Point:描述一对有序的x,y两个坐标值。

 

每个画图对象的具体方法在此就不详细说明了,详细内容在开发文档里面。

 

基本图形的绘制实例

 

绘制渐变的矩形



 

代码

        /// <summary>
        /// 重载Form窗体的OnPaint函数
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            //定义矩形区域
            Rectangle rect = new Rectangle(50, 30, 100, 100);
            //定义画刷对象
            LinearGradientBrush lBrush = new LinearGradientBrush(rect, Color.Red, Color.Yellow, LinearGradientMode.BackwardDiagonal);
            //填充矩形
            g.FillRectangle(lBrush, rect);
        }

 



 

 

其他图形也都可以调用系统提供的绘图方法实现

如DrawArc,DrawLine,DrawEllipse,DrawString,FillPath等等,都可以尝试使用一下

 

如果以上介绍你对C#中的GDI+编程还不懂,那么来下载个开发文档吧。鉴于以前上传的代码小伙伴们都不能下载。估计是被优快云收购后冷落其发展的原因,所以以后代码资源都会挂到优快云上,并附上地址。

 

GDI+开发文档已经上传到:

http://download.youkuaiyun.com/detail/u011458382/6336381

 

希望大家多多支持和指正。

 

 

This Wrox Blox teaches you how to add graphics to C# 2008 applications, explaining fundamental graphics techniques such as: drawing shapes with different colors and line styles; filling areas with colors, gradients, and patterns; drawing text that is properly aligned, sized, and clipped exactly where you want it; manipulating images and saving results in bitmap, JPEG, and other types of files. Also covered are instructions for how to greatly increase your graphics capabilities using transformations. Transformations allow you to move, stretch, or rotate graphics. They also let you work in coordinate systems that make sense for your application. You will also learn how to use all of these techniques in printouts. The author describes the sequence of events that produce a printout and shows how to generate and preview printouts. The final sections describe two powerful new graphic tools that were introduced with .NET Framework 3.0: WPF graphics and FlowDocuments. WPF applications can use XAML graphic commands to declaratively draw and fill the same kinds of shapes that a program can draw by using graphics objects. Finally, a discussion on the FlowDocument object shows you how to define items that should be flowed across multiple pages as space permits. This lets you display text, graphics, controls, and other items that automatically flow across page breaks. FlowDocument viewers make displaying these documents easy for you, and simplifies the user's reading of the documents. This Wrox Blox also contains 35 example programs written in C# 2008, although most of the code works in previous versions of C# as well. The most notable exceptions are WPF graphics and FlowDocuments, both of which require WPF provided in .NET Framework 3.0 and later.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值