Search Tools Links Login

Basic VB Graphics Programming - working with pixels


Visual Basic 6, or VB Classic

Many VB programmers don't understand the graphics capabilities of Visual Basic when you use basic API calls. This (and a series of forthcoming tutorials) will explain the basics of getting and setting pixels using both VB and the Windows API (via SetPixel, SetPixelV, and GetPixel), as well as using the API to set pixels on objects other than picture boxes (such as command buttons, frames, etc.).

Original Author: Tanner Helland

Code

Tanner's VB World - Graphics Programming in Visual Basic Tutorial: Setting and Getting Pixels

Graphics Programming in Visual Basic - Setting and Getting Pixels

By: Tanner Helland

Despite what many programmers will tell you, Visual Basic is an excellent programming language for high-end graphic applications. Its easy-to-use interface and programming language allows you to quickly and accurately create all sorts of neat programs without having to worry about the mess of C++ syntax. Also, you can use a number of easy API calls to speed up your interface to professional speed. So, here's part of how to become a professional graphics programmer using only VB.

-THE EASY WAY TO DO PIXEL STUFF-

This tutorial will go through the basic way to get and set pixels in Visual Basic. You will use both VB and the Windows API and see the differences between both methods. While this way of getting and setting pixels is slower then the forthcoming part 2 of this tutorial (using GetBitmapBits) it is significantly easier for a beginner, and will still offer impressive results.

PART I - GETTING COLORS

Before you can do anything to a picture, you have to first get the color of each pixel. There are two intelligent ways to do this, and both are extremely easy.

Way 1 - Using VB

You can use the Point event in VB to get the color of a specified pixel. The format is as follows:

Color = PictureBox.Point(x,y)

Where PictureBox is the name of the picture box or form you want to retrieve the pixel from, and (x,y) are the pixels coordinates. However, this method is quite slow, and for large pictures it will really start to rack up the time. So basically, don't use it. The best way to get pixels is to use the GetPixel API call:

Way 2 - Using the Windows API

Private Declare Function GetPixel lib "gdi32" (ByVal hDC as Long, ByVal x as Long, ByVal y as Long) as Long

Color = GetPixel(PictureBox.hDC, x, y) | where PictureBox is the name of the picture box or form you want to retrieve the pixel from, and (x,y) are the pixels coordinates. This method is many times faster then using VB, and it is basically the same call, except for the API declaration. I will write more on the API call structure in a future tutorial, but for now just trust me. J

PART II - DRAWING COLORS

Just as with getting pixels from a picture box or form, there are several ways to set pixels onto an object as well. Again, the internal VB method is very slow compared to the 2 API calls you can use. For you die-hard VB users, the very slow PSet command is the way to go:

PictureBox.PSet (x,y), Color

Whereas the API Calls are as follows:

Private Declare Function SetPixel lib "gdi32" (ByVal hDC as Long, ByVal x as Long, ByVal y as Long, ByVal Color as Long) as Long
SetPixel PictureBox.hDC, x, y, Color

Or:

Private Declare Function SetPixelV lib "gdi32" (ByVal hDC as Long, ByVal x as Long, ByVal y as Long, ByVal Color as Long) as Byte
SetPixelV PictureBox.hDC, x, y, Color

The only difference between the two functions, if you notice, is that SetPixel returns a Long (the color that the function was able to set) while SetPixelV returns a byte (whether or not the pixel was set). I would always recommend using SetPixelV, simply because it is slightly faster then SetPixel, but the difference is not very noticeable. So, you should now be able to quickly get and set pixels from any picture box or form, right? But, as always, I have some fun things you can add to the useless programming knowledge section of your brain (heh heh).

PART III - DRAWING AND GETTING COLORS FROM "SPECIAL" THINGS

Up until this point we've been relegated to using only picture boxes and forms because they're the only things that have an accessible hDC property, right? Well, there are certain ways to get around that so that we can set pixels on, say, a command button or a check box. To do this, we use the magical 'GetDC' API call:

Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Dim TemporaryHandle as Long
TemporaryHandle = GetDC(CommandButton.hWnd)

Now you can have all sorts of fun! Say, for some odd reason, that you want to set pixels on a command button. After using the GetDC call to assign a handle to the command button, you can do the SetPixel or SetPixelV call using the variable that contains the newly created hDC and - presto - you can draw on almost anything! Play with that API call for kicks if you ever get bored - it's kind fun...

Back to Tanner's VB World Home

Visit the homepage of Tanner Helland

About this post

Posted: 2002-06-01
By: ArchiveBot
Viewed: 204 times

Categories

Visual Basic 6

Attachments

No attachments for this post


Loading Comments ...

Comments

No comments have been added for this post.

You must be logged in to make a comment.