浅析C#中图形编程

2016-02-19 16:56 9 1 收藏

想不想get新技能酷炫一下,今天图老师小编就跟大家分享个简单的浅析C#中图形编程教程,一起来看看吧!超容易上手~

【 tulaoshi.com - 编程语言 】

  像Java一样,C#提供了一整套相当丰富的类库、方法以及事件以供开发者使用。C#还引入了GDI+,它是由GDI演变而来的,具有比GDI更强大的功能而且简化了程序员的编程工作。所以开发者运用这些,就可以很方便的开发出具有强大图形图像功能的应用程序了。本文,笔者就通过一些实例像读者介绍一下C#中的图形编程的基本知识。

  简单实例:

  首先,让我们从例子开始,以下是一个最简单的实例:

  

using System;using System.Windows.Forms;using System.Drawing;public class Hello:Form {public Hello() {this.Paint += new PaintEventHandler(f1_paint);}private void f1_paint(object sender,PaintEventArgs e) {Graphics g = e.Graphics;g.DrawString("你好,C#!",new Font("Verdana",20),new SolidBrush(Color.Tomato),40,40);g.DrawRectangle(new Pen(Color.Pink,3),20,20,150,100);}public static void Main() {Application.Run(new Hello());}} 

  在上面的实例中,我们用到了一个方法:DrawString(),它带有5个参数。同时,我们发现在运用DrawString()方法以前,我们先创建了一个Graphics类型的对象g=e.Graphics,这就说明了在运用任何图形类的方法以前我们必须先创建该类的一个实例化对象。在DrawString()方法后,我们用到了DrawRectangle()方法,其实我们还可以运用其他的方法来画椭圆或是多边形等等。第一个实例还是相当简单易懂的,不是吗?

  变换图形的度量单位:

  在图形编程中,默认的图形度量单位是象素。不过,你可以通过修改PageUnit属性来修改图形的度量单位,可以是英寸或是毫米等。实现方法如下:

  

Graphics g = e.Graphics;g.PageUnit = GraphicsUnit.Inch 

  操作颜色选择对话框:

  在实际运用特别是图形图像编程过程中,我们可能会经常碰到颜色选择对话框(以及下面要提到的字体选择对话框)。使用颜色选择对话框,我们可以让用户来选择系统预定的颜色以及用户自定义的颜色。在使用颜色选择对话框之前,我们必须先创建一个ColorDialog类型的对象:

  

ColorDialog cd = new ColorDialog(); 

  然后,我们就可以用ShowDialog()方法来显示颜色选择对话框了。之后,就可以通过调用用户的颜色选择进行相关的图形操作了。

  以下,我给大家一个实例。该实例中有一个按钮和一个文本框,通过点击按钮可以调出颜色选择对话框,根据用户的颜色选择就可以设置文本框的背景颜色了。

  

using System;using System.Drawing;using System.Windows.Forms;public class Clr:Form{Button b1 = new Button();TextBox tb = new TextBox();ColorDialog clg = new ColorDialog();public Clr(){b1.Click += new EventHandler(b1_click);b1.Text = "选择颜色";tb.Location = new Point(50,50);this.Controls.Add(b1);this.Controls.Add(tb);}public void b1_click(object sender, EventArgs e){clg.ShowDialog();tb.BackColor = clg.Color;}public static void Main() {Application.Run(new Clr());}}

  操作字体选择对话框:

  字体是图形编程的一个重要组成部分,通过设置不同的字体,你可以在程序中达到不同的视觉效果。和以上的颜色选择对话框的创建差不多,你可以很方便地创建一个字体选择对话框,并通过它来让用户选择其所需的字体。

  下面同样给出一个实例,这个实例和上面的实例差不多,只是用来字体选择对话框代替了原来的颜色选择对话框,最后是根据用户的字体选择来设置文本框的字体。

  

