Archive
Categories
- Antivirus (4)
- Applications (5)
- Books and Articles (24)
- Computer Tutorials (10)
- Flash (3)
- Game (4)
- Graphic Design Tutorials (13)
- Internet Software (5)
- Internet Tutorials (12)
- Movie (8)
- Music (4)
- Novel Collections (5)
- Photoshop (10)
- Programming Tutorials (12)
- Template (3)
Tutorial : Graphics in VB.NET Part 1 – Bitmaps
2:39 AM |
Diposkan oleh
Plunturan Bersatu |
Edit Post
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.
Creating a Bitmap
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:
[highlight=VB.NET] Dim Img As New Bitmap(500, 300)[/highlight]
This example creates a bitmap by giving its size as two integers, so this bitmap will have the width 500 and the height 300.
[highlight=VB.NET] Dim Img As New Bitmap("C:\test.JPG")[/highlight]
[highlight=VB.NET] Dim Img As New Bitmap("C:\test.JPG")[/highlight]
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:
[highlight=VB.NET] Me.BackgroundImage = Img[/highlight]
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:
[highlight=VB.NET] Dim Img As New Bitmap("C:\test.JPG")
Dim W As Integer = Img.Width
Dim H As Integer = Img.Height
Dim S As Size = Img.Size[/highlight]
Modifying pixels
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:
[highlight=VB.NET] Dim Img As New Bitmap(50, 50)
Dim C As Color = Img.GetPixel(30, 30)
Img.SetPixel(10, 10, C)[/highlight]
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:
[highlight=VB.NET] Dim Img As New Bitmap(50, 50)
Img.Save("C:\test.bmp")[/highlight]
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:
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:
Code:
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:
[highlight=VB.NET] Dim Img As New Bitmap(50, 50)
Img.Save("C:\test.JPG", Imaging.ImageFormat.Jpeg)[/highlight]
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.
Label:
Programming Tutorials,
Tutorial
Subscribe to:
Post Comments (Atom)
0 komentar:
Post a Comment