VB Graphics Programming: Part 3 (Advanced API)

Advanced API Pixel Routines

Next comes two advanced ways of getting and setting pixels in Visual Basic: the API routines of GetBitmapBits/SetBitmapBits and GetDIBits/StretchDIBits.  If you haven’t already, I strongly recommend reading the previous two tutorials, “Pure VB Pixel Routines” and “Basic API Pixel Routines,” as they provide the foundation for the advanced graphics principles discussed in this section.

Assuming that you now understand how to use both Visual Basic and the API to get per-pixel data, it’s time to move to the next – and most difficult – section of these tutorials.  Next we are going to discuss the API routines of GetBitmapBits/SetBitmapBits and GetDIBits/StretchDIBits.  Both sets of routines are very fast and very powerful, but they come with a strong disclaimer – pay close attention to any red warnings on this page.  Because these API routines directly interface the heap (dynamically allocated memory), you can easily crash both the VB IDE and/or Windows with a page fault or worse if you use them incorrectly.  Believe me – it’s not a pretty sight to watch your entire machine freeze because you accidentally allocated your array to the wrong size.  (But it gives you a good taste of programming in non-BASIC languages, heh heh.)

But on the happy side of things, these are about as fast as graphics get in VB.  There are ways to use CopyMemory, in-line assembly language, and other freakish routines to get slightly faster effects, but they are not designed specifically for graphics programming so I’m going to avoid them here.

On to the programming!

I – Declaring the Necessary API Functions

At this point in your VB career you are probably used to interacting with images one pixel at a time (using something like the afore-taught .Point/.PSet or GetPixel/SetPixel/V).  These functions are simple to use, but that ease comes at the cost of speed. Many things cause these functions to be slow (as discussed in the first page of this tutorial), so you must be wondering – is there a way to remove some of those speed barriers?

Enter GetBitmapBits and SetBitmapBits.  The big advantage of these two API calls is this: rather than extracting or setting each pixel in an image individually, we pass each of these functions an array and let them fill the whole thing at once with the picture’s pixel data (or set the picture’s pixel data all at once with the information in the array).  This is obviously much more efficient.  The trade-off, of course, is that these techniques involve a little more programming and significantly more risk.  If the array dimensions are off by a mere 1 byte all kinds of things can happen – the picture won’t appear at all, your program will shut itself down, or VB will freeze.  But these only happen if you’re careless and don’t heed my warnings, so pay close attention and you’ll be fine.

We start by declaring a whole bunch of things:

Private Type Bitmap
   bmType As Long
   bmWidth As Long
   bmHeight As Long
   bmWidthBytes As Long
   bmPlanes As Integer
   bmBitsPixel As Integer
   bmBits As Long
End Type

Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, _
  ByVal nCount As Long, ByRef lpObject As Any) As Long

Private Declare Function GetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, _
  ByRef lpBits As Any) As Long

Private Declare Function SetBitmapBits Lib "gdi32" (ByVal hBitmap As Long, ByVal dwCount As Long, _
  ByRef lpBits As Any) As Long

This might seem a little extreme, so let me go through each of these one at a time.

The Bitmap type is required for the GetObject call – if you’ll look at the GetObject declaration, you’ll notice that the last parameter is of type Any. This is where we will be passing our Bitmap object. As for the individual elements of the Bitmap type, typical graphics programming only cares about four out of the seven variables. They are:

  • bmWidth – the width of the bitmap, in pixels
  • bmHeight – the height of the bitmap, in pixels
  • bmWidthBytes – the width of a bitmap in bytes. If a bitmap is 24 bits-per-pixel (bpp), that means that each pixel occupies 3 bytes. So in this color mode, bmWidthBytes would be (bmWidth * 3). If a bitmap were 16 bpp, each pixel would occupy 2 bytes. In that color mode, bmWidthBytes would be (bmWidth * 2).
  • bmBitsPixel – the number of bits per pixel in the image. In 24bpp mode, the number is 24. In 16bpp mode, the number is 16. Pretty straightforward. Divide this number by 8 to get the number of bytes per pixel.

The other three variables aren’t needed for getting and setting pixels; we simply include them to ensure that our Bitmap type matches the Windows Bitmap type.

Next we have the GetObject call.  The purpose of this API call is to…well, get an object.  You’ll see how this works in a moment.

  • hObject is an object (containing a picture) that we want to get information about – most likely PictureBox.Image or Form.Image.  The properties of hObject will be transferred into lpObject (see below).
  • nCount is the size of the type that is going to receive the information (in our case, the size of the Bitmap type).
  • lpObject is the variable that is going to hold all of the information that we get from hObject (a variable of the Bitmap type we’ve just declared, in fact).  Notice that it is passed ByRef – this allows the API call to edit that variable directly.

GetBitmapBits and SetBitmapBits have identical parameters, which in turn are almost identical to the GetObject parameters.

  • hBitmap represents an object containing a picture (like hObject above, most likely PictureBox.Image)
  • dwCount is the total size of the array holding the image’s pixel data
  • lpBits is the starting address of the place in memory where we want to place the image data (almost always the first spot of an array). Notice, again, that it is declared as ByRef – this allows the API call to edit the array directly (which is a good thing, because that’s how we get the image data!).

Okay – that’s a whole lot of information in a small space, so take a quick break to make sure you understand those declarations.  If some of this is a little hazy, that’s okay, because we’re about to see how they work.

II – Getting Pixels Using GetBitmapBits

Ready?  If so, here’s how we use GetBitmapBits:

