In this 4 parts long tutorial series I will introduce you to graphics in VB.NET. The four parts is meant to be in order so I recomend you to read them in order to be able to understand the later parts.

First we'll start with creating a simple bitmap. Bitmaps is a common Image format which is the data type that is used for images in VB.NET. There's a lot of different parameter combinations(12) we can use when creating a new bitmap but I'll only show two of them:

Dim Img As New Bitmap(500, 300)

This example creates a bitmap by giving its size as two integers, so this bitmap will have the width 500 and the height 300.

Dim Img As New Bitmap("C:\test.JPG")

In this example we're using a path to get an image we have on the computer.

When we're only testing and wants to see how our image looks like we can set it as the background image to our form. When you're not testing anymore you will probably use other things for showing your image (for example Pictureboxes). Just how to easy show it an image:

Me.BackgroundImage = Img

The Bitmap's size

When we have an Image we often need to know how big it is, this is simply made by using the width property and the height property or only using the size property:

If we want to modify our image we can change the color of some pixels by using SetPixel together with the pixels X and Y index together with a color(data type color), to get the color of a pixel we'll use GetPixel together with the X and Y index. A simple example:

Dim Img As New Bitmap(50, 50)

Dim C As Color = Img.GetPixel(30, 30) Img.SetPixel(10, 10, C)

In this example we first declared a new Bitmap(50x50) then we stored the color of the Pixel at X=30 and Y=30 in a variable of the data type color. This color we applied to the pixel at index(10,10). However since all the pixels was empty we won't see any difference in this image.

Saving a Bitmap

We may also save an image on the computer by using Save. It could look like this:

Dim Img As New Bitmap(50, 50)

Img.Save("C:\test.bmp")

In this example the image stored in the bitmap variable will be saved at "C:\test.bmp". But maybe we don't want to save the image with bitmap format. Then we have to change it by adding the correct System.Drawing.Imaging.ImageFormat as another parameter, we also need to change the extension in the file path. The different formats supported is:

Bmp
Emf
Exif
Gif
Icon
Jpeg
MemoryBmp
Png
Tiff
Wmf

And here's an example how to use it, in this example I'll save an image as Jpeg instead:

Dim Img As New Bitmap(50, 50)

Img.Save("C:\test.JPG", Imaging.ImageFormat.Jpeg)

In this part we'll only talked about the Bitmap data type, in the next part I'll show you the Graphics data type which helps us modify our bitmaps.