在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