资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

vb.net绘制平滑曲线 vb怎么根据数据绘制曲线

vb.net绘制曲线图

。net  其实还是很好绘制图形的

为乐至等地区用户提供了全套网页设计制作服务,及乐至网站建设行业解决方案。主营业务为成都网站设计、网站建设、乐至网站设计,以传统方式定制建设网站,并提供域名空间备案等一条龙服务,秉承以专业、用心的态度为用户提供真诚的服务。我们深信只要达到每一位用户的要求,就会得到认可,从而选择与我们长期合作。这样,我们也可以走得更远!

你可以看下 Graphics  类

Dim d As New Bitmap(Me.Width, Me.Height)  ‘一个图片吧

  Dim g As Graphics = Graphics.FromImage(d)’绘制  准备在这个图片是进行

然后  就是你绘制的东西了

线 就是   g.DrawLine()

圆 弧度  就用  g.DrawArc(Pens.Black, New Rectangle(0, 0, 400, 200), 0, 360)

复杂的就是      g.DrawBezier()

等  如果你用的是 VS的  编译  上面都有详细的参数说明

Dim d As New Bitmap(Me.Width, Me.Height)

Dim g As Graphics = Graphics.FromImage(d)

g.DrawArc(Pens.Black, New Rectangle(0, 0, 200, 200), 0, 360)

g.DrawLine(Pens.Red, New Point(0, 0), New Point(200, 200))

g.DrawLines(Pens.Green, New Point() {New Point(0, 0), New Point(50, 40), New Point(50, 80), New Point(90, 70), New Point(100, 400)})

g.DrawBezier(Pens.Yellow, New Point(0, 100), New Point(0, 0), New Point(200, 0), New Point(200, 200))

g.Dispose()

Me.BackgroundImage = d

如何在VB中画曲线

在VB中绘制实时曲线是比较难的,一般要应用第三方控件或是Windows

API函数来完成,但是如果你对实时曲线的要求不是很高,只要能表示出当前的一般情况的话,我们可以直接应用VB提供给我们的空间来完成.

原则上讲,直接在Form里绘制曲线都是可以的,MSDN上面很多例程就是直接在Form里面绘制图形的,Form作为绘制图形的容器,不过一般应用中Form中不可避免的会有很多其他控件,所以我们选择PictureBox作为绘制曲线的容器.

实时曲线的绘制一般借助于Timer控件来完成,使用Timer控件,定期将串口或是其他仪器中监测到的数据送往PictureBox1,而曲线的绘制一般画成折线图,采用PictureBox1的Line方法绘制.具体实现如下:

1.选择需要显示的窗体Picture1,加入图片框Picture1,根据实际需要设置图片的大小并移到合适的位置,并在图片的外面画好量程----时间坐标系;然后加上Timer控件以及两个CommandButton,界面就基本设置好了.

2.建立坐标系,根据Picture1的大小和高度设置画出坐标系的X轴和Y轴:

Picture1

.ScaleMode

=

1

‘以VB的基本单位作为建立坐标轴以及绘制图形的单位;

Picture1.Refresh

Picture1.CurrentX

=

Picture1.ScaleLeft

+100

Picture1.CurrentY

=

Picture1.ScaleTop

Picture1.Print

Picture1.ScaleHeight

-

100

Picture1.Line(Picture1.ScaleLeft+100,Picture1.ScaleTop+100)-(Picture1.ScaleLeft+100,

Picture1.ScaleHeight

-

100)

Picture1.CurrentX

=

Picture1.ScaleLeft

+100

Picture1.CurrentY

=

Picture1.ScaleHeight

Picture1.Print

“(0,0)”

Picture1.Line

(Picture1.ScaleLeft

+

100,

Picture1.ScaleHeight

-

100)-(Picture1.ScaleWidth

-

100,

Picture1.ScaleHeight

-

100)

Picture1.CurrentX

=

Picture1.ScaleWidth

Picture1.CurrentY

=

Picture1.ScaleHeight

Picture1.Print

Picture1.ScaleWidth-100

Picture1.AutoRedraw

=

True

‘必要时,用存储在内存中的图象进行重绘

3.绘制曲线并保存,我们这里以正弦曲线作为绘制曲线的数据来源,具体应用是可以采用由串口或其他仪器采集得到的数据.首先我们绘制一条中线,然后在Timer控件的Time事件中绘制曲线:

Picture1.Line

(Picture1.ScaleLeft,

CInt(Picture1.ScaleHeight

/

2))-(Picture1.ScaleWidth,

CInt(Picture1.ScaleHeight

/

2))

‘绘制中线

Private

Sub

Timer1_Timer()

Dim

y1

As

Integer

y1

=

CInt(Sin((x

-

Picture1.Left)

/

20

/

180

*

pi)

*

Picture1.ScaleHeight

/

2)

y1

=

CInt((Picture1.ScaleHeight

+

1000)

/

2)

-

y1

Picture1.Line

(x,

y)-(x

+

20,

y1)

x

=

x

+

20

y

