【发布时间】:2012-07-17 08:30:54
【问题描述】:
在UserControl 中,我有一个PictureBox 和其他一些控件。对于包含这个名为Graph 的图片框的用户控件,我有一个在这个图片框上绘制曲线的方法:
//Method to draw X and Y axis on the graph
private bool DrawAxis(PaintEventArgs e)
{
var g = e.Graphics;
g.DrawLine(_penAxisMain, (float)(Graph.Bounds.Width / 2), 0, (float)(Graph.Bounds.Width / 2), (float)Bounds.Height);
g.DrawLine(_penAxisMain, 0, (float)(Graph.Bounds.Height / 2), Graph.Bounds.Width, (float)(Graph.Bounds.Height / 2));
return true;
}
//Painting the Graph
private void Graph_Paint(object sender, PaintEventArgs e)
{
base.OnPaint(e);
DrawAxis(e);
}
//Public method to draw curve on picturebox
public void DrawData(PointF[] points)
{
var bmp = Graph.Image;
var g = Graphics.FromImage(bmp);
g.DrawCurve(_penAxisMain, points);
Graph.Image = bmp;
g.Dispose();
}
当应用程序启动时,轴被绘制。但是当我调用DrawData 方法时,我得到一个异常,说bmp 为空。可能是什么问题?
我还希望能够在用户单击某些按钮时多次调用DrawData 以显示多条曲线。实现这一目标的最佳方法是什么?
谢谢
【问题讨论】:
标签: c# winforms drawing picturebox