Dim bm As Bitmap

GetObject PictureBox.Image, Len(bm), bm

Dim ImageData() as Byte

ReDim ImageData(0 To (bm.bmBitsPixel \ 8) - 1, 0 To bm.bmWidth - 1, 0 To bm.bmHeight - 1)

GetBitmapBits PictureBox.Image, bm.bmWidthBytes * bm.bmHeight, ImageData(0, 0, 0)

Surprisingly, this procedure is very straightforward.  First, we declare a Bitmap object and call the GetObject function.  When called, GetObject will analyze the specified PictureBox and assign the appropriate values to our Bitmap object, which we can then use to prepare our array to receive the image’s pixel data.

Once we have all the picture’s information available to us in the form of a Bitmap object, we declare an array. This ImageData array is of critical importance – we’re going to use it to hold all of the picture’s pixel information.  To make sure it is the right size, we use ReDim to make its dimensions just perfect:

  • The first dimension will contain the values of each pixel’s red, green, and blue values. (bm.bmBitsPixel should equal 24 because your computer is in 24 bit color mode; thus we get 24/8 = 3 bytes per pixel. Because we start the dimension at zero, we subtract one from the upper bound to give us three total spots: 0, 1, and 2, which correspond to red (2), green (1), and blue (0))
  • The second dimension will be used to address the x coordinates of the image’s pixels.
  • The third dimension will used to address the y coordinates of the image’s pixels.

SIDE NOTE ABOUT ‘GETOBJECT’: You may be wondering why we use the GetObject call at all – couldn’t we just resize the ImageData array using the picture box’s ScaleWidth and ScaleHeight properties?  In theory, you could.  However, VB5 does strange things to the ScaleWidth and ScaleHeight properties depending on what is stored there.  For example, the same image might report different ScaleWidth and ScaleHeight properties at different execution times.  In my experience, JPEGs are notoriously bad at this – when you load one, VB5 sometimes thinks that the picture’s width is one pixel less in the picture box than it is in memory.  Honestly, I have no idea as to why VB5 has this problem.  VB6 seems to work fine.  GetObject is always accurate so I use it instead, and I recommend that you do too.  There is no measurable speed difference between these two mechanisms.

SIDE NOTE ABOUT DECLARING YOUR ARRAY: Arrays supplied to GetBitmapBits (and later in this tutorial, GetDIBits) must have a width that is a multiple of 4, e.g. 4, 8, 16, 256, 360, etc. If your image has a width that is a multiple of four, no worries – but if it is not a multiple of four, you will need to adjust your code accordingly. See the bottom of this page for details (in the section titled “VI – OPTIONAL: The Infamous 4-Byte Alignment Issue”).

ANOTHER SIDE NOTE ABOUT DECLARING YOUR ARRAY: You can use any number of dimensions in your array, so long as the total size is accurate. For example, you could also do something like ReDim ImageData(0 to bm.bmWidth * bm.bmHeight * 3) and the function would still work fine. The API call could care less about how the array is declared – all it gets is the address of the first element in the array and the number of bytes that it’s allowed to work with. The way the array is dimensioned is only for your convenience.  I like the above way because it makes editing the image very easy. This issue will be discussed further in the next section of this tutorial.

WARNING!! This ReDim statement is where you can really screw your computer.  If ImageData is too small, GetBitmapBits will attempt to put the picture data in unallocated memory – causing a general protection fault, a page fault, or some other nasty illegal operation.  Make sure that ImageData is the right size!

The GetBitmapBits call itself is very straightforward: it takes the array (in this case, ImageData()) and fills it with the pixel data located in PictureBox.  Now you can edit the values any way you want.  For example, the following loop would invert all of the pixels in the image:

'First, get the image data using the above code section

Dim X as long, Y as long

For X = 0 to PictureBox.ScaleWidth - 1
For Y = 0 to PictureBox.ScaleHeight - 1

   'Invert the R value
   ImageData(2, X, Y) = 255 - ImageData(2, X, Y)
   'Invert the G value
   ImageData(1, X, Y) = 255 - ImageData(1, X, Y)
   'Invert the B value
   ImageData(0, X, Y) = 255 - ImageData(0, X, Y)
Next Y
Next X

Really, GetBitmapBits is as easy as GetPixel if you understand the API structure.

III – Setting Pixels Using SetBitmapBits

SetBitmapBits is almost identical to GetBitmapBits:

Dim bm As Bitmap

GetObject PictureBox.Image, Len(bm), bm

SetBitmapBits PictureBox.Image, bm.bmWidthBytes * bm.bmHeight, ImageData(0, 0, 0)

If PictureBox.AutoRedraw = True Then
   PictureBox.Picture = PictureBox.Image
   PictureBox.Refresh
End If

Everything is the same as GetBitmapBits, except that we aren’t resizing the array (because it is already the right size and resizing it would erase all of its information!).  The last If/Then statement is included because SetBitmapBits won’t automatically initialize the AutoRedraw event, so we have to tell it to replace the Picture property (what is shown on the screen) with the Image property (what is stored in memory).

I hope you’re finding this easier than expected!  In fact, it’s almost too easy… so of course, there is a slight problem with this method: both GetBitmapBits and SetBitmapBits only work in 24/32-bit color mode (16.7 million colors). Actually, they work in 16 and 8 bit color modes too, but the image data no longer occupies 3bpp (bpp = bits per pixel) so editing the image data is significantly more complicated.  It can be done, but you have to write a function to translate 2 bits into 3 as well as transferring the data into a separate array while you edit it.  Then, to draw it, you have to translate the 3 bits back into 2 bits and then transfer your editing array back into the original one.  It’s messy and time-intensive, so I wouldn’t recommend this method

