首页 > 代码库 > GDI+画图类Graphics的使用
GDI+画图类Graphics的使用
一:基础定义
#region 定义线尾、线头为箭头、字体和笔刷 Pen p = new Pen(Color.Black, 1);//定义画笔 蓝色,宽度为1(坐标显示颜色) p.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头 Pen pk = new Pen(Color.Black, 1);//定义画笔 黑色,宽度为1(坐标显示颜色) Pen pbl = new Pen(Color.Red,1); pbl.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头 StringFormat strF = new StringFormat(StringFormatFlags.DirectionVertical);//定义格式 Font f = new System.Drawing.Font("宋体", 10);//定义字体 SolidBrush bB = new SolidBrush(Color.Black);//定义单色画刷 SolidBrush bl = new SolidBrush(Color.Red);//定义单色画刷 #endregion
二:使用前准备,新建一个picturebox,实例化一个 Graphics类,关联它们
Graphics gph;//绘画区域
Bitmap bmp = new Bitmap(pic_Img.Width, pic_Img.Height);//600, 512 gph = Graphics.FromImage(bmp);
三:实例
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 DrawLine{ public partial class Form1 : Form { public Form1() { InitializeComponent(); #region 定义线尾、线头为箭头、字体和笔刷 Pen p = new Pen(Color.Blue, 1);//定义画笔 蓝色,宽度为1(坐标显示颜色) p.EndCap = LineCap.ArrowAnchor;//定义线尾的样式为箭头 p.StartCap = LineCap.ArrowAnchor;//定义线首的样式为箭头 Font f = new System.Drawing.Font("宋体", 10);//定义字体 SolidBrush bB = new SolidBrush(Color.Black);//定义单色画刷 //SolidBrush bB = new SolidBrush(Color.Blue); #endregion string[] month = new string[12] { "一月", "二月", "三月", "四月", "五月", "六月","七月", "八月", "九月", "十月", "十一月", "十二月" }; float[] d = new float[12] { 20.5F, 60, 10.8F, 15.6F, 30, 70.9F, 50.3F, 30.7F, 70, 50.4F, 30.8F, 20 }; //画图初始化 Bitmap bMap = new Bitmap(526, 500); Graphics gph = Graphics.FromImage(bMap); //gph.Clear(Color.White); PointF cPt = new PointF(40, 420);//中心点 PointF[] xPt = new PointF[3]{new PointF(cPt.Y+15,cPt.Y),new PointF(cPt.Y,cPt.Y-8),new PointF(cPt.Y,cPt.Y+8)};//X轴三角形 PointF[] yPt = new PointF[3]{new PointF(cPt.X,cPt.X-15),new PointF(cPt.X-8,cPt.X),new PointF(cPt.X+8,cPt.X)};//Y轴三角形 gph.DrawString("红马車公司月生产量图表", new Font("宋体", 14) , Brushes.Black, new PointF(cPt.X + 60, cPt.X));//图标标题 //画X轴 gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.Y, cPt.Y); gph.DrawPolygon(Pens.Black, xPt); gph.FillPolygon(new SolidBrush(Color.Black), xPt); gph.DrawString("月份", new Font("宋体", 12), Brushes.Black, new PointF(cPt.Y + 10, cPt.Y + 10)); //画Y轴 gph.DrawLine(Pens.Black, cPt.X, cPt.Y, cPt.X, cPt.X); gph.DrawPolygon(Pens.Black, yPt); gph.FillPolygon(new SolidBrush(Color.Black), yPt); gph.DrawString("单位(万)", new Font("宋体", 12), Brushes.Black, new PointF(0, 7)); for (int i = 1; i <= 12; i++) { //画Y轴刻度 if (i < 11) { gph.DrawString((i * 10).ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X - 30, cPt.Y - i * 30 - 6)); gph.DrawLine(Pens.Black, cPt.X - 3, cPt.Y - i * 30, cPt.X, cPt.Y - i * 30); } //画X轴项目 gph.DrawString(month[i - 1].Substring(0, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 5)); gph.DrawString(month[i - 1].Substring(1, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 20)); if (month[i - 1].Length > 2) gph.DrawString(month[i - 1].Substring(2, 1), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30 - 5, cPt.Y + 35)); gph.DrawLine(Pens.Black, cPt.X + i * 30, cPt.Y, cPt.X + i * 30, cPt.Y + 3); //画点 gph.DrawEllipse(Pens.Black, cPt.X + i * 30, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3); gph.FillEllipse(new SolidBrush(Color.Black), cPt.X + i * 30, cPt.Y - d[i - 1] * 3 - 1.5F, 3, 3); //画数值 gph.DrawString(d[i - 1].ToString(), new Font("宋体", 11), Brushes.Black, new PointF(cPt.X + i * 30, cPt.Y - d[i - 1] * 3)); //画折线 if (i > 1) gph.DrawLine(Pens.Red, cPt.X + (i - 1) * 30, cPt.Y - d[i - 2] * 3, cPt.X + i * 30, cPt.Y - d[i - 1] * 3); } pictureBox1.Image = bMap; } }}
四:注意事项
如何建立一个有比例关系的坐标系,主要是确定实际距离和像素的关系,一个像素代表多少米的距离
如何生成一个动态比例尺、动态刻度值的坐标系,这就需要确立一个参照点,将这个参照点作为零点,再计算刻度值,从零点的左右两边分别标刻度值。标点时,需要确立动态的像素和距离的关系来确定位置
GDI+画图类Graphics的使用
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。