在UserControl
我有一个PictureBox
和一些其他控件。对于包含该PictureBox的命名为Graph
我只好动用此图片框的曲线的方法的用户控制:在PictureBox上绘图
//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
以在用户单击某些按钮时显示多条曲线。达到这个目标的最好方法是什么?
谢谢
不,我没有分配,我想在图上调用Paint方法会使图像。你能解释一下,请问如何解决这个问题? – 2012-07-18 10:21:01
@ Sean87查看更新。 – 2012-07-18 10:26:10
@KonradRudolph非常感谢。 – 2013-10-30 08:02:33