GDI+

edit

Graphics object

edit

A System.Drawing.Graphics object represents a drawing surface.

To get a Graphics object for a PictureBox control, use the .Graphics property of the PaintEventArgs parameter in the PictureBox's Paint() event.

    Private Sub PictureBox1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles PictureBox1.Paint
        Using g as Graphics = e.Graphics
            'use the methods of g to draw 
        End Using
    End Sub

To load an image and then draw on it, use Graphics.FromImage()

    Using MyImage As Bitmap = Bitmap.FromFile("C:\Temp\MyImage.BMP")
        Using g as Graphics = Graphics.FromImage(MyImage)
            'use the methods of g to draw 
        End Using
    End Using

The .Save() method can be used to save the Bitmap to a file or a Stream. Note: Bitmap objects created from a file keep the file open and are linked to the file while the object is in use. You will need to copy the file into a MemoryStream first if you do not want the Bitmap object to be linked to the file.

Drawing Methods

edit

Use methods such as Graphics.DrawLine and Graphics.DrawString to draw lines and text

Measurement

edit

Graphics.MeasureString can be used to measure how tall or wide a piece of text will be when output with .DrawString.

Scaling

edit

Simple scaling

edit

Use the Graphics.PageUnit property as a simple way to change the coordinate system. More complicated transformations and scaling are described below.

World, Page and Device coordinate systems

edit

When you supply coordinates to VB.NET graphics functions, these coordinates are part of the World Coordinate system. The units of the World coordinate system are defined by you, but the default is Pixels.

The World Transformation converts your World coordinates to Page coordinates. Page coordinates can use different units, but are device independent. The idea is that a distance expressed in Page coordinates will be the same if viewed on a monitor, or printed on a page.

The Page Transformation converts Page Coordinates to Device Coordinates. In other words, the Page Transformation ensures that the graphics output looks the same no matter what device the image is sent to. Device Coordinates are usually Pixels.

You can specify a translation matrix to convert the points you specify into the final output. This transformation can translate (offset), scale, rotate or skew the final output. This transformation is called an Affine Transformation.

Converting from Twips

edit

Previous versions of Visual Basic used Twips as the unit of measure. 1,440 Twips = 1 inch. However, VB.NET uses Pixels as its unit of measure. To convert from Twips to Pixels requires a Graphics object (since you don't know the number of dots-per-inch). Therefore, if XTwips is the measurement in Twips, then XPixels, the measurement in pixels, is calculated as follows (g is the appropriate Graphics object):

    XPixels = XTwips * g.DpiX / 1440

Use g.DpiY to calculate coordinates for the Y axis.