So of course, someone is going to ask “but how can I do fast graphics in 16 or 8 bit color mode?” That is what DIB sections are for, so if you want to know about them then keep reading.

(Personally, I would recommend using DIB sections for all of your graphics programs because you’ll never get unexpected color-mode errors with them, and it’s a great way to add additional functionality to your graphics program.  I have only discussed BitmapBits because they make for an excellent introduction to DIB sections.)

IV – A Crash Course in Declaring DIB Sections

DIB section stands for ‘Device Independent Bitmap.’  The name is pretty self-explanatory: DIBs are simply a way of interacting with bitmaps in any color mode or on any computer and getting consistent results.  There are actually two varieties of DIBs – OS/2 encoded and Windows encoded, so I guess “device independent’ isn’t totally accurate… but that’s okay.

DIBs share many characteristics with BitmapBits.  The calls share certain parameters and the underlying logic is very much the same.  However, DIB sections have several major differences you need to be aware of: they’re slightly more confusing to use, they require more code, and they return the image data upside-down.  The most important difference, however, is that DIB sections work in any color mode – and, as a bonus, the StretchDIBits call is much more powerful than SetBitmapBits. Below are the required DIB section declarations:

Private Type BITMAP
   bmType As Long
   bmWidth As Long
   bmHeight As Long
   bmWidthBytes As Long
   bmPlanes As Integer
   bmBitsPixel As Integer
   bmBits As Long
End Type

Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, _
  ByVal nCount As Long, ByRef lpObject As Any) As Long

Private Type RGBQUAD
   rgbBlue As Byte
   rgbGreen As Byte
   rgbRed As Byte
   rgbAlpha As Byte
End Type

Private Type BITMAPINFOHEADER
   bmSize As Long
   bmWidth As Long
   bmHeight As Long
   bmPlanes As Integer
   bmBitCount As Integer
   bmCompression As Long
   bmSizeImage As Long
   bmXPelsPerMeter As Long
   bmYPelsPerMeter As Long
   bmClrUsed As Long
   bmClrImportant As Long
End Type

Private Type BITMAPINFO
   bmHeader As BITMAPINFOHEADER
   bmColors(0 To 255) As RGBQUAD
End Type

Private Declare Function GetDIBits Lib "gdi32" (ByVal hDC As Long, ByVal hBitmap As Long, _
  ByVal nStartScan As Long, ByVal nNumScans As Long, lpBits As Any, lpBI As BITMAPINFO, _
  ByVal wUsage As Long) As Long

Private Declare Function StretchDIBits Lib "gdi32" (ByVal hDC As Long, ByVal x As Long, _
  ByVal y As Long, ByVal dWidth As Long, ByVal dHeight As Long, ByVal SrcX As Long, _
  ByVal SrcY As Long, ByVal SrcWidth As Long, ByVal SrcHeight As Long, lpBits As Any, _
  lpBI As BITMAPINFO, ByVal wUsage As Long, ByVal RasterOp As Long) As Long

Quite the mess of declarations, isn’t it? You should notice some similarities between these declarations and the BitmapBits ones. Here’s a quick explanation of the DIBits calls:

  • The first type and declaration are just the same old GetObject stuff – you already know all about that.
  • The next type, RGBQuad, represents basic pixel data – red, green, and blue values, along with an alpha channel.

SIDE NOTE ON ALPHA CHANNELS: For those who are interested: both DIBs and regular bitmaps can contain transparency information.  If you have (what used to be, heh) an expensive monitor and video card and run them at 32-bit color mode, that extra byte contains transparency data (a number from 0-255, 0 being opaque and 255 being transparent).  So technically, 32-bit bitmaps and DIBs could be used like GIFs or PNGs and displayed transparently.  There’s actually an API call with Win2K/ME/XP called AlphaBlend that’s similar to BitBlt/StretchBlt except that it utilizes an alpha channel (this method is very similar to DirectX and a transparent key color); you can read all about the AlphaBlend call at MSDN.

  • The BITMAPINFOHEADER type contains all of a particular bitmap’s information, unlike the stripped-down version we use for GetObject.  This is what makes DIB sections “Device Independent” – by knowing all of that extra information about the bitmap we can display it accurately on any device at any color resolution.
  • Lastly, the BITMAPINFO class combines a header and an array of RGBQUADs (used for the palette in 8-bit images).  This header is capable of holding data for DIB sections of any color depth – from 1bpp to 32bpp.  Again, this is part of making DIB sections “Device Independent.”  It also comes in handy when using 8bpp color modes because you can directly edit the palette for super-fast graphics effects.

The GetDIBits call is somewhat more complicated than the GetBitmapBits one, so let’s go through it one part at a time.

  • hDC is the same as it was in GetPixel – it is the handle/”address” of the device we want to get the pixel data from. Most likely, this is PictureBox.hDC or Form.hDC.
  • hBitmap is the location of the pixel data itself. This is most commonly PictureBox.Image or Form.Image
  • nStartScan is the line we want to start reading the pixel data from.  This will always be 0, unless for some odd reason you want to read the data from the middle of the image.
  • nNumScans is the number of horizontal lines that you want to read from the image.  This will always be the height of the image (in pixels), unless you want to extract every-other-line or something strange like that.  (Scanlines also come in handy for doing fast image rotations, but I’m not going to discuss those here ;) )
  • lpBits is the same as it was in GetBitmapBits.  This is the array that the image’s data will be copied into.
  • lpBI is a BitmapInfo variable that contains all of the desired information of the bitmap we are want to get.  This includes the width, height, and – important! – the color depth.  This is how we let Windows know that even though the computer may be in 16bpp or 8bpp color modes, we want the bitmap information to be in 24bpp mode (color depth is handled via the .bmBitCount property).
  • Last is the wUsage variable, which is related to referencing source-DC-based palettes. Because this tutorial focuses only on 24-bit (i.e. non-paletted) image processing, we’re going to ignore this variable completely – just always leave it as zero.

