+ Reply to Thread
Results 1 to 4 of 4

Thread: Graphics in VB.NET Part 1 - Bitmaps

  1. #1
    Join Date
    Apr 2009
    Location
    Uppsala, Sweden
    Posts
    9,547
    Blog Entries
    5
    Rep Power
    98

    Graphics in VB.NET Part 1 - Bitmaps

    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.

    Graphics in VB.NET
    1. Bitmaps
    2. Graphics
    3. Figures
    4. Strings





    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]
    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:

    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.
    Last edited by Vswe; 11-07-2009 at 02:45 PM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    Jordan Guest

    Re: Graphics in VB.NET Part 1 - Bitmaps

    Nice start to a good looking series! +rep

  4. #3
    Join Date
    Jul 2006
    Posts
    16,491
    Blog Entries
    75
    Rep Power
    143

    Re: Graphics in VB.NET Part 1 - Bitmaps

    Nice one. +rep
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

  5. #4
    so1i's Avatar
    so1i is offline Programming Professional
    Join Date
    Sep 2009
    Location
    Aberystwyth, United Kingdom
    Posts
    309
    Rep Power
    0

    Re: Graphics in VB.NET Part 1 - Bitmaps

    I liked this, cheers. +rep

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. C# Tutorial: Cropping images and bitmaps
    By ArekBulski in forum CSharp Tutorials
    Replies: 13
    Last Post: 12-10-2010, 11:37 AM
  2. Graphics in VB.NET Part 3 - Figures
    By Vswe in forum Visual Basic Tutorials
    Replies: 5
    Last Post: 01-13-2010, 12:01 PM
  3. Graphics in VB.NET Part 2 - Graphics
    By Vswe in forum Visual Basic Tutorials
    Replies: 4
    Last Post: 11-07-2009, 06:03 PM
  4. Graphics in VB.NET Part 4 - Strings
    By Vswe in forum Visual Basic Tutorials
    Replies: 2
    Last Post: 11-07-2009, 05:31 PM
  5. Comparing two monochrome bitmaps
    By brownhead in forum C and C++
    Replies: 9
    Last Post: 08-14-2009, 04:20 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts