BitBlt

This article will introduce you to BitBlt api function. This function provides a very fast image bit transfer. You see, in Windows, all the drawing that appears on the screen is actually drawing on this thing called Device Context (DC). Not going too deep into the Device Contexts and what they are, just remember that there is probably no better way of transferring bits from one picture box to another picture box than using BitBlt. You might wonder, why do I want to use API for that instead of just seting Picture property. Well, you can do that, but what if you want to take just, let's say, 10x10 piece out of 30x30 picture and put that piece in the other picture box. Can you do that with Picture property? I don't think so.

Anyways, here is the declaration for BitBlt that you put in the module (as Public) or in the form (as Private):


Public Declare Function BitBlt Lib "gdi32" (ByVal hDestDC As Long, _
ByVal X As Long, _
ByVal Y As Long, _
ByVal nWidth As Long, _
ByVal nHeight As Long, _
ByVal hSrcDC As Long, _
ByVal xSrc As Long, _
ByVal ySrc As Long, _
ByVal dwRop As Long) As Long


To describe this function shortly, it takes a handle to the destincation DC, the coordinates of the destination area and the length and the width of the area that the image bits will be transferred to. Then it takes a handle to a source DC and the starting coordinates of the wanted bits. The last parameter specifies the type of transfer that you desire. Here are some options that you have for dwRop:

Public Const SRCAND = &H8800C6
Public Const SRCCOPY = &HCC0020
Public Const SRCERASE = &H440328
Public Const SRCINVERT = &H660046
Public Const SRCPAINT = &HEE0086


How does dwRop affect the transfer? It affects the resulting image. Let's say if you set dwRop to SRCCOPY, then the destination image will look the same as the source. If you pick SRCAND, the current destination image bits will be logically ANDed with the source image bits and the resulting bits written to the destination. If you pick SRCERASE, then the current destination bits will be inversed (logically NOTed) and the result will be ANDed with the source and then the final result is written to the destination image. SRCPAINT option ORs destination and the source to get the final bits. SRCINVERT XORs destination and the source to get the final result. These 5 options is not all that you have. These constants are easily accessible for C/C++ programmers and are defined in wingdi.h file that comes with C++. In that file these constants are defined and can be used to define VB constants. Click here to see the page that shows C++ defines and VB declarations for dwRop constants.

That is that, now, let's move on to the example. Let's say you have this image:


The idea that i have is that I want to display any of the text anywhere on the form or some picture box without actually having 4 different images stored. BitBlt is perfect for tha scenerio. If you create a Picture box, set it's Picture property to the image shown above and Visible to False, then you can use that picture boxe's hDC property to get any of the image parts.
I will make the picture part go to some area of the form. If you have called your invisible picture box by the name picSource then to transfer "cool stuff" to the form, here is how you can do it:

BitBlt Me.hDC, 100, 100, 100, 30, picTest.hDC, 0, 110, SRCCOPY

The coordinates are specified by you and depends on your image. One thing to note that is very important: the destination control must have it's AutoRedraw prorty set to True, otherwise nothing will happen.

BitBlt can be very fun and should be taken advantage of in your applications.


Written By Laimonas Simutis. 2002. mailto://laijerrad@yahoo.com
Disclaimer of Warranty - Copyright Notice