|
|
Title | Warp an image in VB .NET using DrawImage |
Keywords | warp, rotate, image, VB.NET, graphics |
Categories | Graphics, VB.NET |
|
|
The Graphics object's DrawImage method copies an image much as Visual Basic 6's PaintPicture method does. In VB .NET, however, there are 30 overloaded versions of this routine. One of them takes as parameters three points that represent where the routine should map the upper left, upper right, and lower left corners of the original image. DrawImage figures out where to put the remaining point to make the resulting figure a parallelogram. [It would have been better if it (also) let the programmer specify where to put all four points. Then you could perform the more useful mappings needed for three-dimensional graphics. Alas.]
You can use this version of DrawImage to warp an image. This program maps the points (0, 0), (wid, 0), and (0, hgt) to the points (wid * 0.5, 0), (0, hgt * 0.4), and (wid, hgt * 0.6). This reflects the image and warps it.
|
|
Private Sub Form1_Load(ByVal sender As Object, ByVal e As _
System.EventArgs) Handles MyBase.Load
' Get the source bitmap.
Dim bm_source As New Bitmap(picSource.Image)
' Make an array of points defining the
' transformed image's corners.
Dim wid As Single = bm_source.Width
Dim hgt As Single = bm_source.Height
Dim corners As Point() = { _
New Point(wid * 0.5, 0), _
New Point(0, hgt * 0.4), _
New Point(wid, hgt * 0.6)}
' Make a bitmap for the result.
Dim bm_dest As New Bitmap( _
CInt(bm_source.Width), _
CInt(bm_source.Height))
' Make a Graphics object for the result Bitmap.
Dim gr_dest As Graphics = Graphics.FromImage(bm_dest)
' Copy the source image into the destination bitmap.
gr_dest.DrawImage(bm_source, corners)
' Display the result.
picDest.Image = bm_dest
End Sub
|
|
|
|
|
|