We’re almost done!

Unfortunately, if you thought GetDIBits was long-winded, you’re not going to like StretchDIBitsStretchDIBits is a very powerful call, but this means there are a lot of parameters.  If you are familiar with BitBlt and/or StretchBlt this part will probably make a lot of sense to you.  The StretchDIBits parameters are:

  • hDC: same as GetDIBits
  • x: the x coordinate of the top-left corner you want to draw the pixels to
  • y: the y coordinate of the top-left corner you want to draw the pixels to
  • dWidth: the desired width of the destination image.  This is usually the same size as the image you got the data from, although you can stretch or shrink the destination size if you wish.
  • dHeight: the desired height of the destination image.  Refer to the dWidth notes.
  • SrcX: the x coordinate of the top-left pixel in the source array (for us, ImageData()) that you want to start taking the pixels from. Usually zero, but you can change this value to only take a portion of the source image.
  • SrcY: the y coordinate of the top-left pixel in the source array that you want to start taking pixel data from.  Refer to the SrcX notes.
  • SrcWidth: the desired width of the pixel selection from the source image. This is usually the same size as the original image, although you can stretch or shrink the source size if you wish.
  • SrcHeight: the desired height of the pixel selection from the source image. Refer to the SrcWidth notes.
  • lpBits: same as GetDIBits. This is the array that the pixel data will be taken from (again, ImageData())
  • lpBi: same as GetDIBits. This is the BitmapInfo variable that contains all of the correct parameters for our pixel data.
  • wUsage: same as GetDIBits – leave it as zero.
  • RasterOp: a raster operation, exactly the same as you would use for BitBlt or StretchBlt. There is a complete list of RasterOp constants listed in the VB help files (or MSDN collection), but the most common one is vbSrcCopy, which will simply copy the pixels from the source array to the destination picture box.

V – Using DIB Sections

Now that your brain has had some time to digest all of those declarations, let’s demonstrate the GetDIBits call.  Here’s a full-blown example of how to get an image’s data using the GetDIBits call, minus the declarations above:

'Routine to get an image's pixel information into an array dimensioned (rgb, x, y)
Public Sub GetImageData(ByRef SrcPictureBox As PictureBox, ByRef ImageData() As Byte)

'Declare variables of the necessary bitmap types
Dim bm As Bitmap
Dim bmi As BITMAPINFO

'Now we fill up the bmi (Bitmap information variable) with all the necessary data
bmi.bmHeader.bmSize = 40 'Size, in bytes, of the header (always 40)
bmi.bmHeader.bmPlanes = 1 'Number of planes (always one)
bmi.bmHeader.bmBitCount = 24 'Bits per pixel (always 24 for image processing)
bmi.bmHeader.bmCompression = 0 'Compression: none or RLE (always zero)

'Calculate the size of the bitmap type (in bytes)
Dim bmLen As Long
bmLen = Len(bm)

'Get the picture box information from SrcPictureBox and put it into our 'bm' variable
GetObject SrcPictureBox.Image, bmLen, bm

'Build a correctly sized array.
ReDim ImageData(0 To 2, 0 To bm.bmWidth - 1, 0 To bm.bmHeight - 1)

'Finish building the 'bmi' variable we want to pass to the GetDIBits call
bmi.bmHeader.bmWidth = bm.bmWidth
bmi.bmHeader.bmHeight = bm.bmHeight

'Now that we've filled the 'bmi' variable, we use GetDIBits to take the data from SrcPictureBox and put
'  it into the ImageData() array using the settings specified in 'bmi'
GetDIBits SrcPictureBox.hDC, SrcPictureBox.Image, 0, bm.bmHeight, ImageData(0, 0, 0), bmi, 0

End Sub

We’re almost done with DIB sections – all that’s left is StretchDIBits.

The procedure required to set up our variables for StretchDIBits is almost identical to the procedure we used for GetDIBits. In fact, everything up to the actual GetDIBits call is the same – everything except the ReDim ImageData() line, of course.  (If we ReDimmed the array before setting it, we would erase all of the pixel data!).  Here’s a full example:

'Routine to set an image's pixel information from an array dimensioned (rgb, x, y)
Public Sub SetImageData(ByRef DstPictureBox As PictureBox, ByRef ImageData() As Byte)

'Variables for the necessary bitmap types
Dim bm As Bitmap
Dim bmi As BITMAPINFO

'Fill the bmi (Bitmap information variable) with appropriate values
bmi.bmHeader.bmSize = 40
bmi.bmHeader.bmPlanes = 1
bmi.bmHeader.bmBitCount = 24
bmi.bmHeader.bmCompression = 0

'Calculate the size of the bitmap type (in bytes)
Dim bmLen As Long
bmLen = Len(bm)

'Get the picture box information from DstPictureBox and put it into our 'bm' variable
GetObject DstPictureBox.Image, bmLen, bm