using System;using System.Drawing;using System.Windows.Forms;public class Fonts:Form {Button b1 = new Button();TextBox tb = new TextBox();FontDialog flg = new FontDialog();public Fonts() {b1.Click += new EventHandler(b1_click);b1.Text = "选择字体";tb.Location = new Point(50,50);this.Controls.Add(b1);this.Controls.Add(tb);}public void b1_click(object sender, EventArgs e) {clg.ShowDialog();tb.FontName = flg.Font;}public static void Main() {Application.Run(new Fonts());}} 

  使用System.Drawing.Drawing2D名字空间:

  如果你有一些图形图像编程的经验,那么你一定知道画笔和画刷的概念。它们在图形编程有非常广泛的运用。System.Drawing.Drawing2D名字空间提供了相当强大的功能,能让开发者很容易地操作画笔以及画刷对象。比如,你可以通过设置画笔的DashStyle属性(有Dash、DashDot、Solid等风格)来确定直线的风格。同样,通过运用SolidBrush、HatchBrush、GradientBrush等画笔你可以很轻易地修改被填充区域的外观。比如,你可以用SolidBrush将一个矩形区域用许许多多不同粗细的直线来填充。那么,我们在什么时候运用画笔和画刷呢?就像上面的例子中那样,通常一个图形轮廓(运用DrawXXX()方法)是用画笔对象来实现的,而一个填充区域(运用FillXXX()方法)则是用画刷对象来实现的。

  使用画笔对象:

  在下面的实例中,我们用到了System.Drawing.Drawing2D名字空间。实例中我们用画笔以不同的风格画了直线、椭圆、馅饼图形、多边形等图形。

  

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;public class Drawgra:Form {public Drawgra() {this.Text = "运用画笔示例";this.Size = new Size(450,400);this.Paint += new PaintEventHandler(Draw_Graphics);}public void Draw_Graphics(object sender,PaintEventArgs e) {Graphics g = e.Graphics;Pen penline = new Pen(Color.Red,5);Pen penellipse = new Pen(Color.Blue,5);Pen penpie = new Pen(Color.Tomato,3);Pen penpolygon = new Pen(Color.Maroon,4);/*DashStyle有Dash、DashDot、DashDotDot、Dot、Solid等风格*///以Dash风格画一条直线penline.DashStyle = DashStyle.Dash;g.DrawLine(penline,50,50,100,200);//以DashDotDot风格画一个椭圆penellipse.DashStyle = DashStyle.DashDotDot;g.DrawEllipse(penellipse,15,15,50,50);//以Dot风格画一个馅饼图形penpie.DashStyle = DashStyle.Dot;g.DrawPie(penpie,90,80,140,40,120,100);//以Solid风格画一个多边形g.DrawPolygon(penpolygon,new Point[]{new Point(30,140),new Point(270,250),new Point(110,240),new Point(200,170),new Point(70,350),new Point(50,200)});}public static void Main() {Application.Run(new Drawgra());}} 

  使用画刷对象:

  画刷对象是用特定的颜色、模式或是图像来填充一块区域的。总共有四种类型的画刷:SolidBrush(默认的画刷)、HatchBrush、GradientBrush以及TexturedBrush。下面,我们分别给出实例来进行介绍。

  1、运用SolidBrush:

  

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;public class Solidbru:Form {public Solidbru() {this.Text = "运用SolidBrush示例";this.Paint += new PaintEventHandler(Fill_Graph);}public void Fill_Graph(object sender,PaintEventArgs e) {Graphics g = e.Graphics;//创建一把SolidBrush并用它来填充一个矩形区域SolidBrush sb = new SolidBrush(Color.Pink);g.FillRectangle(sb,50,50,150,150);}public static void Main() {Application.Run(new Solidbru());}} 

  2、运用HatchBrush:

  

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;public class Hatchbru:Form {public Hatchbru() {this.Text = "运用HatchBrush示例";this.Paint += new PaintEventHandler(Fill_Graph);}public void Fill_Graph(object sender,PaintEventArgs e) {Graphics g = e.Graphics;//创建一把HatchBrush并用它来填充一个矩形区域/*该画刷的HatchStyle有DiagonalCross、ForwardDiagonal、Horizontal、 Vertical、 Solid等不同风格 */HatchStyle hs = HatchStyle.Cross;HatchBrush sb = new HatchBrush(hs,Color.Blue,Color.Red);g.FillRectangle(sb,50,50,150,150);}public static void Main() {Application.Run(new Hatchbru());}} 

  3、运用GradientBrush:

  GradientBrush又可分为LinearGradientBrush和PathGradientBrush两种,从它们的名称我们可以知道前者是线性渐变的,而后者则是路径渐变的,因而能创造出更复杂和完美的效果。下面我就给大家分别举例:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  1)、运用LinearGradientBrush:

(本文来源于图老师网站,更多请访问https://www.tulaoshi.com/bianchengyuyan/)

  

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;public class LinearGradientbru:Form {public LinearGradientbru() {this.Text = "运用LinearGradientBrush示例";this.Paint += new PaintEventHandler(Fill_Graph);}public void Fill_Graph(object sender,PaintEventArgs e) {Rectangle r = new Rectangle(500, 300, 100, 100);LinearGradientBrush lb = new LinearGradientBrush(r, Color.Red, Color.Yellow,LinearGradientMode.BackwardDiagonal);e.Graphics.FillRectangle(lb, r);}public static void Main() {Application.Run(new LinearGradientbru());}}

  所得图形如下:

  2)、运用PathGradientBrush:

  

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;public class PathGradientbru:Form {public PathGradientbru() {this.Text = "运用PathGradientBrush示例";this.Paint += new PaintEventHandler(Fill_Graph);}public void Fill_Graph(object sender,PaintEventArgs e) {e.Graphics.TextRenderingHint = TextRenderingHint.AntiAliased;e.Graphics.FillRectangle(backgroundBrush, ClientRectangle);e.Graphics.FillRectangle(new SolidBrush(Color.FromArgb(180, Color.White)), ClientRectangle);//先设置好一个路径GraphicsPath path = new GraphicsPath(new Point[] {new Point(40, 140),new Point(275, 200),new Point(105, 225),new Point(190, 300),new Point(50, 350),new Point(20, 180),}, new byte[] {(byte)PathPointType.Start,(byte)PathPointType.Bezier,(byte)PathPointType.Bezier,(byte)PathPointType.Bezier,(byte)PathPointType.Line,(byte)PathPointType.Line,});//创建一把PathGradientBrushPathGradientBrush pgb = new PathGradientBrush(path);//设置画刷的周围颜色pgb.SurroundColors = new Color[] { Color.Green, Color.Yellow, Color.Red, Color.Blue, Color.Orange, Color.White,};//用画刷进行填充e.Graphics.FillPath(pgb, path);}public static void Main() {Application.Run(new PathGradientbru());}} 

  所得图形如下:

  4、运用TexturedBrush:

  

using System;using System.Windows.Forms;using System.Drawing;using System.Drawing.Drawing2D;public class Texturedbru:Form {Brush bgbrush;public Texturedbru() {//创建一幅图像以供填充椭圆的背景用Image bgimage = new Bitmap("dotnet.gif");bgbrush = new TextureBrush(bgimage);this.Paint+=new PaintEventHandler(Text_bru);}public void Text_bru(object sender,PaintEventArgs e) {Graphics g = e.Graphics;g.FillEllipse(bgbrush,50,50,500,300);}public static void Main() {Application.Run(new Texturedbru());}} 

  使用图像:

  图像在图形编程中经常要用到的,其对用户界面的作用也是非常明显的。在以前的编程过程中,对图像的操作细节是相当繁琐的而且很容易出错。现在,在GDI+下面,你可以用C#语言很容易的完成你所希望的图像编程。

  很简单,你只要通过以下步骤就可以实现图像的编程。

  1、 创建一个位图类对象如下:

  Image img = new Bitmap("image.bmp");

  2、 在DrawImage()方法中运用上述对象:

  g.DrawImage(img,20,20,100,90);

  至于使用图像的实例,限于篇幅,我就不再这里介绍了。相信大家能很容易地完成一个运用图像的实例的。

  总结:

  在这篇文章中,我主要用到了两个非常核心的名字空间:一个是System.Drawing、一个是System.Drawing.Drawing2D。有了它们,我们就可以很方便的调用其中的方法、属性来实现以往进行图形编程需要付出很大代价才能完成的任务,这不能不说是GDI+给我们带来的优点。所以,掌握好GDI+,我相信你的图形编程能力会更上一层楼的。

来源:https://www.tulaoshi.com/n/20160219/1613591.html

延伸阅读
我们在写Remoting程序或者其他的一些应用程序的时候难免要和线程打交道,.Net使我们很容易就可以创建一个线程,但是它提供的创建线程和启动线程的方法没有明显的提供参数,假如我们要用线程来启动类里面一个带参数的方法该怎么办?下面就简单的介绍如何使用.NET提供的丰富的框架来实现这个功能。为了可以生动详细的介绍整个过程,我建立下面的一...
在组件编程中对事件的理解是十分重要的,C# 中的事件是当对象发生某些有趣的事情时,类向该类的客户提供通知的一种方法。与事件联系最为紧密的,个人认为是委托.委托可以将方法引用封装在委托对象内。为了弄清组件-事件-委托三者的关系,本人用实际的例子来谈 谈小弟的理解。 首先创建一个Windows控件项目,添加如下控件样板。 ...
Visual C# 2005在变量类型、泛型等方面都作了一些强化,基本上,可以将此细分为下列几个部分: 要增进程序编写的效率,利用程序代码段是非常多程序设计员使用的方法,深入地了解程序代码段将为您带来如虎添翼的效果。 使用变量之前,一定要先声明变量类型。Visual C# 2005 新增了Null类型,让变量在处理数据时能够获得更大的弹性...
在2005年底微软公司正式发布了C# 2.0,与C# 1.x相比,新版本增加了很多新特性,其中最重要的是对泛型的支持。通过泛型,我们可以定义类型安全的数据结构,而无需使用实际的数据类型。这能显著提高性能并得到更高质量的代码。泛型并不是什么新鲜的东西,他在功能上类似于C++的模板,模板多年前就已存在C++上了,并且在C++上有大量成熟应用。...
单个写入程序/多个阅读程序在.Net类库中其实已经提供了实现,即System.Threading.ReaderWriterLock类。本文通过对常见的单个写入/多个阅读程序的分析来探索c#的多线程编程。 问题的提出 所谓单个写入程序/多个阅读程序的线程同步问题,是指任意数量的线程访问共享资源时,写入程序(线程)需要修改共享资源,而阅读程序(线程)...

经验教程

180

收藏

50
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部