如何在C#中用GDI+动态的绘制曲线

zw945099269 2011-09-10 05:32:51
如题,在GDI+中利用多点绘制曲线折线一般用DrawCurve(Pen, Point[])和DrawLines(Pen, Point[])之类的函数,可是这两个函数的第二个参数都要求是纯Point[]数组类型,在绘制动态曲线的时候,显然是要等到程序运行之后才能确定数据,并且数组里的个数也一直在变(第一秒钟有两个点,第二秒钟有三个点,以此类推的增加)。我的想法自然是先定义动态数组 List<Point> myPoints = new List<Point>();需要点的时候直接Add()就行了,但是你把泛型的myPoints传到DrawCurve(Pen, Point[])里面程序是会报错的,myPoints.ToArray();之后也一样不行。
另外,就算使用下下策最开始的时候定义几个足够大的数组(就暂时不考虑内存浪费了)也不行,比方开始Point[] myPoints=new Point[100](想后面把泛型里面数传会数组)当数组里面的数据点没有100个的时候,里面会有很多的(0,0),那样绘制的曲线后面就有一大堆没用的(0,0)直线。怎么办?怎么办?
问题应该已经描述清楚了,请教各位大侠,在教师节里,各位也当当好老师吧!
...全文
423 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
zw945099269 2011-09-12
  • 打赏
  • 举报
回复
中秋节,小顶一下
gclol 2011-09-11
  • 打赏
  • 举报
回复
没想到myPoints.ToArray()会是错的,那就只能
Point[] points=new Point[myPoints.Count],再挨个赋值呗
xiongxyt2 2011-09-11
  • 打赏
  • 举报
回复
引用ZedGraph.dll
using ZedGraph;

GraphPane myPan = new GraphPane();
PointPairList list = new PointPairList();
Random ran = new Random();
LineItem myCurve;


DataSet ds = new DataSet();
SqlConnection myConnection = null;
SqlCommand myCommand = null;
SqlDataAdapter adapt = null;
SqlDataReader reader = null;
public Form1()
{
InitializeComponent();
}


public string GetConnectString(string server, string database, string username, string password)
{
return "Server=" + server + ";Database=" + database + ";UID=" + username + ";PWD=" + password;
}

public void Exc()
{
double x, y;
myConnection = new SqlConnection();
myConnection.ConnectionString = GetConnectString("192.168.0.101", "AMR", "sa", "123");
myCommand = new SqlCommand("select MSID,UpdateTime from Tab_BS_Dailyfile", myConnection);
myCommand.CommandType = CommandType.Text;
myConnection.Open();

adapt = new SqlDataAdapter();

try
{
//myCommand.ExecuteNonQuery();

reader = myCommand.ExecuteReader();
while (reader.Read())
{
for (int i = 0; i < reader.FieldCount; )
{
y = double.Parse(reader[i++].ToString());
x = (double)new XDate(DateTime.Parse(reader[i++].ToString()));
list.Add(x, y);
}
}

}
catch (SqlException ex)
{
MessageBox.Show("插入错误!" + ex.Message.ToString());
}
myCommand.Clone();
myConnection.Close();
}

private void Form1_Load(object sender, EventArgs e)
{
this.zedGraphControl1.GraphPane.Title.Text = "动态折线图";

this.zedGraphControl1.GraphPane.XAxis.Title.Text = "时间";
this.zedGraphControl1.GraphPane.X2Axis.Scale.FontSpec.Size = 28;



this.zedGraphControl1.GraphPane.YAxis.Title.Text = "数量";
this.zedGraphControl1.GraphPane.YAxis.Scale.FontSpec.Size = 8;


myPan.XAxis.Scale.IsVisible = true;


this.zedGraphControl1.GraphPane.XAxis.Type = ZedGraph.AxisType.DateAsOrdinal;


Exc();

DateTime dt = DateTime.Now;



myCurve = zedGraphControl1.GraphPane.AddCurve("My Curve",

list, Color.DarkGreen, SymbolType.None);



this.zedGraphControl1.AxisChange();

this.zedGraphControl1.Refresh();

}


/// <summary>
/// 动起来
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void timer1_Tick(object sender, EventArgs e)
{
zedGraphControl1.GraphPane.XAxis.Scale.MaxAuto = true;

double x = (double)new XDate(DateTime.Now);

double y = ran.NextDouble();

list.Add(x, y);//改成你数据库的数据就可以了
list.RemoveAt(0);

this.zedGraphControl1.AxisChange();

this.zedGraphControl1.Refresh();

}


zw945099269 2011-09-11
  • 打赏
  • 举报
回复
楼主又回来了,人生第一大乐事是编程,第二大乐事是分享~~
高手们不愿意露面,我自个儿依葫芦画瓢学着上面的写了个,一并贴出来和大家分享。

private void DrawCurves(Graphics grp, List<Point> pointList)
{
Point[] temps = new Point[pointList.Count];
for (int i = 0; i < pointList.Count; i++)
{
temps[i] = pointList[i];
}
grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grp.DrawCurve(new Pen(Color.Red, 2), temps);
grp.Dispose();
}


如此,问题大致算解决了,大家还有什么新奇的好的想法,欢迎提出来交流交流……
zw945099269 2011-09-11
  • 打赏
  • 举报
回复
楼主回来了,下面的代码是博客园里网名为“天行健 自强不息”的大侠给的,可以实现传入泛型画直线,但是圆滑的曲线要怎么办?继续向高手求救。想想微软也是的,我觉得他完全有必要把这两个都作为函数的重载版本给出来的……

private void DrawLines(Graphics grp, List<Point> pointList)
{
Point one, two;
for (int i = 0; i < pointList.Count-1; i++)
{
one = pointList[i];
two = pointList[i + 1];
grp.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
grp.DrawLine(new Pen(Color.Red, 2), one,two);
}
grp.Dispose();
}
EdsionWang 2011-09-11
  • 打赏
  • 举报
回复
作为学生的飘过~
pois 2011-09-10
  • 打赏
  • 举报
回复
我也想知道
zw945099269 2011-09-10
  • 打赏
  • 举报
回复
高手啊高手们啊,别都回去看老师了就忘了csdn里的学生啊!!!!!!!!
zw945099269 2011-09-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 bdmh 的回复:]

那就别用这两个函数了,直接用DrawLine,用一个线程去画图,时时根据列表中的点计算坐标
[/Quote]

这样也行?按照net的风格不至于非要把人往这路上逼吧!另外,画折线的话这样是可以,可我要画的是圆滑曲线啊?还有什么高招没,求教求教~
bdmh 2011-09-10
  • 打赏
  • 举报
回复
那就别用这两个函数了,直接用DrawLine,用一个线程去画图,时时根据列表中的点计算坐标

110,569

社区成员

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

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

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