'Now that we know the object's size, finish building the temporary header to pass to StretchDIBits
bmi.bmHeader.bmWidth = bm.bmWidth
bmi.bmHeader.bmHeight = bm.bmHeight

'Use StretchDIBits to take the data from the ImageData() array and put it into SrcPictureBox using
'  the settings specified in 'bmi'
StretchDIBits DstPictureBox.hDC, 0, 0, bm.bmWidth, bm.bmHeight, 0, 0, bm.bmWidth, bm.bmHeight, _
  ImageData(0, 0, 0), bmi, 0, vbSrcCopy

'Since this doesn't automatically initialize AutoRedraw, we have to do it manually
'Note: set AutoRedraw to 'True' when using DIB sections. Otherwise, you may get unpredictable results.
If DstPictureBox.AutoRedraw = True Then
   DstPictureBox.Picture = DstPictureBox.Image
   DstPictureBox.Refresh
End If

End Sub

And there you have it – a complete explanation of how to get image data from any picture in any color mode and how to set that same data back into a picture once you’re done editing it.

VI – DIB Sections in Action

This .zip file shows these exact routines – cut and pasted out of this tutorial into a form – being used to adjust the brightness of an image.  Quite a bit better than GetPixel and SetPixel or .Point and .PSet, aren’t they?

It may surprise you, but in the next tutorial we’re going to get this program running even faster – we’re going to use some DIB section tricks to make it run in real-time.

Before we continue, however, I am including an optional section regarding the infamous 4-bit alignment issue associated with DIB sections.  If you are interested in using DIB sections only casually, this issue may not apply to you.  If, however, you plan on using DIB sections extensively, this issue is critical.  DIB sections have trouble if you use them on an image whose width is not a multiple of 4.  We’ll discuss how to deal with this problem in the optional section below.

CONTINUE TO “TANNER’S TOP 10 LIST OF GRAPHICS OPTIMIZATIONS”

VI – OPTIONAL: The Infamous 4-Byte Alignment Issue

As you may know, all versions of Windows at the time of this writing (95, 98, ME, NT4, 2000, XP) are 32-bit operating systems.  This means that memory within Windows is split up into 32-bit, or 4-byte, chunks.  Normally this means very little to a VB6 programmer, but when using DIB sections we must take this into account.

Because DIB sections are designed to be fast, they’re optimized for speed in many ways.  One such way is that they require any arrays associated with them to be 4-bytes (or 32 bits) wide.  This allows the arrays to line up in memory exactly – without any trailing bytes – which in turn allows Windows to access the information more quickly, since it doesn’t have to re-align each horizontal line of the image to match up with 32-bit memory spaces.

As it turns out, this isn’t a problem for images whose width is already a multiple of 4: 800x600, 32x32, 1920x1080, etc.  All standard image/screen sizes are multiples of 4 for a reason.

Sometimes, however, it’s necessary to work with images whose width may not be divisible by 4 (e.g. 29, 30, and 31 instead of 32).  This creates serious problems with DIB sections – try plugging such an image into the code above and you’ll see what I mean.

So how do we solve such a problem?  There’s no good way, to be honest.  Two main options exist:

1) Resize the image to make it have a width that’s a multiple of 4.

2) Manually force the array containing the image data to have a width that’s a multiple of 4.

The first option is preferable, but if that’s not available to us then we have to do some hardcore adjusting of the array we use (ImageData() in this tutorial).  This requires CopyMemory and a For loop – both of which are time killers, which is contrary to the whole point of this tutorial.

So in the interest of space and brevity, I’m not going to go through the intricacies of this method in this tutorial.  Instead, you can download a modified brightness program that demonstrates how to do this.  Robert Rayment – a profilic VB programmer in his own right – put it together, and all credit for the modifications go to him.  Please shower him with praise.

An easier fix – and my preferred method – is to use a 2-dimensional array to receive the image data instead of a 3-dimensional one, like so:

ArrayWidth = (bm.bmWidth * 3) - 1
ArrayWidth = ArrayWidth + (bm.bmWidth Mod 4)
ArrayHeight = bm.bmHeight
ReDim ImageData(0 To ArrayWidth, 0 To ArrayHeight) As Byte

This method works well, but accessing individual pixels is slightly more cumbersome:

  • Red is in location (x * 3 + 2, y)
  • Green is in location (x * 3 + 1, y)
  • Blue is in location (x * 3, y)

Using a temp variable to store the x * 3 value actually makes this faster than the 3d array used in the tutorials.  As such, this is my favorite method. It is also the method I use in PhotoDemon.

Streams, mentioned in the next tutorial, also need to be 4-byte aligned.

CONTINUE TO “TANNER’S TOP 10 LIST OF GRAPHICS OPTIMIZATIONS”

 

This site - and its many free downloads - are 100% funded by donations. Please consider a small contribution to fund server costs and to help me support my family. Even $1.00 helps. Thank you!

VB Graphics Programming: Part 2 (Beginning API)

Basic API Pixel Routines
Next, let’s discuss the basics of per-pixel graphics programming using the simple API routines of GetPixel and SetPixel/SetPixelV.  If you haven’t already, I recommend reading the previous page, “Pure VB Pixel Routines,” as it provides the foundation for the advanced graphics principles discussed in this and the next two sections.

