Giáo trình Visual Basic
Trang 1Đồ họa trong NET
• GDI+ (Graphics Device Interface)
Là hệ thống nâng cấp từ GDI - là hệ thống các thư viện hỗ trợ đồ họa
để người lập trình có thể xây dựng chương trình ứng dụng đồ họa Gồm các khối :
• System.Drawing
• System.Drawing.Drawing2D
• System.Drawing.Imaging
• System.Drawing.Text
• System.Drawing.Printing
GDI+ giúp ta có thể tạo ra các hình vẽ hiện trên màn hình và in ra trên các thiết bị in
• Tạo đối tượng graphics
(Creating a Graphics Object)
Đối tượng graphics được coi như trang giấy phủ lên bề mặt đối tượng
và ta vẽ hình trên đó theo chế độ vẽ vecto Cách này vẽ hình nhanh, đơn giản
Dùng phương thức CreateGraphics
Ví dụ
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim g As Graphics
Dim point1, point2 As Point
point1.X = 100
point1.Y = 100
point2.X = 800
point2.Y = 600
g = Me.CreateGraphics
g.DrawLine(Pens.Blue, point1, point2)
EndSub
+ Mô tả các đối tượng đồ họa
Dim point1, point2 As Point
Dim x As Rectangle
Dim pent1 As New Pen(Color.Red, 3)
Dim choi As New SolidBrush(Color.Red)
+ Cách phương thức vẽ cơ bản vẽ
g.DrawLine(pen, point0, point1)
Trang 2g.DrawEllipse(brush, pen, center,radiusX, radiusY)
g.DrawRectangle(pent1, rect)
Ví dụ
Private Sub Button1_Click( ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
Button1.Click
Dim g As Graphics
Dim point1, point2 As Point
Dim rect As Rectangle
Dim pent1 As New Pen(Color.Red, 3)
Dim redBrush As New SolidBrush(Color.Red)
rect.X = 10
rect.Y = 10
rect.Width = 200
rect.Height = 100
point1.X = 100
point1.Y = 100
point2.X = 800
point2.Y = 600
g = Me CreateGraphics
g.DrawRectangle(pent1, rect)
Dim x As Integer = 0
Dim y As Integer = 0
Dim width As Integer = 200
Dim height As Integer = 100
Dim rect1 As New Rectangle(x, y, width, height) ' Fill ellipse on screen.
g.FillEllipse(redBrush, rect1)
End Sub
Ví du 2
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim g As Graphics
Dim rect As Rectangle
rect.X = 10
rect.Y = 10
rect.Width = 200
rect.Height = 100
point1.X = 100
point1.Y = 100
point2.X = 800
point2.Y = 600
Trang 3g = Me.CreateGraphics
g.DrawRectangle(Pens.Blue, rect)
EndSub
5- Phương thức DrawImage
Có thể dùng phương thức này để vẽ lại các ảnh gốc lên đối tượng graphics g
ở dạng bitmap Ảnh gốc có thể là các file ảnh dạng : BMP, GIF, JPEG, EXIF, PNG, TIFF và ICON
Ví dụ1 :
Bitmap a1 = new Bitmap("C:\anh1.bmp");
Bitmap a2 = new Bitmap("C:\anh2.gif");
Bitmap a3 = new Bitmap("c:\anh3.jpg");
Bitmap a4 = new Bitmap("c:\anh4.png");
Bitmap a5 = new Bitmap("c:\anh5.tif");
g.DrawImage(a1, 10, 10);
g.DrawImage(a2, 220, 10);
g.DrawImage(a3, 280, 10);
g.DrawImage(a4, 150, 200);
g.DrawImage(a5, 300, 200);
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button1.Click
Dim g As Graphics
Dim rect1 As Rectangle
Bitmap a1 = new Bitmap("C:\h1.bmp");
rect1.Height = 100
rect1.Width = 100
rect1.X = 50
rect1.Y = 50
g = Me.CreateGraphics
g.DrawImage(a1, rect1)
-Ví dụ vẽ Đồ thị hàm sinx
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles utton1.Click
Dim a, b, tlx, tly, x0, x1, y0, y1 AsDouble
Dim rong, cao As Double
Trang 4Dim g As Graphics
Dim pen1 As New Pen(Color.Red, 3) rong = Me Width
cao = Me Height
a = 0
b = 12.66 '(4pi)
tlx = rong / (b - a)
tly = 100
g = Me.CreateGraphics()
Dim point1 AsNew Point(0, 0)
Dim Point2 AsNew Point(0, 0)
've truc tung
point1.X = 0
point1.Y = 0
Point2.X = 0
Point2.Y = Convert.ToInt16(cao)
've truc hoanh, điểm tịnh tiến gốc (0,cao/2)
g.DrawLine(pen1, point1, Point2)
point1.X = 0
point1.Y = Convert.ToInt16(cao / 2)
Point2.X = Convert.ToInt16(rong)
Point2.Y = Convert.ToInt16(cao / 2)
g.DrawLine(pen1, point1, Point2)
'tính điểm đầu
x0 = a
y0 = Math.Sin(a)
x1 = a
point1.X = Convert.ToInt16((x0 - a) * tlx)
point1.Y = Convert.ToIn16(cao / 2 - (y0 * tly)) pen1.Color = Color.Blue
pen1.Width = 2
DoWhile (x1 <= b)
x1 = x1 + 0.05
y1 = Math.Sin(x1)
Trang 5Point2.X = Convert.ToInt16((x1 - a) * tlx) Point2.Y = Convert.ToInt16(cao / 2 - (y1 * tly)) g.DrawLine(pen1, point1, Point2)
point1.X = Point2.X
point1.Y = Point2.Y
Loop
EndSub