Click here to Skip to main content
15,861,125 members
Articles / Programming Languages / Visual Basic

Using BitBlt to Copy and Paste Graphics

Rate me:
Please Sign up or sign in to vote.
4.22/5 (20 votes)
5 May 2010CPOL4 min read 227.3K   5.1K   49   23
This article will demonstrate how to use BitBlt. and provide some technical pointers
Sample Image - Bitblt_wrapper_class.jpg

Introduction

As most of you already know, the .NET environment offers us to Frameworks to do all kinds of cool stuff, including the Drawing classes and objects. BUT?

When we want to use the graphics on a high frame rate, the DrawImage method just won't do, it's too slow (I am not sure why but it is :] ). So as very well introduced by Matthew Hazlett, the BitBlt comes to our aid by doing stuff VERY fast. (His article which is very informative and was the foundation for this article and code is located here.)

What I will try to do here is to take it just one step further, give out some guidelines and pointers. Plus - this sample code will provide a very clean and well commented class you can use to learn or even implement in your projects.

In the demo project, I have created a class which copies to memory, and from memory using BitBlt, it's well documented and easy to understand.

A Quick Run Through The Basics

Just to get things stated, in order to use the BitBlt function we have to declare it, as it is not a part of the Frameworks, but a part of the Window's API. The function is located at the "GDI32.dll" library, and that's about it. The declaration goes like this:

VB.NET
Private Declare Auto Function BitBlt Lib "GDI32.DLL" _
    ( ByVal hdcDest As IntPtr, ByVal nXDest As Integer, _
    ByVal nYDest As Integer, ByVal nWidth As Integer, _
    ByVal nHeight As Integer, ByVal hdcSrc As IntPtr, _
    ByVal nXSrc As Integer, ByVal nYSrc As Integer, _
    ByVal dwRop As Int32) As Boolean

Now what we do is to copy Bitmaps from one location in the memory to another, these locations are called a Device Context, as they are directly connected to a Graphical Device on the system, for instance - a Picture Box or a Form.

What are all the other parameters? There are the X, Y, Width, Height which tell BitBlt what is the area we wish to copy, and X and Y parameters to Where to copy at the target Device Context (which is used as our canvas).

Creating a Device Context is Not That Complicated

So for us to be able to use BitBlt, we need 2 Device Contexts (hard to pronounce isn't it?) To create those, we first need a Graphics object, we create that one from (almost) any object we want, for example, we can use a picture box:

VB.NET
 PictureBox1.CreateGraphics

' But we need to keep a reference to it, so we will use this:
Dim GrpObj as Graphics = PictureBox1.CreateGraphics

Now we have GrpObj as a Graphics object, which we can use to create the Device Context:

VB.NET
Dim MyDeviceContext as IntPtr ' Creating the
  ' variable to keep the DC address
MyDeviceContext = GrpObj.GetHdc()

(Note: Hdc = Handle of Device Context)

Now what did we just do? We created an Object in Memory which represents the Graphical Object GrpObj. Now we can use the Device Context to BitBlt.

RULE NUMBER 1

While we have a Device Context in use, the Parent Object (in this case GrpObj) cannot be accessed directly, an attempts to draw to it (like using the .DrawArc method) will result in an exception! We DO NOT want that.

So - right after creating and using a Device Context, we make sure to Release it:

VB.NET
GrpObj.ReleaseHdc (MyDeviceContext)

Note that the Parent of GrpObj is also locked until the Device Context is released. AND we have to dispose of it too, to keep our memory leaks to the minimum. ;)

VB.NET
GrpObj.Dispose()

Now we need another Device Context to copy to. To do so, we will create an invisible Device Context in the memory very easily.

VB.NET
 ' We Create a Bitmap object at the desired size,
Dim srcBmp As New Bitmap(Size.Width, Size.Height, GrpObj)

 ' Creating Graphics Objects
MemoryGrpObj = Graphics.FromImage(srcBmp) 'Create a Graphics object

 ' Creating a Device Context
MemoryHdc = MemoryGrpObj.GetHdc 'Get the Device Context

BitBliting the Images

Now we can copy anything we want to or from this Device Context. Providing

RULE NUMBER 2

After Blitting to memory, we have to keep the Device Context in scope, because when we Blit to memory, the changes will reset after we release the device context. THUS- we either keep the MemoryHDC as a Global variable or use it in the same procedure.

Still we mustn't forget to release the Device Context so we also have to make sure to keep the Graphics object MemotyGrpObj reference as well, ok?

