Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

Graphics in VB.NET Part 1 - Bitmaps

VB.NET form

  • Please log in to reply
5 replies to this topic

#1 Vswe

Vswe

    CC Leader

  • Expert Member
  • PipPipPipPipPipPipPip
  • 1989 posts
  • Programming Language:Java, C#, PHP, Python, JavaScript, PL/SQL, Visual Basic .NET, Lua, ActionScript

Posted 07 November 2009 - 01:03 PM

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






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:


        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:


        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




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:


        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.

Edited by Vswe, 07 November 2009 - 02:45 PM.

  • 3

#2 Guest_Jordan_*

Guest_Jordan_*
  • Guest

Posted 07 November 2009 - 02:31 PM

Nice start to a good looking series! +rep
  • 0

#3 WingedPanther73

WingedPanther73

    A spammer's worst nightmare

  • Moderator
  • 17757 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others

Posted 07 November 2009 - 05:27 PM

Nice one. +rep
  • 0

Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

My MineCraft server site: http://banishedwings.enjin.com/


#4 so1i

so1i

    CC Addict

  • Advanced Member
  • PipPipPipPipPip
  • 297 posts

Posted 07 November 2009 - 05:59 PM

I liked this, cheers. +rep
  • 0

#5 RichardK123

RichardK123

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 10 September 2013 - 08:30 PM

Thank you very much, you have help with a graphics problem I have had for a while

Your examples were easy to understand and very complete. this added the finishing touch to my project

 

RichardSincerly


  • 0

#6 jadenviraj

jadenviraj

    CC Lurker

  • Just Joined
  • Pip
  • 1 posts

Posted 09 October 2014 - 06:09 AM

cool tutorial .. i like this


  • 0