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;
        }
       
    }
}
复制代码

 四:注意事项

如何建立一个有比例关系的坐标系,主要是确定实际距离和像素的关系,一个像素代表多少米的距离

如何生成一个动态比例尺、动态刻度值的坐标系,这就需要确立一个参照点,将这个参照点作为零点,再计算刻度值,从零点的左右两边分别标刻度值。标点时,需要确立动态的像素和距离的关系来确定位置

 

 

生活中的不便,用软件解决,C#学而不用,白学了-----红马車
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值