Now we can Actually Blit

VB.NET
BitBlt(MemoryHDC, 0, 0, width, Height, MyDeviceContext, 0, 0, SRCCOPY)
^ Target ^                        ^ Source ^

This will copy From MyDeviceContext To MemoryHdc.

A quick side note: The 'SRCCOPY' is one of the many raster operations BitBlt can do for us, this (the most commonly used) operation copies an exact copy of the bitmap, I have declared SRCCOPY as a Const, this is the value.

VB.NET
Dim SRCCOPY as Integer = &HCC0020 ' Hex
    or
Dim SRCCOPY as Integer = 13369376 ' Dec

We can also use BitBlt to create masks, transparency and some more cool stuff (FYI).

All we need to do now is to create our target Device Context, and BitBlt to it from the memory, we can keep an array of Device Contexts, just remember to follow the rules!

To finish up, note that if you want to draw on a specific location, here is the syntax:

VB.NET
BitBlt(TargetHdc, Xpos, Ypos, width, Height, MemoryHDC, 0, 0, SRCCOPY)
            ^                                ^
'(Assuming the TargetHdc is our target Device Context,
'and Xpos and Ypos are the location to draw at)

The sample project will demonstrate it all and maybe save you the trouble. Good luck.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Chief Technology Officer ISQ
Israel Israel
Just another coder out there Smile | :)
Currently working on a natural language search index with nanoRep

Comments and Discussions

 
Generalnice Pin
Dilatazu10-Dec-07 1:30
Dilatazu10-Dec-07 1:30 
GeneralProblem in BitBlt Pin
jeykumars22-Aug-06 0:59
jeykumars22-Aug-06 0:59 
GeneralBliting From Invisible Pictureboxes Pin
Dark-Cube216-Mar-05 11:55
Dark-Cube216-Mar-05 11:55 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Fade (Amit BS)17-Mar-05 2:03
Fade (Amit BS)17-Mar-05 2:03 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Dark-Cube217-Mar-05 16:09
Dark-Cube217-Mar-05 16:09 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Mathew Hall22-Mar-05 4:06
Mathew Hall22-Mar-05 4:06 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Fade (Amit BS)23-Mar-05 1:47
Fade (Amit BS)23-Mar-05 1:47 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Dark-Cube223-Mar-05 15:00
Dark-Cube223-Mar-05 15:00 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Fade (Amit BS)9-May-05 16:18
Fade (Amit BS)9-May-05 16:18 
GeneralRe: Bliting From Invisible Pictureboxes Pin
TwoDigital28-Jul-05 10:20
TwoDigital28-Jul-05 10:20 
GeneralRe: Bliting From Invisible Pictureboxes Pin
themuppeteer9-May-05 7:47
themuppeteer9-May-05 7:47 
GeneralRe: Bliting From Invisible Pictureboxes Pin
Not_Possible15-Dec-06 18:04
Not_Possible15-Dec-06 18:04 
GeneralRe: Bliting From Invisible Pictureboxes Pin
pinturic18-Dec-06 4:45
pinturic18-Dec-06 4:45 
GeneralRe: Bliting From Invisible Pictureboxes Pin
pinturic18-Dec-06 3:47
pinturic18-Dec-06 3:47 
GeneralRe: Bliting From Invisible Pictureboxes Pin
hunterz852-May-09 2:05
hunterz852-May-09 2:05 
GeneralPicture doesn't refresh... Pin
mdProgrammer221-Sep-04 3:21
mdProgrammer221-Sep-04 3:21 
GeneralRe: Picture doesn't refresh... Pin
Fade (Amit BS)22-Sep-04 4:26
Fade (Amit BS)22-Sep-04 4:26 
GeneralnOOb question Pin
andreaskoller31-Aug-04 23:56
andreaskoller31-Aug-04 23:56 
GeneralRe: nOOb question Pin
Fade (Amit BS)2-Sep-04 11:49
Fade (Amit BS)2-Sep-04 11:49 
GeneralWhy BitBlt can't work on win 98 & ME Pin
Member 123250014-Jul-04 15:48
Member 123250014-Jul-04 15:48 
GeneralRe: Why BitBlt can't work on win 98 & ME Pin
Fade (Amit BS)18-Jul-04 7:50
Fade (Amit BS)18-Jul-04 7:50 
GeneralRe: Why BitBlt can't work on win 98 & ME Pin
themuppeteer9-May-05 5:23
themuppeteer9-May-05 5:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.