C#中图形编程

浅析C#中图形编程

本文关键词: C# 图形 编程

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());
}

}

 

 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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、付费专栏及课程。

余额充值