What's New
Q & A
Tip Jar
C# Helper...
Follow VBHelper on Twitter
MSDN Visual Basic Community
TitleDraw a warped path in VB .NET
DescriptionThis example shows how to draw a warped path in Visual Basic .NET.
Keywordspath, GraphicsPath, warp, VB.NET
CategoriesGraphics, VB.NET
VB .NET has the ability to warp a graphics path by mapping the corners of one quadrilateral to another. When it needs to redraw, the program makes a GraphicsPath object and uses its AddLines method to add a series of connected lines to it. It then closes the path's current figure.

Next the program draws the untransformed path so you can see what the original looked like. It then creates a source rectangle and a series of points that define where the corners of the rectangle should be mapped. It calls the path's Warp method and then draws the resulting path. Finally it draws the source rectangle and destination polygon so you can see how the mapping works.

Private Sub Form1_Paint(ByVal sender As Object, ByVal e As _
    System.Windows.Forms.PaintEventArgs) Handles _
    Dim gr As Graphics = e.Graphics

    Dim pts() As PointF = { _
        New PointF(20.0F, 60.0F), _
        New PointF(25.0F, 90.0F), _
        New PointF(15.0F, 110.0F), _
        New PointF(15.0F, 145.0F), _
        New PointF(55.0F, 145.0F), _
        New PointF(55.0F, 110.0F), _
        New PointF(30.0F, 90.0F), _
        New PointF(50.0F, 60.0F) _

    Dim path As New GraphicsPath

    ' Draw the path before applying a warp transformation.
    gr.DrawPath(Pens.Blue, path)

    ' Define a warp transformation, and warp the path.
    Dim src_rect As New RectangleF(10.0F, 50.0F, 50.0F, _

    Dim dest_pts() As PointF = { _
        New PointF(120.0F, 10.0F), _
        New PointF(200.0F, 10.0F), _
        New PointF(100.0F, 150.0F), _
        New PointF(400.0F, 150.0F) _

    path.Warp(dest_pts, src_rect)

    ' Draw the warped path.
    gr.DrawPath(Pens.Green, path)

    ' Draw the source rectangle and the destination polygon.
    gr.DrawRectangle(Pens.Black, Rectangle.Round(src_rect))
    ' gr.DrawPolygon(Pens.White, dest_pts)
    gr.DrawLine(Pens.Red, dest_pts(0), dest_pts(1))
    gr.DrawLine(Pens.Red, dest_pts(0), dest_pts(2))
    gr.DrawLine(Pens.Red, dest_pts(1), dest_pts(3))
    gr.DrawLine(Pens.Red, dest_pts(2), dest_pts(3))
End Sub
Unfortunately there doesn't seem to be a method for mapping an image from one quadrilateral to another. That would be a useful transformation for three-dimensional graphics.
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.