C# GDI+ 绘图
来源:互联网 发布:怎么清空淘宝搜索记录 编辑:程序博客网 时间:2024/04/29 21:57
1 坐标系统
(1) 坐标原点:在窗体或控件的左上角,坐标为(0,0)
(2) 正方向:X轴正方向为水平向右,Y轴正方向为竖直向下
(3) 单位:在设置时,一般以像素为单位,像素(Pixel)是由图像(Picture)和元素(Element)组成,是用来计算数码影像的一种单位。
把影像放大数倍,会发现这些连续的色调其实是有许多色彩相近的小方点组成,这些小方点是构成影像的最小单位—像素。
图形的质量是有像素决定,像素越大,分辨率也越大。
2 命名空间 --- System.Drawing
(1) System.Drawing 提供了对GDI+基本图形功能的访问
(2) System.Drawing 常用基本类及结构
类
说明
Bitmap
用于处理有像素数据定义的图像的对象。
Brush
定义用于填充图形形状的内部对象。
Font
定义特定的文本格式。
Graphics
封装一个GDI+绘图图画,无法继承此类。
Pen
用于绘制直线和曲线的对象,无法继承此类。
Region
指示由矩形和路径构成的图形形状的内部,无法继承此类。
Color
表示RGB颜色。
Point
定义二维平面中定义的点。
Rectangle
存储一组整数,共4个,表示一个矩形的位置和大小。
Size
存储一个有序整数对,通常为矩形的宽和高。
3 Graphics类
Graphics类封装了一个GDI+绘制界面,提供将对象绘制到显示界面的方法。使用GDI+创建图形图像时,需要先创建Graphics对象,即在哪里画图。
共有3种类型的绘图界面:
(1) 窗体和控件
(2) 打印机
(3) 内存的位图
创建图形对象的3中方法:
(1)控件类的OnPaint()方法参数PaintEventArgs获取Graphics对象
(2)窗体类或控件类中的CreateGraphics()方法获得Graphics对象
(3)从位图对象(Bitmap)产生一个Graphics对象
Graphics类的常用方法
名称
说明
Dispose
释放Graphics使用的所有资源。
DrawEllipse
绘制椭圆,有高度,宽度,一对坐标。
DrawArc
绘制弧形。
DrawLine
绘制一条直线,由2个点指定。
DrawPolygon
绘制由一组Point结构定义的多边形。
DrawRectangle
绘制矩形。
DrawPie
绘制一个扇形。
DrawCurse
绘制曲线,由参数Point数组指定。
FillEllipse
填充边框所定义的椭圆的内部。
FillRegion
填充Region的内部。
ScaleTransform
将制定的缩放操作应用于次Graphics。
TanslateTransform
平移更改坐标系统的原点。
4 绘图工具类
类名
说明
Pen
设置画笔的颜色,线条粗细和线条样式(实线和虚线)。
Brush
用于填充图形,设置笔刷的样式,颜色及线条的粗细。
5 Brush类的派生类
名称
说明
ImageBrush
图像绘制区域。
LinearGradientBrush
线性渐变绘制区域。
RadialGradientBrush
径向渐变绘制区域,焦点定义渐变的开始,椭圆定义渐变的终点。
SolidColorBrush
单色绘制区域。
VideoBrush
视频内容绘制区域。
6 案例 免费下载地址 http://download.csdn.net/detail/taoerit/8350869
7 代码
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;namespace GDI绘图{ public partial class MainDialog : Form { public MainDialog() { InitializeComponent(); } private void MainDialog_Load(object sender, EventArgs e) { } private void lineButton_Click(object sender, EventArgs e) { // 画直线 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); pen.Width = 2; Point startPoint = new Point(20,20); Point endPoint = new Point(70,20); gra.DrawLine(pen,startPoint,endPoint); pen.Dispose(); gra.Dispose(); } private void rectangleButton_Click(object sender, EventArgs e) { //画矩形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.DrawRectangle(pen, 20,50, 100,100); pen.Dispose(); gra.Dispose(); } private void cyliderButton_Click(object sender, EventArgs e) { //圆柱体,有许多个椭圆有底部逐渐叠起来的,最后填充颜色 int height = this.ClientSize.Height - 150; int width = this.ClientSize.Width - 50; int vHeight = 200; int vWidth = 100; Graphics gra = this.CreateGraphics(); gra.Clear(Color.White); Pen pen = new Pen(Color.Gray,2); SolidBrush brush = new SolidBrush(Color.Gainsboro); for (int i = height / 2; i > 0;i-- ) { gra.DrawEllipse(pen,width/2,i,vHeight,vWidth); } gra.FillEllipse(brush,width/2,0,vHeight,vWidth); } private void fillRectangleButton_Click(object sender, EventArgs e) { //画矩形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red,3); Brush brush = pen.Brush; Rectangle rect = new Rectangle(20,50,100,100); gra.FillRectangle(brush,rect); gra.Dispose(); } private void drawEllispeButton_Click(object sender, EventArgs e) { Graphics gra = this.CreateGraphics(); Rectangle rect = new Rectangle(0,0,200,100); LinearGradientBrush brush = new LinearGradientBrush(rect,Color.Orange,Color.Purple,90); gra.FillEllipse(brush,rect); gra.Dispose(); } private void fontButton_Click(object sender, EventArgs e) { Graphics gra = this.CreateGraphics(); Font font = new Font("隶书",24,FontStyle.Italic); Pen pen = new Pen(Color.Blue,3); gra.DrawString("Windows应用程序设计",font,pen.Brush,10,100); } private void ellispeButton_Click(object sender, EventArgs e) { // 画圆形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.DrawEllipse(pen, 0, 0, 200,100); pen.Dispose(); gra.Dispose(); } private void moveEllispeButton_Click(object sender, EventArgs e) { // 移动圆形 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.TranslateTransform(10,10);// 改变起坐标(10,10) gra.DrawEllipse(pen, 0, 0, 200, 100); gra.Dispose(); } private void scaleEllispeButton_Click(object sender, EventArgs e) { // 缩放圆形 float xScale = 1.5F; float yScale = 2F; Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Red); gra.ScaleTransform(xScale, yScale);// X轴放大1.5倍, Y轴放大2倍 gra.DrawEllipse(pen, 0, 0, 200, 100); gra.Dispose(); } private void curveButton_Click(object sender, EventArgs e) { //绘制曲线 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Blue,3); Point oo1 = new Point(30,this.ClientSize.Height -100); Point oo2 = new Point(this.ClientSize.Width - 50 ,this.ClientSize.Height - 100); gra.DrawLine(pen,oo1,oo2); Point oo3 = new Point(30, 30); gra.DrawLine(pen, oo1, oo3); Font font = new System.Drawing.Font("宋体",12,FontStyle.Bold); gra.DrawString("X",font,pen.Brush,oo2); gra.DrawString("Y", font,pen.Brush,10,10); int x1 = 0, x2 = 0; double a = 0; double y1 = 0, y2 = this.ClientSize.Height - 100; 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-100)/2; gra.DrawLine(pen,x1 +30,(float)y1 ,x2+30,(float)y2); x1 = x2; y1 = y2; } gra.Dispose(); } private void piechartButton_Click(object sender, EventArgs e) { //饼图 Graphics gra = this.CreateGraphics(); Pen pen = new Pen(Color.Blue, 3); Rectangle rect = new Rectangle(50,50,200,100); Brush brush = new SolidBrush(Color.Blue); gra.FillPie(pen.Brush,rect,0,60); gra.FillPie(brush,rect,60,150); brush = new SolidBrush(Color.Yellow); gra.FillPie(brush,rect,210,150); } }}
- c# GDI+简单绘图
- c# GDI+简单绘图
- c# GDI+简单绘图
- c# GDI+简单绘图
- c# GDI+简单绘图
- C# GDI+ 简单绘图
- c# GDI+简单绘图
- C# GDI+简单绘图
- C# GDI+ 简单绘图
- C# GDI+ 简单绘图
- C# GDI+ 简单绘图
- C# GDI+ 简单绘图
- C# GDI+简单绘图
- C# GDI+ 绘图
- c# GDI+简单绘图
- C# GDI+ 绘图
- C# GDI+绘图
- C# GDI+简单绘图二
- java并发中的协同工具类介绍-CountDownLatch-CyclicBarrier-Semphone-Exchanger
- EDA/VHDL基本概念
- Netfilter/Iptables入门
- htmlspecialchars()和htmlspecialchars_decode()
- Android 实现图片缓存异步加载框架学习笔记
- C# GDI+ 绘图
- POJ1160 Post Office 动态规划
- 雷军在联想的演讲:全场无言,除了掌声!
- extern "C"的用法解析
- BitmapDrawable cannot be cast to android.graphics.drawable.AnimationDrawable解决办法
- 来看看国外招聘WEB前端工程师的帖子都写了什么
- 街区最短路径问题
- 给程序员介绍一些C++开源库
- jdk的安装和配置