Assuming that you now understand how to use built-in Visual Basic functions to get per-pixel data, it is time to extend that information a little bit further to include the Windows API.  For those who don’t know, the Windows API is a collection of dynamically linked libraries (so-called ‘DLL files’) that contain commonly used programming routines.  These routines, or “interfaces” (API = Application Programming Interface) range from I/O to networking to multimedia, and they generally work much faster than the intrinsic Visual Basic routines.  The API that we are going to be looking at is called “GDI32,” which stands for Graphic Device Interface: 32-bit version.  (This dll is included with every Windows OS since Win95, so no matter what OS you are using with VB 5/6 this method will work.)

GDI32 is a collection of – surprise, surprise – routines commonly used in graphic and image manipulation.  This includes brushes for drawing, bit-block transfers (BitBlt) for painting image sections, and what we’re most interested in for this tutorial: several important ways to get and set pixel data.  We will start by looking at three functions in particular: GetPixel, SetPixel, and SetPixelV.

I – Declaring the Necessary API Functions

Because the GDI32 functions aren’t an integral part of the Visual Basic programming language, we have to declare them in the General Declarations section just as we would a variable or a type.  The syntax each of our API function declarations is as follows:

Private Declare Function GetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long) As Long

Private Declare Function SetPixel Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Long

Private Declare Function SetPixelV Lib "GDI32" (ByVal hDC As Long, ByVal x As Long, ByVal y As Long, ByVal crColor As Long) As Byte

For those unfamiliar with API declarations, these might look imposing at first – but don’t worry.  They’re all pretty darn straightforward.

  • Private simply means that we only want to use these functions within the current code block. If we were to use Public instead of Private, we could use these function calls in any module, class module, or form in the current project.
  • Declare Function FunctionName simply lets VB know that we plan on using a function titled GetPixel or SetPixel.
  • Lib "GDI32" is short for Library “C:WindowsSystemGDI32.dll”‘ – it tells Visual Basic which dynamically-linked library (DLL) contains the code for the ‘FunctionName’ we just declared.

The values inside of the parentheses are nothing more than variable declarations, just as you would see for a standard sub or function.

  • hDC stands for “Handle Device Context.” This is the computer’s way of saying ‘address of an object.’ This variable tells us what picture box we want to draw on (like PictureBox1.hDC)
  • X and Y are the location – in pixels – of the pixel we want to work with. API calls always work in pixels, so you should too.  If your picture boxes use twips, inches, or any measurement other than pixels, these calls won’t work.
  • crColor in SetPixel and SetPixelV is the color that we want to set pixel (x,y) to.  This is identical to the ‘Color’ part of the PSet call.  (I’m not entirely sure why this is typically declared as “crColor”; you could call it anything you wanted, but convention seems to stick to “crColor.” Go figure…)

Looks familiar, eh? You should be able to recognize some major similarities between these calls and Point/PSet. If you don’t, you may want to reread the previous tutorial page.

II – Using Our New Friend, GetPixel

Now that we’ve told Visual Basic everything it needs to know about our “GDI32” functions, we can use them anywhere and everywhere we want to!  Yay!

Let’s demonstrate.  If you wanted to get a color from pixel (35, 42) of picture box “Picture1”, you would use the following:

Dim Color as Long
Color = GetPixel(Picture1.hDC, 35, 42)

I hope this looks painfully easy, because that’s exactly what it is.

Now that you’ve gotten your color into a variable of type Long, you still have to extract the individual red, green, and blue components just like before. I’m not going to repeat all that stuff, so just cut and paste those functions out of the last tutorial if you need them.

III – Setting Pixels Using SetPixel and SetPixelV

Again, setting a pixel’s color is almost identical to getting its color:

Dim APIReturnValue as Long
APIReturnValue = SetPixel(PictureBox.hDC, x, y, Color)

OR

SetPixel PictureBox.hDC, x, y, Color

OR

Dim APIReturnValue as Byte
APIReturnValue = SetPixelV(PictureBox.hDC, x, y, Color)

OR

SetPixelV PictureBox.hDC, x, y, Color

Ahhh! Four different ways to do exactly the same thing! Seems a little weird, doesn’t it?  Let me explain why this is.

Every function returns a value of some type.  If you’ll scroll back up and look at the SetPixel and SetPixelV declarations, you’ll notice a slight difference between the two: SetPixel is of type Long and SetPixelV is of type Byte. This is because SetPixel returns the color that it was able to set (for example, if you tried to set RGB(0, 0, 1) in 8- or 16-bit color mode, SetPixel would likely only be able to set RGB(0, 0, 0) instead), while SetPixelV only returns whether or not the pixel was set (1 or 0).  Because SetPixelV only has to return a boolean value instead of a Long (though they’re stored identically in memory), I prefer to use it.  However, SetPixel could be useful in color modes other than 24-bit, because you could determine the difference between the color you wanted to set and the color that actually got set, as mentioned above.

This is the difference between SetPixel and SetPixelV.  The reason for the other two declarations is whether or not we care what value SetPixel/SetPixelV returns.  If we don’t care, it is easier to just use the second form of the call – the one without the extra variable declaration.  If, however, we want to know what value they return, we need to use the first form.

Now you know four different ways to set pixels using the API!  Use that to impress your friends… :)

Let’s quickly demonstrate a specific example using SetPixel/SetPixelV. Using our example from the last tutorial, let’s say that you want to set pixel (35,42) of the picture box titled “Picture1” to the value of variable ‘Color:’

SetPixelV Picture1.hDC, 35, 42, Color

Again we note that Color is of type Long – you could use the RGB() function just as you did in part 1.  In that case, you could write:

SetPixelV Picture1.hDC, 35, 42, RGB(255, 0, 0)

