C#在picturebox上面画坐标轴和曲线

wangqinli93 2016-05-13 07:57:32
下面是我的代码,不知道为什么不显示曲线


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.Runtime.InteropServices;
using System.Drawing.Drawing2D;

namespace delll
{
public partial class Form1 : Form
{
int i=0;
int y0 = 0; //上一个Y
static int jiange = 10; //水平间隔10像素
static int shuiping = 50; //水平方向总点数50
static int chuizi = 200; //垂直200像素
struct line
{
int x1, y1, x2, y2;
public void Move(int x1, int y1, int x2, int y2)
{
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public void paint(Graphics g)
{
g.DrawLine(Pens.Black, x1, y1, x2, y2);
g.SmoothingMode = SmoothingMode.AntiAlias;
}
}
struct point
{
public int x, y;
public void Move(int x, int y)
{

this.x = x;
this.y = y;
}
public void paint(Graphics g)
{
g.DrawEllipse(Pens.Red, x-1, y-1 , 3, 3);
g.SmoothingMode = SmoothingMode.AntiAlias;
}
}
line[] l = new line[shuiping];
point p = new point();


public Form1()
{
InitializeComponent();
}


private void Form1_Load(object sender, EventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Bitmap BRTmap1 = new Bitmap(pictureBox1.Width, pictureBox1.Height);
Graphics BRTg1 = Graphics.FromImage(BRTmap1);
Pen pen = new Pen(Color.Black, 1);//画笔
SolidBrush sb = new SolidBrush(Color.Green);//画刷
BRTg1.Clear(Color.White);
BRTg1.DrawLine(pen, 35, pictureBox1.Height - 20, pictureBox1.Width - 35, pictureBox1.Height - 20);//绘制X坐标轴
//g.DrawString("X轴", new Font("宋体", 9), sb, pictureBox1.Width - 25, pictureBox1.Height - 35);//标注X轴
//绘制Y坐标轴
BRTg1.DrawLine(pen, 35, pictureBox1.Height - 20, 35, 5);//坐标是相对于pictureBox而言
//绘制平行Y轴的竖线,相当于X轴上的刻度
float BRTxlength1 = pictureBox1.Width - 70;
float BRTx1 = BRTxlength1 / 3;
float BRTxt1 = 35, BRTyt1 = pictureBox1.Height - 20, BRTxb1 = 35, BRTyb1 = pictureBox1.Height - 15;//第一条线的起止位置
string[] BRTstrx1 = { "0", "1", "2", "3" };

for (int i = 0; i < 4; i++)
{
BRTg1.DrawLine(pen, BRTxt1 + i * BRTx1, BRTyt1, BRTxb1 + i * BRTx1, BRTyb1);
BRTg1.DrawString(BRTstrx1[i], new Font("宋体", 9), sb, BRTxt1 + i * BRTx1 - 22, pictureBox1.Height - 15);
}
// BRTg1.DrawString("时间", new Font("宋体", 9), sb, 30 + BRTx1 / 2 - 5, pictureBox2.Height - 15);
//绘制平行Y轴的竖线,相当于X轴上的刻度
float BRTylength1 = pictureBox1.Height - 25;
float BRTy1 = BRTylength1 / 3;
float BRTxt11 = 35, BRTyt11 = pictureBox1.Height - 20, BRTxb11 = 30, BRTyb11 = pictureBox1.Height - 20;//第一条线的起止位置
string[] BRTstry1 = { "0", "2", "4", "6" };
for (int i = 0; i < 4; i++)
{
BRTg1.DrawLine(pen, BRTxt11, BRTyt11 - i * BRTy1, BRTxb11, BRTyb11 - i * BRTy1);
BRTg1.DrawString(BRTstry1[i], new Font("宋体", 9), sb, 0, BRTyt11 - 5 - i * BRTy1);
}
BRTg1.DrawString("通道1", new Font("宋体", 9), new SolidBrush(Color.Blue), pictureBox1.Width - 40, 25);
// BRTg1.DrawString("GHz", new Font("宋体", 9), new SolidBrush(Color.Blue), pictureBox2.Width - 30, 35);
pictureBox1.Image = BRTmap1;



}

Random r = new Random();

private void timer1_Tick(object sender, EventArgs e)
{
draw(r.Next(chuizi));
}

void draw(int y)
{
int ii = i % shuiping;

l[ii].Move(ii * jiange,y0,(ii+1)*jiange,y);
p.Move((ii + 1) * jiange, y);
textBox1 .Text +=p.x.ToString ()+" , "+p.y.ToString ()+Environment .NewLine ;
i++;
pictureBox1.Invalidate();
y0 = y;
}

private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
for (int i = 0; i < shuiping; i++)
{
l[i].paint(e.Graphics );
}
p.paint(e.Graphics);
}


}
}
...全文
1628 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
xuzuning 2016-05-14
  • 打赏
  • 举报
回复
timer1.Enabled = true; 你没有启动定时器,曲线(应该是折线)怎么开始画? private void timer1_Tick(object sender, EventArgs e) { draw(r.Next(chuizi)); }
threenewbee 2016-05-13
  • 打赏
  • 举报
回复
画图的代码要写在paint里面,否则刷新一下就被擦掉了。
  • 打赏
  • 举报
回复
仔细看了下LZ的代码,没看到有绘制曲线的相关代码
wangqinli93 2016-05-13
  • 打赏
  • 举报
回复
之前用的是08的,没有chart控件,后来坐标写得差不多了,就差画曲线了,最近才知道有chart,就想先这样试试,
  • 打赏
  • 举报
回复
为什么不直接使用Chart控件呢?

110,532

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