=

y1

If

x

=

Picture1.ScaleWidth

Then

SavePicture

Picture1.Image,

"c:\sin.bmp"

‘保存图画,

VB如何实现平滑曲线

VERSION 5.00 Begin VB.Form FrmMain AutoRedraw = -1 'True Caption = "光滑直线" ClientHeight = 6780 ClientLeft = 60 ClientTop = 450 ClientWidth = 9795 LinkTopic = "Form1" ScaleHeight = 452 ScaleMode = 3 'Pixel ScaleWidth = 653 StartUpPosition = 3 '窗口缺省 Begin VB.CommandButton CmdDraw Caption = "绘制" Height = 375 Left = 120 TabIndex = 0 Top = 120 Width = 1215 End Begin VB.Line Line1 Visible = 0 'False X1 = 48 X2 = 272 Y1 = 128 Y2 = 88 End End Attribute VB_Name = "FrmMain" Attribute VB_GlobalNameSpace = False Attribute VB_Creatable = False Attribute VB_PredeclaredId = True Attribute VB_Exposed = False Private Declare Function SetPixel Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long Function Min(s1 As Long, s2 As Long) As Long If s1 s2 Then Min = s1 Else Min = s2 End Function Function Max(s1 As Long, s2 As Long) As Long If s1 s2 Then Max = s1 Else Max = s2 End Function Private Sub CmdDraw_Click() Dim Scal As Single, Gray As Long, MinX As Long, MaxX As Long, MinY As Long, MaxY As Long Dim X1 As Long, X2 As Long, Y1 As Long, Y2 As Long, X3 As Single, Y3 As Single Line1.Visible = False MinX = Min(Line1.X1, Line1.X2): MaxX = Max(Line1.X1, Line1.X2) MinY = Min(Line1.Y1, Line1.Y2): MaxY = Max(Line1.Y1, Line1.Y2) If (Line1.Y1 Line1.Y2 And Line1.X1 Line1.X2) Or (Line1.Y1 Line1.Y2 And Line1.X1 Line1.X2) Then If Abs(Line1.X1 - Line1.X2) Abs(Line1.Y1 - Line1.Y2) Then Scal = Abs(Line1.Y2 - Line1.Y1) / Abs(Line1.X2 - Line1.X1) For i = 0 To MaxX - MinX Y3 = i * Scal + MinY: Y1 = Int(Y3): Y2 = Y1 + 1 Gray = (Y2 - Y3) * 255 SetPixel Me.hdc, i + MinX, Y2, RGB(Gray, Gray, Gray) Gray = (Y3 - Y1) * 255 SetPixel Me.hdc, i + MinX, Y1, RGB(Gray, Gray, Gray) Next i Else Scal = Abs(Line1.X2 - Line1.X1) / Abs(Line1.Y2 - Line1.Y1) For i = 0 To MaxY - MinY X3 = i * Scal + MinX: X1 = Int(X3): X2 = X1 + 1 Gray = (X2 - X3) * 255 SetPixel Me.hdc, X2, i + MinY, RGB(Gray, Gray, Gray) Gray = (X3 - X1) * 255 SetPixel Me.hdc, X1, i + MinY, RGB(Gray, Gray, Gray) Next i End If Else If Abs(Line1.X1 - Line1.X2) Abs(Line1.Y1 - Line1.Y2) Then Scal = -Abs(Line1.Y2 - Line1.Y1) / Abs(Line1.X2 - Line1.X1) For i = 0 To MaxX - MinX Y3 = i * Scal + MaxY: Y1 = Int(Y3): Y2 = Y1 + 1 Gray = (Y2 - Y3) * 255 SetPixel Me.hdc, i + MinX, Y2, RGB(Gray, Gray, Gray) Gray = (Y3 - Y1) * 255 SetPixel Me.hdc, i + MinX, Y1, RGB(Gray, Gray, Gray) Next i Else Scal = -Abs(Line1.X2 - Line1.X1) / Abs(Line1.Y2 - Line1.Y1) For i = 0 To MaxY - MinY X3 = i * Scal + MaxX: X1 = Int(X3): X2 = X1 + 1 Gray = (X2 - X3) * 255 SetPixel Me.hdc, X2, i + MinY, RGB(Gray, Gray, Gray) Gray = (X3 - X1) * 255 SetPixel Me.hdc, X1, i + MinY, RGB(Gray, Gray, Gray) Next i End If End If Me.Refresh End Sub Private Sub Form_Load() Me.ScaleMode = vbPixels Me.AutoRedraw = True End Sub Private Sub Form_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single) Line1.Visible = True Line1.X1 = x: Line1.Y1 = y End Sub Private Sub Form_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single) If Button = vbLeftButton Then Line1.X2 = x: Line1.Y2 = y End Sub Private Sub Form_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single) Line1.X2 = x: Line1.Y2 = y End Sub


当前标题:vb.net绘制平滑曲线 vb怎么根据数据绘制曲线
链接分享:http://www.cdkjz.cn/article/dodsedi.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220