GDI+绘图
- 简介
- System.Drawing命名空间
- System.Drawing命名空间中的常用类:
- System.Drawing命名空间中的常用结构:
- Graphics 类
- Pen类
- Brush类
- Font类
- PictureBox图像控件
- Bitmap类
简介
GDI(Graphics Device Interface)图像设备接口,属于绘图方面的API。System.Drawing命名空间提供了对GDI+基本图形功能的访问。
System.Drawing命名空间
System.Drawing命名空间中的常用类:
类 | 说明 |
---|---|
Bitmap | 封装GDI+位图,此位图由图形图像及其属性的像素数据组成。 |
Brush | 画刷,定义用于填充图形形状(如:矩形、椭圆、饼图、多边形和封闭路径)的内部对象 |
Font | 字体,定义特定的文本格式,包括:字体、字号和字形属性。无法继承此类 |
Graphics | 封装一个GDI+绘图图面。无法继承此类 |
Pen | 画笔,定义用于绘制直线和曲线的对象。无法继承此类 |
Region | 指示由矩形和路径构成的图形形状的内部。无法继承此类 |
System.Drawing命名空间中的常用结构:
结构 | 说明 |
---|---|
Color | 表示RGB颜色 |
Point | 表示二维平面中定义的点、整数X和Y坐标的有序对 |
Rectangle | 存储一组整数,共4个,表示一个矩形的位置和大小 |
Size | 存储一个有序整数对,通常为矩形的宽度和高度 |
Graphics 类
Graphics类封装了一个GDI+绘图界面,提供将对象绘制到显示设备的方法。
Graphics类的常用方法:
名称 | 说明 |
---|---|
Dispose | 释放由Graphics使用的所有资源 |
DrawEllipse | 绘制一个由边框(该边框由一对坐标、高度和宽度指定)定义的椭圆 |
DrawArc | 绘制弧形 |
DrawLine | 绘制直线 |
DrawPolygon | 绘制由一组Point结构定义的多边形 |
DrawRectangle | 绘制由坐标对、宽度和高度指定的矩形 |
DrawPie | 绘制一个扇形,该形状由一个坐标对、宽度、高度以及两天射线所指定的椭圆定义 |
DrawCurse | 绘制曲线,由参数Point数组指定 |
FillEllips | 填充边框所定义的椭圆的内部,该边框由一对坐标、一个宽度和一个高度指定 |
FillRegion | 填充Region的内部 |
ScaleTransform | 将指定的缩放操作应用与此Graphics |
TanslateTransform | 平移坐标系原点 |
Pen类
Pen类可以设置画笔的颜色,线条的粗细和线条的样式(实现、虚线等)。画笔是绘图的工具,Graphics对象是绘画的场所。
Brush类
Brush类(画刷),用于填充图形。该类是一个抽象基类,不能直接实例化。
Brush类的派生类:
名称 | 说明 |
---|---|
ImageBrush | 图像绘制区域 |
LinearGradientBrush | 线性渐变绘制区域 |
RadialGradientBrush | 径向渐变绘制区域,焦点定义渐变的开始,椭圆定义渐变的重点 |
SolidColorBrush | 单色绘制区域 |
VideoBrush | 视频内容绘制区域 |
Font类
Font类,绘制文本时,可以设置字体的样式、大小、以及字体的种类。
using System.Drawing.Drawing2D;
using System.Numerics;
using System.Security.Cryptography.Xml;
using System.Threading;
namespace GDIDemo
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 绘制直线
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Black);
g.DrawLine(p, 0, this.Height / 2, this.Width, this.Height / 2);
p.Dispose();
g.Dispose();
}
/// <summary>
/// 绘制矩形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Red);
g.DrawRectangle(p, 50, 50, 200, 200);
p.Dispose();
g.Dispose();
}
/// <summary>
/// 绘制圆形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen p = new Pen(Color.Blue);
g.DrawEllipse(p, 50, 50, 100, 100);
p.Dispose();
g.Dispose();
}
/// <summary>
/// 绘制圆柱
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button4_Click(object sender, EventArgs e)
{
int height = this.ClientSize.Height - 100;
int width = this.ClientSize.Width - 50;
int vHeight = 200;
int vWidth = 100;
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
Pen pen = new Pen(Color.Gray);
SolidBrush brush = new SolidBrush(Color.Gainsboro);
for (int i = height / 2; i > 0; i--)
{
g.DrawEllipse(pen, width / 2, i, vHeight, vWidth);
}
g.FillEllipse(brush, width / 2, 0, vHeight, vWidth);
}
/// <summary>
/// 绘制填充矩形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button5_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue);
Brush brush = pen.Brush;
Rectangle rect = new Rectangle(50, 50, 100, 100);
g.FillRectangle(brush, rect);
pen.Dispose();
g.Dispose();
}
/// <summary>
/// 绘制渐变圆形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button6_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Rectangle rect = new Rectangle(150, 50, 200, 200);
LinearGradientBrush brush = new LinearGradientBrush(rect, Color.Orange, Color.Purple, 90);
g.FillEllipse(brush, rect);
brush.Dispose();
g.Dispose();
}
/// <summary>
/// 绘制文字
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button7_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Pen pen = new Pen(Color.Blue);
Font f = new Font("隶书", 24, FontStyle.Italic);
g.DrawString("Windows 应用程序设计", f, pen.Brush, 50, 50);
pen.Dispose();
g.Dispose();
}
private void button9_Click(object sender, EventArgs e)
{
using (Graphics g = this.CreateGraphics())
{
g.Clear(Color.White);
}
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
#if false
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
Pen myPen = new Pen(Color.Red, 3);
g.DrawRectangle(myPen, 0, 0, 200, 100);
g.DrawEllipse(myPen, 0, 0, 200, 100);
g.Dispose();
myPen.Dispose();
#endif
}
/// <summary>
/// 坐标平移
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button8_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
Pen myPen = new Pen(Color.Red, 3);
g.DrawRectangle(myPen, 0, 0, 200, 100);
g.DrawEllipse(myPen, 0, 0, 200, 100);
Thread.Sleep(1000);
g.Clear(Color.White);
g.TranslateTransform(30, 30);
g.DrawRectangle(myPen, 0, 0, 200, 100);
g.DrawEllipse(myPen, 0, 0, 200, 100);
g.Dispose();
myPen.Dispose();
}
/// <summary>
/// 坐标缩放
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button10_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
Pen myPen = new Pen(Color.Red, 3);
g.DrawRectangle(myPen, 0, 0, 200, 100);
g.DrawEllipse(myPen, 0, 0, 200, 100);
Thread.Sleep(1000);
g.Clear(Color.White);
g.ScaleTransform(1.5f, 2.0f); // 横轴放到1.5倍,纵轴放大2倍
g.DrawRectangle(myPen, 0, 0, 200, 100);
g.DrawEllipse(myPen, 0, 0, 200, 100);
g.Dispose();
myPen.Dispose();
}
/// <summary>
/// 绘制波形
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button11_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
g.Clear(Color.White);
Pen pen = new Pen(Color.Blue, 3);
Point p1 = new Point(30, this.ClientSize.Height - 200);
Point p2 = new Point(this.ClientSize.Width - 100, this.ClientSize.Height - 200);
Point p3 = new Point(30, 30);
g.DrawLine(pen, p1, p2);
g.DrawLine(pen, p1, p3);
Font f = new Font("宋体", 12, FontStyle.Bold);
g.DrawString("x", f, pen.Brush, p2);
g.DrawString("y", f, pen.Brush, 10, 10);
double x1, x2, y1, y2, a;
x1 = x2 = y1 = y2 = 0;
y2 = this.ClientSize.Height - 200;
for (x2 = 0; x2 < this.ClientSize.Width; x2++)
{
a = 2 * Math.PI * x2 / this.ClientSize.Width;
y2 = Math.Sin(a);
y2 = (1 - y2) * (this.ClientSize.Height - 200) / 2;
g.DrawLine(pen, (int)(x1 + 30), (int)y1, (int)(x2 + 30), (int)y2);
x1 = x2;
y1 = y2;
}
}
/// <summary>
/// 绘制饼状图
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button12_Click(object sender, EventArgs e)
{
Graphics g = this.CreateGraphics();
Rectangle rect = new Rectangle(50, 50, 200, 100);
Brush brush = new SolidBrush(Color.Blue);
g.FillPie(brush, rect, 0, 60);
g.FillPie(brush, rect, 60, 150);
brush = new SolidBrush(Color.Yellow);
g.FillPie(brush, rect, 210, 150);
brush.Dispose();
g.Dispose();
}
}
}
PictureBox图像控件
PictureBox控件,图片框是操作图形图像的基本控件,主要用于显示,保存图形图像信息,其主要属性方法:
- 属性:
- Image:设置或获取与该控件显示的图像
- SizeMode:指示如何显示图像
- 方法:
- Load:显示图像
Bitmap类
Bitmap类,封装GDI+位图,此位图由图形图像及其属性的像素数据组成。Bitmap是用于处理由像素数据定义的图像的对象,其常用属性及方法:
名称 | 说明 |
---|---|
Size | 获取图像的以像素为单位的宽度和高度 |
Width | 获取图像的宽度 |
Height | 获取图像的高度 |
FromFile() | 从指定的文件创建图像 |
FromStream() | 从指定的数据流创建图像 |
GetPixel | 获取此Bitmap中指定像素的颜色 |
MakeTransparent | 使默认的透明颜色对此Bitmap透明 |
Save | 将此图像以指定的格式保存到指定的流中 |
RotateFlip | 旋转、镜像或同时旋转和镜像图像 |
/// <summary>
/// 创建位图并保存为图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button13_Click(object sender, EventArgs e)
{
Bitmap bm = new Bitmap(this.ClientSize.Width, this.ClientSize.Height,
System.Drawing.Imaging.PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bm);
Font f = new Font("隶书", 24, FontStyle.Italic);
Pen pen = new Pen(Color.OrangeRed);
g.DrawString("创建位图测试保存图像", f, pen.Brush, 0, 0);
pictureBox1.Image = bm;
pictureBox1.Show();
bm.Save("c:\\temp\\1.bmp");
pen.Dispose();
g.Dispose();
}
/// <summary>
/// 加载图片并另存图片
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button14_Click(object sender, EventArgs e)
{
OpenFileDialog openfile = new OpenFileDialog();
openfile.Filter = "图像文件*.bmp|*.bmp|*.jpg|*.png";
if (openfile.ShowDialog() == DialogResult.OK)
{
pictureBox1.Image = Image.FromFile(openfile.FileName);
pictureBox1.Image.Save("c:\\temp\\2.bmp");
}
}