GDI绘图小例子

今日学习了一个利用GDI画图的小例子,算是为以后深入学习GDI做个小铺垫。

using System;
using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace GDI_
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}


private PointF PointF(int p, int p_2)
{
throw new NotImplementedException();
}

private void Form1_MouseMove(object sender, MouseEventArgs e)
{
//获取桌面坐标
this.Text = string.Format("x={0},Y={1}",e.X,e.Y);
}

private void button1_Click(object sender, EventArgs e)
{
DrawImage drawImage = new DrawImage();
drawImage.obj = rtb_Image.CreateGraphics();
drawImage.Draw();
}

private void textBox1_TextChanged(object sender, EventArgs e)
{

}
}
public class DrawImage
{

//属性
/// <summary>
/// 该属性用于获取不同的Graphics对象(图像初始600*385)
/// </summary>
public Graphics obj
{
get;
set;
}
//绘图方法
public void Draw()
{
Font font =new Font("宋体",9);
//确定绘图路径,这里只是做了测试
GraphicsPath gpath = new GraphicsPath();
//每月盈利数据
double[] data = { 34.3, 65.7, 33.1, 87, 57.3, 56, 99, 56.3, 41.6, 48.1, 57.3, 35.8 };
//存储每月盈利数据最高点的坐标
PointF[] dotP = new System.Drawing.PointF[12];
//固定原点坐标不再修改
Point O = new Point(35, 350);
//重新定义一支笔,带有前端箭头
Pen pen = new Pen(Color.Red);
pen.EndCap = System.Drawing.Drawing2D.LineCap.ArrowAnchor;
//创建Graphics对象
Graphics g = obj;
//原点标识
g.DrawString("O",font, Brushes.Black, O.X - 5, O.Y + 5);
//绘制X轴
g.DrawLine(pen, O.X, O.Y, O.X + 550, O.Y);
//绘制Y轴
g.DrawLine(pen, O.X, O.Y, O.X, O.Y - 325);
// 绘制X轴刻度(X,Y+5), 月份
for (int i = 0; i < 12; i++)
{
g.DrawLine(Pens.Red, O.X + (i + 1) * 42, O.Y, O.X + (i + 1) * 42, O.Y - 5);
//确定月份坐标
PointF monP = new System.Drawing.PointF(O.X + (i + 1) * 42 - 10, O.Y + 5);
g.DrawString(string.Format("{0}月", i + 1), font, Brushes.Green, monP);
//每月最高盈利点坐标确定
PointF mofpP = new PointF(monP.X, O.Y - (float)data[i] * 3);
dotP[i] = mofpP;
// g.DrawEllipse(Pens.SeaGreen,new RectangleF(mofpP.X,mofpP.Y,2,2));
gpath.AddEllipse(new RectangleF(mofpP.X, mofpP.Y, 2, 2));
}
gpath.AddLines(dotP);
g.DrawPath(Pens.Purple, gpath);
// g.DrawLines(Pens.Plum, dotP);
//绘制Y轴坐标(X,Y-5),盈利 10W=30px==>1w=3px
for (int i = 0; i < 10; i++)
{
g.DrawLine(Pens.Red, O.X, O.Y - (i + 1) * 30, O.X + 5, O.Y - (i + 1) * 30);
//确定金额坐标
PointF payP = new PointF(O.X - 35, O.Y - (i + 1) * 30 - 8);
g.DrawString(string.Format("{0}万", (i + 1) * 10), font, Brushes.Green, payP);
}
pen.Dispose();//释放创建的pen对象
}
}
}

效果图:

原文地址:https://www.cnblogs.com/rohelm/p/2400229.html