Click here to Skip to main content
15,119,637 members
Articles / Programming Languages / Visual Basic
Posted 29 Dec 2003


26 bookmarked

Copying graphics with BitBlt (.NET Style)

Rate me:
Please Sign up or sign in to vote.
4.50/5 (15 votes)
29 Dec 2003
When Graphics.Clone() just won't do.


When the basic GDI+ functions just won't do, what you want is back to API school. BitBlt quite simply makes copies of portions of the screen. This is done by accessing the Windows hDC and other low level mind numbing things.

First we most Declare the functions that are in the GDI32.DLL file so we can use them in VB.NET.

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

Then I have created a special function that will copy the area of the screen specified by rectangleF.

Private Function copyRect(ByVal src As PictureBox, _ 
       ByVal rect As RectangleF) As Bitmap
     'Get a Graphics Object from the form
     Dim srcPic As Graphics = src.CreateGraphics
     'Create a EMPTY bitmap from that graphics
     Dim srcBmp As New Bitmap(src.Width, src.Height, srcPic)
     'Create a Graphics object in memory from that bitmap
     Dim srcMem As Graphics = Graphics.FromImage(srcBmp)

     'get the IntPtr's of the graphics
     Dim HDC1 As IntPtr = srcPic.GetHdc
     'get the IntPtr's of the graphics
     Dim HDC2 As IntPtr = srcMem.GetHdc

     'get the picture 
     BitBlt(HDC2, 0, 0, rect.Width, _ 
       rect.Height, HDC1, rect.X, rect.Y, 13369376)

     'Clone the bitmap so we can dispose this one 
     copyRect = srcBmp.Clone()

     'Clean Up 
End Function

Then all we need to do is call the function to return the image you want:

Dim bmp = CType(copyRect(src, _ 
    New RectangleF(0, 0, 50, src.Height)), Bitmap)    
dest.Image = bmp.CloneShorthand:


dest.Image = CType(copyRect(src, _ 
   New RectangleF(0, 0, 50, src.Height)), Bitmap).Clone

Either statement will clone the src image at (0,0,50, src.height) and return a bitmap object. I have simply then taken the bitmap object and cloned it into a picturebox so you can see the results.


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

Matthew Hazlett
Web Developer
United States United States
I started programming for fun when I was about 10 on an Franklin Ace 1000.

I still do it just for fun but it has gotten me a few jobs over the years. More then I can say for my Microsoft Certifications. Smile | :)

The way I learned was by example, now its time to give back to the next generation of coders.

Comments and Discussions