To set pixel (35, 42) of “Picture1” to pure red.  See how easy the API calls are to use?  Once declared, they are no harder than than PSet and Point – and, as you’re about to see, they’re significantly faster.

IV – Using GetPixel and SetPixel to Edit an Image

This .zip file contains a program identical to the last one, except that this one utilizes the API for its pixel interaction.  Compare the results of this program to the PSet/Point one – notice a pretty significant difference?  As a good programming exercise, build a small function to time how long each method takes and compare the results.  The API calls can be anywhere from 3-10x faster than PSet and Point – a pretty noticeable difference for hardly any extra work.

V – In Conclusion: The Need for Speed – GetPixel/SetPixel vs. Point/PSet and Beyond…

I hope that you’re beginning to see the advantages of well-used API calls within your Visual Basic programs. If you can master using the Windows API, your VB capabilities are endless.  GetPixel/SetPixel are just the tip of the iceberg, too – the next tutorial will show you a method 10-50x faster than this one!  Get excited!!

But before we head into the next tutorial, let’s think of ways that GetPixel and SetPixel/SetPixelV could be improved.

For one, we still have to manually extract the red, green, and blue values out of a Long-type variable – this is not only annoying, but it’s slow as well.  Things would go faster if we could somehow get Windows to separate the Long variable for us.

Also, there is this problem of having to use GetPixel/SetPixel for every single pixel.  Any way you slice it, running two functions for each of our 120,000 pixels is a pretty slow proposition.  What if there was a way to get the data for every single pixel at once – that way, we’d only have to use a single API call to get ALL of our pixel data.  Now that would be fast!

Well believe-it-or-not, our next tutorial will explain how to do just that – get Windows to give us all of an image’s pixel data at once, nicely parsed into its red, green, and blue components.  These are commonly referred to as DIB sections, the most powerful API graphics call you can use from within VB.

CONTINUE TO “ADVANCED API PIXEL ROUTINES”

 

This site - and its many free downloads - are 100% funded by donations. Please consider a small contribution to fund server costs and to help me support my family. Even $1.00 helps. Thank you!

VB Graphics Programming: Part 1 (Pure VB)

Pure VB Pixel Routines

First, let’s discuss the basics of per-pixel graphics programming using only built-in Visual Basic functions.  I recommend that even hardened VB veterans glance through this document, as it provides the foundation for the advanced graphics principles discussed in the next three tutorials.  We will discuss the only VB per-pixel graphics functions (Point and PSet), and after this is done you may know a lot more than you ever wanted to about VB graphics…  :) *hehe*

Most of you have probably heard of the “horrible twins of PSet and Point”*. These two routines are usually the first per-pixel methods introduced to VB programmers (since they come included as part of the language), but – perhaps inevitably – they are also the slowest way to do things.  Avoid ever using these routines for per-pixel image processing (though they do have some uses in non-pixel-sized work). I include them here only for completeness; I would never recommend using them in an actual image processing program because they are so extremely slow.

Why are they so slow? We’ll discuss that later, after we’ve looked at their syntax.

I – Getting the Color of an Individual Pixel

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

Dim Color as Long
Color = PictureBox.Point(x,y)

PictureBox is the name of the picture box or form you want to retrieve the pixel from, and (x,y) are the pixel’s coordinates.

For example, if you wanted to get a color from spot (35, 42) of picture box “Picture1”, you would use the following:

Color = Picture1.Point(35,42)

It doesn’t get much simpler than that, folks.

So now the question is “what do I do once I’ve gotten the color?” After all, it comes in Long type, which is some strange number from -2 billion to +2 billion…and that really doesn’t lend itself to easily adjusting the color of that pixel.

Thus the challenge is figuring out how to change one 4-byte number into three 1-byte numbers (red, green, and blue).

While this may sound easy, the theory behind doing this requires some knowledge of binary encoding…which is far too large a topic to be covered here. Luckily for you, here are three functions that will automatically do RGB color extraction for you:

Public Function ExtractR(ByVal CurrentColor As Long) As Byte
   ExtractR = CurrentColor And 255
End Function

Public Function ExtractG(ByVal CurrentColor As Long) As Byte
   ExtractG = (CurrentColor \ 256) And 255
End Function

Public Function ExtractB(ByVal CurrentColor As Long) As Byte
   ExtractB = (CurrentColor \ 65536) And 255
End Function

To utilize these functions, use the following syntax:

Dim R as Byte, G as Byte, B as Byte

Dim Color as Long

Color = PictureBox.Point(0, 0)

R = ExtractR(Color)
G = ExtractG(Color)
B = ExtractB(Color)

Pretty neat, eh?

This is the first method we’ll use for getting a pixel’s data and breaking it down into its red, green, and blue components.  Now let’s quickly mention how to set this data back into a picture box.

II – Setting the Color of an Individual Pixel

Setting a pixel’s color is almost identical to getting its color. You use the VB event “PSet,” which stands for “Pixel Set.”

PictureBox.PSet (x,y), Color

Again, PictureBox is the name of the picture box or form you want to set the pixel to and (x,y) are the pixel’s coordinates. The only difference here is that we also include the color that we want to set. So, using the example above, if you wanted to set a color to pixel (35, 42) of picture box “Picture1” you would use the following:

Picture1.PSet (35,42), Color

It is worth noting that Color is of type Long, which creates the same problem we discussed above – how to change three separate red/green/blue values into a single 4-byte number. Fortunately, VB has a built-in command called RGB() that does this conversion for us. To illustrate it’s use, let’s use the same example saying that you want to change the color of the pixel at (35,42) to pure red:

