PictureBox绘制实时曲线图,连接后两个点时,前两个点的连线就没了。
Public Class Form1Dim PreVoltage As New PointF(0, 0)
Dim i As Single = 0
Private Function PlotData(ByVal Voltage As PointF) As PointF
Dim PicWidth As Integer = PictureBox1.ClientSize.Width
Dim PicHeight As Integer = PictureBox1.ClientSize.Height
Dim PicBmp As New Bitmap(PicWidth, PicHeight)
Dim PicGraphics As Graphics = Graphics.FromImage(PicBmp)
Dim BluePen As New Pen(Color.Blue)
'建立坐标
Dim ScaleFactorX As Integer = 300
Dim ScaleFactorY As Integer = 150
Dim PicScaleX As Single
Dim PicScaleY As Single
PicScaleX = PictureBox1.Width / ScaleFactorX
PicScaleY = (PictureBox1.Height * 0.97) / ScaleFactorY
'测量点的坐标化
Voltage.X = Voltage.X * PicScaleX
Voltage.Y = Voltage.Y * PicScaleY
'绘图
PicGraphics.DrawLine(BluePen, PreVoltage, Voltage)
'输出至图框
PictureBox1.Image = PicBmp
PictureBox1.Refresh()
'返回当前点坐标,用做下次绘图
Return Voltage
End Function
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
'取假定测量点
Dim Voltage As New PointF(i, i)
PreVoltage = PlotData(Voltage)
i = i + 1
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Timer1.Enabled = True
End Sub
End Class
希望是根据测量数据绘制一条实时曲线图,通过连接每个测量的点来实现。
当前代码总是只能显示两个点的连线,无法把之前的连线保存。怎么才能解决呢?