Forme 3D, courbe 3D et boule 3D dans VB .Net utilisant GDI+ | |
| Forme 3D, Courbe 3D et Boule 3D en VB .Net en utilisant GDI+ | |
| La forme la plus élémentaire de programmation graphique dans Visual Basic.Net utilise un ensemble de classes appelées GDI+. | |
| GDI+ signifie Graphics Device Interface Plus et la raison du suffixe plus est qu'avant GDI+ | |
| il y avait GDI, il est donc là pour être présenté comme une amélioration par rapport à son prédécesseur. | |
| Les classes GDI+ résident dans l'espace de noms System.Drawing et certaines classes sont : | |
| - System.Drawing.Bitmap | |
| - System.Drawing.Graphics | |
| - System.Drawing.Icon | |
| - System.Drawing.Image | |
| Une fois que vous avez une toile sur laquelle dessiner, vous devez disposer d’une sorte d’objet pour commencer le dessin ; le | |
| La classe System.Drawing.Graphics est celle qui s'occupe des opérations de dessin dans | |
| Visual Basic.Net. Il existe trois méthodes différentes pour utiliser la classe Graphics, une méthode accède | |
| un objet Graphics qui existe déjà tandis que les deux autres méthodes créent en fait un objet Graphics. | |
| Afin d'accéder à un objet Graphics qui existe déjà, vous devez utiliser PaintEventArgs dans un | |
| l’événement Paint du contrôle. Il s'agit du paramètre e dans la déclaration d'événement : | |
Private Sub Form1_Paint(ByVal sender As Object, ByVal e As PaintEventArgs) Handles MyBase.Paint
Dim g As Graphics = e.Graphics
End Sub
|
| Or get that of PictureBox1 control like the following code: |
Dim g As System.Drawing.Graphics g = PictureBox1.CreateGraphics |
| Ouvrez la Communauté Microsoft Visual Studio 2019 ou avec une licence et sélectionnez un nouveau projet dans le menu Fichier. | |
| Sélectionnez Visual Basic, Windows Form Application puis cliquez sur OK, insérez le premier formulaire Form1, | |
| à partir de la barre d'outils, insérez trois boutons Button1, Button2 et Button3 | |
| ajouter un autre contrôle PictureBox1 pour le dessin | |
| Dans ce projet, vous devez déclarer les variables suivantes |
Dim g As System.Drawing.Graphics
Dim pen1 As New System.Drawing.Pen(Color.Blue, 0.5) ' the pen with a specific color
Const PI = 3.14159
Dim flag, col, r, h, hl, n, n1 As Short
Dim x, y, z, cx, cy, cz, thx, thy, thz As Single
Dim point1, point2, point3, point4, point5 As System.Drawing.Point
|
| Double-cliquez sur le Button1 et ajoutez le code suivant |
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim th As Single
Static ax(9), ay(9), az(9)
Static bx(9), by(9), bz(9)
h = 160 : thy = 0.2
col = 0 : n1 = 1
r = 100
n = 8
thx = 0.2
PictureBox1.Refresh()
ax(0) = h : ay(0) = h : az(0) = h
n1 = 1
For th = 0 To 2 * PI + 0.1 Step 2 * PI / n
x = r * Math.Cos(th) : y = -h : z = r * Math.Sin(th)
Call dzorotationX() : Call dzorotationX()
bx(n1) = x : by(n1) = y : bz(n1) = z
n1 = n1 + 1
Next th
For n1 = 1 To n
x = ax(n1) : y = ay(n1)
Call dzodrawline()
x = ax(n1 + 1) : y = ay(n1 + 1)
Call dzodrawline()
x = bx(n1 + 1) : y = by(n1 + 1)
Call dzodrawline()
x = bx(n1) : y = by(n1)
Call dzodrawline()
Next n1
End Sub
|
| Et voici le résultat après exécution | |
![]() | |
| Double click on the Button2 and add the following code | |
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox1.Refresh()
g = PictureBox1.CreateGraphics
Dim x, y, s, cx, cy, zz, xx, yy As Single
Dim h, a, b, sx, sx1, sx2, sy, sy1, sy2 As Integer
Dim x1, x2, y1, y2, xw, yw As Integer
s = pi / 16
x1 = -3
x2 = 3
xw = x2 - x1
y1 = -9 : y2 = 9 : yw = y2 - y1
sx1 = 140 : sx2 = 600
sy1 = 360 : sy2 = 100
cx = (sx2 - sx1) / xw
cy = (sy1 - sy2) / yw
h = 170 : a = 6 : b = 6
For y = y1 + 6 To y2 - 6 Step 1 / cy
For x = x1 To x2 Step 1 / cx * 2
zz = h * Math.Exp(-x * x / a * a - y * y / b * b)
xx = x * cx + sx1 + 160
yy = sy1 - y * cy
sx = Int(xx + y * cy * Math.Cos(s))
sy = yy - zz
sy = sy - 100
If x = -3 Then
point1.X = sx - 60 : point1.Y = sy - 20
Else : point2.X = sx - 60 : point2.Y = sy - 20
g.DrawLine(pen1, point1, point2)
point1 = point2
End If
Next x
Next y
End Sub
|
| Et voici le résultat après exécution | |
![]() | |
| Double click on the Button3 and add the following code | |
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
PictureBox1.Refresh()
g = PictureBox1.CreateGraphics
Dim i, r As Integer
Dim x, y, z, s, q, p, px, py As Single
Dim mx(600), my(600) As Integer
s = pi / 4
r = 100
i = 1
q = -pi / 2 + 0.1
For p = 0 To 2 * pi Step 0.2
x = r * Math.Cos(q) * Math.Sin(p)
y = r * Math.Sin(q)
z = r * Math.Cos(q) * Math.Cos(p)
px = x * 1.2
py = y - z * Math.Sin(s)
mx(i) = px + 200
my(i) = 150 - py
i = i + 1
Next p
point1.X = mx(1) : point1.Y = my(1)
For q = -pi / 2 + 0.2 To pi / 2 Step 0.2
i = 1
For p = 0 To 2 * pi Step 0.2
x = r * Math.Cos(q) * Math.Sin(p)
y = r * Math.Sin(q)
z = r * Math.Cos(q) * Math.Cos(p)
px = x * 1.2
py = y - z * Math.Sin(s)
point2.X = px + 200 : point2.Y = 150 - py
g.DrawLine(pen1, point1, point2)
point1 = point2
point3.X = mx(i) : point3.Y = my(i)
point4.X = px + 200 : point4.Y = 150 - py
g.DrawLine(pen1, point3, point4)
point3 = point4
mx(i) = px + 200
my(i) = 150 - py
i = i + 1
Next p
p = 0
x = r * Math.Cos(q) * Math.Sin(p)
y = r * Math.Sin(q)
z = r * Math.Cos(q) * Math.Cos(p)
px = x * 1.2
py = y - z * Math.Sin(s)
point5.X = px + 200 : point5.Y = 150 - py
g.DrawLine(pen1, point4, point5)
point4 = point5
Next q
End Sub
|
| Et voici le résultat après exécution | |
![]() | |
| Enfin vous aurez le code suivant dans notre classe Form1 | |
Public Class Form1
Dim g As System.Drawing.Graphics
Dim pen1 As New System.Drawing.Pen(Color.Blue, 0.5)
Const PI = 3.14159
Dim flag, col, r, h, hl, n, n1 As Short
Dim x, y, z, cx, cy, cz, thx, thy, thz As Single
Dim point1, point2, point3, point4, point5 As System.Drawing.Point
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim th As Single
Static ax(9), ay(9), az(9)
Static bx(9), by(9), bz(9)
h = 160 : thy = 0.2
col = 0 : n1 = 1
r = 100
n = 8
thx = 0.2
PictureBox1.Refresh()
ax(0) = h : ay(0) = h : az(0) = h
n1 = 1
For th = 0 To 2 * PI + 0.1 Step 2 * PI / n
x = r * Math.Cos(th) : y = -h : z = r * Math.Sin(th)
Call dzorotationY() : Call dzorotationX()
bx(n1) = x : by(n1) = y : bz(n1) = z
n1 = n1 + 1
Next th
For n1 = 1 To n
x = ax(n1) : y = ay(n1)
Call dzodrawline()
x = ax(n1 + 1) : y = ay(n1 + 1)
Call dzodrawline()
x = bx(n1 + 1) : y = by(n1 + 1)
Call dzodrawline()
x = bx(n1) : y = by(n1)
Call dzodrawline()
Next n1
End Sub
Public Sub dzodrawline()
Dim gx, gy As Single
gx = 250 + x
gy = 180 + y
If flag = 0 Then point1.X = gx : point1.Y = gy
flag = 1
point2.X = gx : point2.Y = gy
g = PictureBox1.CreateGraphics
g.DrawLine(pen1, point1, point2)
point1 = point2
End Sub
Private Sub dzorotationX()
Dim yw, zw As Single
yw = y : zw = z
y = yw * Math.Cos(thx) - zw * Math.Sin(thx)
z = yw * Math.Sin(thx) + zw * Math.Cos(thx)
End Sub
Private Sub dzorotationY()
Dim zw, xw As Single
zw = z : xw = x
x = zw * Math.Cos(thy) - xw * Math.Sin(thy)
z = zw * Math.Sin(thy) + xw * Math.Cos(thy)
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
PictureBox1.Refresh()
g = PictureBox1.CreateGraphics
Dim x, y, s, cx, cy, zz, xx, yy As Single
Dim h, a, b, sx, sx1, sx2, sy, sy1, sy2 As Integer
Dim x1, x2, y1, y2, xw, yw As Integer
s = pi / 16
x1 = -3
x2 = 3
xw = x2 - x1
y1 = -9 : y2 = 9 : yw = y2 - y1
sx1 = 140 : sx2 = 600
sy1 = 360 : sy2 = 100
cx = (sx2 - sx1) / xw
cy = (sy1 - sy2) / yw
h = 170 : a = 6 : b = 6
For y = y1 + 6 To y2 - 6 Step 1 / cy
For x = x1 To x2 Step 1 / cx * 2
zz = h * Math.Exp(-x * x / a * a - y * y / b * b)
xx = x * cx + sx1 + 160
yy = sy1 - y * cy
sx = Int(xx + y * cy * Math.Cos(s))
sy = yy - zz
sy = sy - 100
If x = -3 Then
point1.X = sx - 60 : point1.Y = sy - 20
Else : point2.X = sx - 60 : point2.Y = sy - 20
g.DrawLine(pen1, point1, point2)
point1 = point2
End If
Next x
Next y
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
PictureBox1.Refresh()
g = PictureBox1.CreateGraphics
Dim i, r As Integer
Dim x, y, z, s, q, p, px, py As Single
Dim mx(600), my(600) As Integer
s = pi / 4
r = 100
i = 1
q = -pi / 2 + 0.1
For p = 0 To 2 * pi Step 0.2
x = r * Math.Cos(q) * Math.Sin(p)
y = r * Math.Sin(q)
z = r * Math.Cos(q) * Math.Cos(p)
px = x * 1.2
py = y - z * Math.Sin(s)
mx(i) = px + 200
my(i) = 150 - py
i = i + 1
Next p
point1.X = mx(1) : point1.Y = my(1)
For q = -pi / 2 + 0.2 To pi / 2 Step 0.2
i = 1
For p = 0 To 2 * pi Step 0.2
x = r * Math.Cos(q) * Math.Sin(p)
y = r * Math.Sin(q)
z = r * Math.Cos(q) * Math.Cos(p)
px = x * 1.2
py = y - z * Math.Sin(s)
point2.X = px + 200 : point2.Y = 150 - py
g.DrawLine(pen1, point1, point2)
point1 = point2
point3.X = mx(i) : point3.Y = my(i)
point4.X = px + 200 : point4.Y = 150 - py
g.DrawLine(pen1, point3, point4)
point3 = point4
mx(i) = px + 200
my(i) = 150 - py
i = i + 1
Next p
p = 0
x = r * Math.Cos(q) * Math.Sin(p)
y = r * Math.Sin(q)
z = r * Math.Cos(q) * Math.Cos(p)
px = x * 1.2
py = y - z * Math.Sin(s)
point5.X = px + 200 : point5.Y = 150 - py
g.DrawLine(pen1, point4, point5)
point4 = point5
Next q
End Sub
End Class
|
Conclusion | |
| J'espère que vous avez appris à dessiner dans Visual Basic.Net à l'aide de GDI+. Comme vous pouvez le constater, le dessin est assez | |
| simple mais peut avoir ses pièges, comme ne pas utiliser les instructions Using lors de la déclaration des différents objets. | |
También te puede interesar |
![]() |
Comment créer un outil Paint dans VB .Net |
Utilisez VB .Net et Active Directory |
Capture Webcam dans VB.NET |
Utilisation de ChatGPT dans VB .Net |
|