Graphics类是GDI+的核心,Graphics对象表示GDI+绘图表面,提供将对象绘制到显示设备的方法。
1.创建Graphics对象的方法
(1.)在窗体或控件的paint事件中创建,将其作为PaintEventArgs的一部分。
private void Form1_Paint(object sender,PaintEventArgs e)
{
Graphics g=e.Graphics;
}
(2.)调用控件或窗体的Create Graphics方法获取对Graphics对象的引用,该对象表示控件或窗体的绘图画面。在已经存在的窗体或控件上绘图,使用此方法。
private void Form1_Paint(object sender,PaintEventArgs e)
{
Graphics g;
g=this.CreateGraphics();
}
(3.)由从Image继承的任何对象创建Graphics对象,此方法在需要更改已存在的图像时十分有用。
private void Form1_Load(object sender,EventArgs e)
{
Bitmap mbit=new Bitmip(@"C:\mr.bmp");
Graphics g=Graphics.FormImage(mbit);
}
2.画笔
Pen类主要用于设置画笔。
//构造
//public Pen (Color color,float width)
//color:设置Pen的颜色
//width:设置Pen的宽度
Pen myPen=new Pen(Color.Blue,2);
3.画刷
Brush类,设置画刷,填充几何图形。Brush类是一个抽象基类,不能进行实例化,使用派生类实例化对象。
(1.)SolidBrush类
定义单色画刷,画刷用于填充图形形状。
//public SolidBrush(Color color)
Brush mybs=new SolidBrush(Color.Red);
(2.)HatchBrush类
一种特定样式的图形,填充满整个封闭区域。
//public HatchBrush(HatchStyle hatchstyle,Color foreColor)//图案,颜色
//在使用HatchBrush类时,必须添加System.Drawing.Drawing2D命名空间
Brush brush = new HatchBrush(HatchStyle.Cross,Color.Red);
(3.)LinerGradientBrush类
渐变色特效,填满图形的内部区域。
//public LinearGradientBrush(Point point1,Point point2,Color color1,Color color2)
//point1 线性渐变开始点 point2 线性渐变结束点
//color1 开始颜色 color2 结束色
Point p1=new Point(100,100);
Point p2=new Point(150,150);
LinearGradientBrush brush = new LinearGradientBrush(p1,p2,Color.red,Color.Blue);
4.绘制几何图形
g.DrawArc(pen,100,100,100,50,270,200);//弧线 p1,p2,p3
g.DrawLine(pen,10,10,50,10);//直线p1,p2
g.DrawString("aaaa",new Font("楷体",16),brush,10,10);
5.绘制图形,绘制验证码
(1.)新建Windows窗体应用程序,在Form1中添加PictureBox控件,显示图形验证码;button控件,生成图形验证码;
(2.)代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
///
///绘制图形
namespace GDI_Graphics
{
public partial class Form1 : Form
{
private string CheckCode()//使用此方法生成验证码
{
int number;
char code;
string checkCode=String.Empty;//声明变量存储随机生成的4个英文字母或数字
Random random = new Random();//生成随机数
for(int i = 0; i < 4; i++)
{
number = random.Next();//返回非负随机数
if(number%2 == 0)//判断是否为偶数
{
code = (char)('0' + (char)(number % 10));
}
else//不是偶数
{
code = (char)('A' + (char)(number % 26));
}
checkCode += ""+code.ToString();//累加字符串
}
return checkCode;
}
private void CodeImage(string checkCode)//将生成的验证码绘制成图形,并将该图片显示在PictureBox控件中
{
if (checkCode == null || checkCode.Trim() == String.Empty)
return;
Bitmap image=new Bitmap((int)Math.Ceiling((checkCode.Length*12.5)),22);
Graphics g= Graphics.FromImage(image);
try
{
Random random = new Random();//生成随机生成器
g.Clear(Color.White);//清空图片背景色
for(int i = 0;i < 3;i++) { //画图片的背景噪音线
int x1=random.Next(image.Width);
int x2=random.Next(image.Width);
int y1=random.Next(image.Height);
int y2=random.Next(image.Height);
g.DrawLine(new Pen(Color.Black), x1, y1, x2, y2);
}
Font font = new Font("Arial", 12, (FontStyle.Bold));
g.DrawString(checkCode, font,new SolidBrush(Color.Red),2,2);
for(int i = 0; i < 150;i++)//画图片的前景噪音线
{
int x=random.Next(image.Width);
int y=random.Next(image.Height);
image.SetPixel(x, y, Color.FromArgb(random.Next()));
}
//画图片的边框线
g.DrawRectangle(new Pen(Color.Silver),0,0,image.Width-1,image.Height-1);
this.pictureBox1.Width = image.Width;//设置pictureBox1的宽度
this.pictureBox1.Height = image.Height;//设置pictureBox1的高度
this.pictureBox1.BackgroundImage = image;//设置pictureBox1的背景图片
}
catch
{
}
}
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
private void Form1_Load(object sender, EventArgs e)
{
CodeImage(CheckCode());
}
private void button2_Click(object sender, EventArgs e)
{
Form2 form2 = new Form2();
form2.Show();
}
}
}
6.绘制图像
(1.)DrawImage方法
public void DrawImage(Image image,int x,int y)
public void DrawImage(Image image,int x,int y,int width,int height)
(2.)代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
///
///绘制图像
namespace GDI_Graphics
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
}
private void Form2_Paint(object sender, PaintEventArgs e)
{
Image image = Image.FromFile("jiliang.jpg");
Graphics g = this.CreateGraphics();
g.DrawImage(image, 0, 0,150,150);
}
}
}