Picture1.PSet (35,42), RGB(255, 0, 0)

The first RGB parameter is red, then green, last blue, so RGB(255,0,0) will set the color to pure red.  Easy!

III – Using Point and PSet to Edit an Image

First, a little disclaimer: entire books have been written on the theories behind GP and there are entire programming disciplines whose job is nothing but optimizing graphics routines.  So, while what I’m about to show you in code is a nice method, be advised that GP is an extremely complicated field and to truly succeed in it you must be willing to do a little research.  I have chosen a well-optimized and very standard method for my sample programs because it is easy to understand while still offering good results.  But, for this first tutorial, don’t be disappointed if the results aren’t particularly incredible or lightning-fast.  That’s what the next three tutorials are for!

The code in this .zip file will demonstrate how to change the brightness of an image using a standard linear brightness algorithm. The code is simple and well-commented. Read through the comments and make sure that you understand how everything works.

IV – Why are Point and PSet So Slow?

If you’ve tried out the sample code, you’re probably not impressed…and rightfully so!  Point and PSet – though easy to use – are extremely slow. Can you imagine trying to work with images 4 or 5 times the size of the demo one? Not so cool.

So why are these two functions SO slow?  To illustrate it, let’s follow the path your computer takes for changing the color of a single pixel using Point and PSet as you just saw done in the sample program. (Author’s Note: I base these conclusions on general programming knowledge, not known facts; so while I’m pretty sure that this explanation is accurate, I could be wrong on some of the details. I invite and encourage input on making this section 100% accurate.)

  1. Upon encountering a Point command, VB’s first task is to do a whole crapload of error checking. This involves things like making sure that the pixel you want is within the image’s boundaries, making sure that the PictureBox exists, seeing if an image has been loaded or if you’re working with a blank image, etc. This step is speed killer #1 – but the advantage is that Point will never crash your machine, thankfully.
  2. Once VB has decided that there is actually a pixel at point (x,y), it now has to figure out where to get the pixel color from.  This changes depending on both the status of AutoRedraw and whether or not you’ve updated the image since loading it. VB will usually go to the ‘Image’ property, but in certain cases AutoRedraw may tell it to go to the ‘Picture’ property.  (If that doesn’t make any sense, forget about it!)  This step is speed killer #2 – but again, you never crash your machine and you always get predictable results.
  3. After VB knows where the pixel data resides, it can now go and get the pixel information. This step is all handled in memory, so this is really fast.
  4. After VB gets the color of this pixel, it must transfer that information into the variable specified by the original Point command.  This is all but instantaneous – no speed problems here.
  5. Once a variable of type Long contains the color of the pixel, we must parse that long into its red, green, and blue components. This step is speed killer #3, because we gotta do three ‘Ands’ and two ‘Divides’ for every single pixel. For an image like the sample one, that’s 400×300 or 120,000 pixels… meaning there’s a grand total of 360,000 ‘Ands’ and 240,000 ‘Divides.’ This step is a very, very bad one for speed – the ‘Ands’ are fast, but the ‘Divides’ are extremely slow (times 240,000).
  6. Once we have a red, green, and blue component, we change these values to the new values specified by the look-up table. This step is very fast because, again, it’s nothing more than simple memory transfers.
  7. Next comes the PSet step.  This process is almost identical to Point, so I’m going to abbreviate its steps.  First, it does the error checking.  Speed killer #4 here.
  8. VB will automatically assign the new color to its appropriate location within the ‘Image’ property.  This is fast – again, it’s all done in memory.
  9. Now VB has to decide whether or not to refresh the image.  If AutoRedraw is set to false, VB will attempt to redraw the entire image after each pixel has been set.  Do not do this – EVER – while working with per-pixel programming routines.  If AutoRedraw is set to true, VB will only redraw the entire image after you explicitly tell it to or after you finish the loop containing the PSet calls.  Redrawing the image is very slow because your computer has to copy the information for thousands of pixels from the Picture or Image property to wherever the screen data is located (either VRAM or RAM – this is yet one more thing your computer has to figure it; it too takes time). Although your RAM is one of the faster parts of your computer, it will be slowed down by lots of huge memory chunk transfers (like graphics).  This is speed killer #5.

Steps 1, 2, 5, 7, and 9 are what’s slowing down your PSet/Point-based graphics program. Visual Basic is very nice in that it does almost all of your error checking for you, but there is a definite speed trade-off.  In other programming languages and per-pixel routines, many of these error checking steps are removed – which is one of the reasons why other languages and functions are generally faster but more dangerous to use. In the next three tutorials we will discuss alternate methods of doing graphics that cut out some of these “speed killer” steps.

V – Conclusion

Hope that all made sense to you!  Does it feel good knowing you can now program any graphics routine using nothing but VB?  Hope it does – but don’t get too comfortable yet.

To be totally honest, I hope that you completely forget that PSet and Point even exist – at least as far as per-pixel image processing is concerned – after reading the next three tutorials.  Both are extremely slow and… well, just bad programming for image processing.  VB6 is good for a lot of things, but its pixel interfacing is a total joke.

Thankfully, there are three more tutorials that will show you better, faster ways to do graphics programming – but at least you now know how to use PSet and Point if the need ever arises.

CONTINUE TO “BASIC API PIXEL ROUTINES”

*As quoted by Matt Hart, the legendary VB programmer

 

This site - and its many free downloads - are 100% funded by donations. Please consider a small contribution to fund server costs and to help me support my family. Even $1.00 helps. Thank you!