GeneralClon BitBlt Vb to .Net (Sub BitBlt2) Pin
B-NetAje15-Aug-08 15:12
MemberB-NetAje15-Aug-08 15:12 
Questioncoloring images in picture box Pin
surgunsum13-Nov-07 1:16
Membersurgunsum13-Nov-07 1:16 
GeneralPrint Screen Pin
AliBong20-Aug-06 1:06
MemberAliBong20-Aug-06 1:06 
GeneralConverting the Code Above for Layered Windows and Cmd Line Arguments [modified] Pin
Meskibob20-Jul-06 20:01
MemberMeskibob20-Jul-06 20:01 
Generalcode doesn't work with my version of Pin
serendipity 223-May-06 15:10
Memberserendipity 223-May-06 15:10 
GeneralMore memory problems with this example Pin
Mike.Anderson20-Dec-05 8:47
MemberMike.Anderson20-Dec-05 8:47 
GeneralRe: More memory problems with this example Pin
Matthew Hazlett20-Dec-05 9:53
MemberMatthew Hazlett20-Dec-05 9:53 
GeneralGood work, thanks! Pin
Pinhead7512-May-05 21:59
MemberPinhead7512-May-05 21:59 
Generalcopy invisible images Pin
thecopymaster29-Sep-04 5:20
Memberthecopymaster29-Sep-04 5:20 
GeneralRe: copy invisible images Pin
bskirkman26-Jan-06 13:37
Memberbskirkman26-Jan-06 13:37 
GeneralRe: copy invisible images Pin
di~v~inci11-Sep-08 6:03
Memberdi~v~inci11-Sep-08 6:03 
GeneralRe: copy invisible images Pin
Wan--Vevi20-Oct-08 18:22
MemberWan--Vevi20-Oct-08 18:22 
GeneralImages Pin
Fade (Amit BS)18-Apr-04 14:22
MemberFade (Amit BS)18-Apr-04 14:22 
GeneralRe: Images Pin
serendipity 223-May-06 15:14
Memberserendipity 223-May-06 15:14 
AnswerRe: Images- off screen Pin
Fade (Amit BS)9-May-06 23:33
MemberFade (Amit BS)9-May-06 23:33 
Yes and No, you can do Load images off screen and then bitblt from memory, but not from a picture box (and who wants too when they don't have to? Wink | ;) )

what you need to do is create a bitmap in memory and get a dc for it - i'll show you:

<br />
' Creating Image object to hold the image in memory<br />
            TextImage = New Bitmap(yourWidth,yourHeight)' <br />
            Dim ImageGraphics As Graphics = Graphics.FromImage(TextImage) ' creating a graphics object to manipulate the image<br />
            ImageGraphics.drawImage(...) ' load your image here<br />
            ' dispose of graphics object<br />
            ImageGraphics.Dispose()<br />
<br />
            ' (Note : objSurfaceGraphics is a graphics object from the form or other object you want to draw on)<br />
            objSurfaceDC = objSurfaceGraphics.GetHdc() ' creating a DC for the target surface, using the dc you want to paste on (it is also possible to use desktop's dc)<br />
            ' create a compatible dc for the surface - we will blit from there            Dim MemDC As IntPtr = CreateCompatibleDC(objSurfaceDC)<br />
            Dim OldHBitmap As IntPtr ' to hold the reference<br />
            Dim HBitmap As IntPtr = TextImage.GetHbitmap ' get HBitmap<br />
            OldHBitmap = SelectObject(MemDC, HBitmap) ' and assign to our newly created MemDC - note that SelectObject returns the previous used DC, we need to keep it for later dispose or we'll have a memory leak'*** do blitting here<br />
<br />
            ' now we dispose - to properly dispose of all do this:<br />
            SelectObject(MemDC, OldHBitmap)<br />
            DeleteObject(HBitmap)<br />
            DeleteDC(MemDC)<br />
            DeleteDC(OldHBitmap)<br />

if you still have problems email me Smile | :)
let me know if this works for you Cool | :cool:

Fade (Amit BS)
GeneralRe: Images Pin
Fade (Amit BS)9-May-06 23:38
MemberFade (Amit BS)9-May-06 23:38 
GeneralFix for Memory Leak Pin
Matthew Hazlett30-Dec-03 15:42
MemberMatthew Hazlett30-Dec-03 15:42 
GeneralRe: Fix for Memory Leak Pin
Heath Stewart2-Jan-04 10:53
protectorHeath Stewart2-Jan-04 10:53 
GeneralBitBlt vs DrawImage Pin
Stan Shannon30-Dec-03 14:45
MemberStan Shannon30-Dec-03 14:45 
GeneralRe: BitBlt vs DrawImage Pin
Matthew Hazlett30-Dec-03 15:43
MemberMatthew Hazlett30-Dec-03 15:43 
GeneralRe: BitBlt vs DrawImage Pin
Weenerpoo16-Jan-04 6:14
sussWeenerpoo16-Jan-04 6:14 
GeneralRe: BitBlt vs DrawImage Pin
Fade (Amit BS)18-Apr-04 14:17
MemberFade (Amit BS)18-Apr-04 14:17 
GeneralRe: BitBlt vs DrawImage Pin
yvdh22-Jul-05 15:31
Memberyvdh22-Jul-05 15:31 

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.