5,401,186 members and growing! (18,646 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

Using BitBlt to Copy and Paste graphics

By Fade (Amit BS)

This article will demonstrate how to use BitBlt. and provide some technical pointers
VBWindows, .NET, .NET 1.0, Win2K, WinXP, Win2003VS.NET2002, Visual Studio, Dev

Posted: 15 Apr 2004
Updated: 15 Apr 2004
Views: 72,851
Bookmarked: 17 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
15 votes for this Article.
Popularity: 4.51 Rating: 3.83 out of 5
1 vote, 6.7%
1
1 vote, 6.7%
2
3 votes, 20.0%
3
1 vote, 6.7%
4
9 votes, 60.0%
5

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 wont 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:

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 location 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 tells BitBlt what is the area we wish to copy, and a 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:

 PictureBox1.CreateGraphics

' But we need to keep a refference 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:

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
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 minimum ;)

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.

 ' 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 musn't forget to release the Device Context so we also have to make sure to keep the Graphics object MemotyGrpObj refference as well, ok?

Now we can Actualy Blit:

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-

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:

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Fade (Amit BS)


Just another coder out there, looking for the truth, and for THE answer(the universe, life, and everything)
Currently leading the development of a Digital Signage masterpiece Called TVM2 at ActiveRoom (www.ActiveRoom.Net)
Occupation: Web Developer
Location: Israel Israel

Other popular GDI+ articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 22 of 22 (Total in Forum: 22) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralnicememberDilatazu2:30 10 Dec '07  
GeneralProblem in BitBltmemberjeykumars1:59 22 Aug '06  
GeneralBliting From Invisible PictureboxesmemberDark-Cube212:55 16 Mar '05  
GeneralRe: Bliting From Invisible PictureboxesmemberFade (Amit BS)3:03 17 Mar '05  
GeneralRe: Bliting From Invisible PictureboxesmemberDark-Cube217:09 17 Mar '05  
GeneralRe: Bliting From Invisible PictureboxesmemberMathew Hall5:06 22 Mar '05  
GeneralRe: Bliting From Invisible PictureboxesmemberFade (Amit BS)2:47 23 Mar '05  
GeneralRe: Bliting From Invisible PictureboxesmemberDark-Cube216:00 23 Mar '05  
GeneralRe: Bliting From Invisible PictureboxesmemberFade (Amit BS)17:18 9 May '05  
GeneralRe: Bliting From Invisible PictureboxesmemberTwoDigital11:20 28 Jul '05  
GeneralRe: Bliting From Invisible Pictureboxesmemberthemuppeteer8:47 9 May '05  
GeneralRe: Bliting From Invisible PictureboxesmemberNot_Possible19:04 15 Dec '06  
GeneralRe: Bliting From Invisible Pictureboxesmemberpinturic5:45 18 Dec '06  
GeneralRe: Bliting From Invisible Pictureboxesmemberpinturic4:47 18 Dec '06  
GeneralPicture doesn't refresh...membermdProgrammer4:21 21 Sep '04  
GeneralRe: Picture doesn't refresh...memberFade (Amit BS)5:26 22 Sep '04  
GeneralnOOb questionmemberandreaskoller0:56 1 Sep '04  
GeneralRe: nOOb questionmemberFade (Amit BS)12:49 2 Sep '04  
GeneralWhy BitBlt can't work on win 98 & MEmemberfong0116:48 14 Jul '04  
GeneralRe: Why BitBlt can't work on win 98 & MEmemberFade (Amit BS)8:50 18 Jul '04  
GeneralRe: Why BitBlt can't work on win 98 & MEmemberthemuppeteer6:23 9 May '05  
GeneralStill faster than DrawImage?memberRobinLetMeLogin7:37 28 Mar '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Apr 2004
Editor: Nishant Sivakumar
Copyright 2004 by Fade (Amit BS)
Everything else Copyright © CodeProject, 1999-2008
Web19 | Advertise on the Code Project