Click here to Skip to main content
6,305,776 members and growing! (16,799 online)
Email Password   helpLost your password?
Multimedia » GDI+ » General     Intermediate

Copying graphics with BitBlt (.NET Style)

By Matthew Hazlett

When Graphics.Clone() just won't do.
VB.NET 1.0, Win2K, WinXP, Win2003, Dev
Posted:29 Dec 2003
Views:88,782
Bookmarked:19 times
Announcements
Loading...
 
Search    
Advanced Search
printPrint   Broken Article?Report       add Share
  Discuss Discuss   Recommend Article Email
14 votes for this article.
Popularity: 3.67 Rating: 3.20 out of 5
3 votes, 21.4%
1

2
1 vote, 7.1%
3
3 votes, 21.4%
4
7 votes, 50.0%
5

Introduction

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 

     srcPic.ReleaseHdc(HDC1)
     srcMem.ReleaseHdc(HDC2)
     srcPic.Dispose()
     srcMem.Dispose()
     srcMem.Dispose()
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:

-or-

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.

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

Matthew Hazlett


Member
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.



Occupation: Web Developer
Location: United States United States

Other popular GDI+ articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 23 of 23 (Total in Forum: 23) (Refresh)FirstPrevNext
GeneralClon BitBlt Vb to .Net (Sub BitBlt2) PinmemberMember 366877515:12 15 Aug '08  
Questioncoloring images in picture box Pinmembersurgunsum1:16 13 Nov '07  
GeneralPrint Screen PinmemberAliBong1:06 20 Aug '06  
GeneralConverting the Code Above for Layered Windows and Cmd Line Arguments [modified] PinmemberMeskibob20:01 20 Jul '06  
Generalcode doesn't work with my version of vb.net Pinmemberserendipity 2215:10 3 May '06  
GeneralMore memory problems with this example PinmemberMike.Anderson8:47 20 Dec '05  
GeneralRe: More memory problems with this example PinmemberMatthew Hazlett9:53 20 Dec '05  
GeneralGood work, thanks! PinmemberPinhead7521:59 12 May '05  
Generalcopy invisible images Pinmemberthecopymaster5:20 29 Sep '04  
GeneralRe: copy invisible images Pinmemberbskirkman13:37 26 Jan '06  
GeneralRe: copy invisible images Pinmemberdi~v~inci6:03 11 Sep '08  
GeneralRe: copy invisible images PinmemberWan--Vevi18:22 20 Oct '08  
GeneralImages PinmemberFade (Amit BS)14:22 18 Apr '04  
GeneralRe: Images Pinmemberserendipity 2215:14 3 May '06  
AnswerRe: Images- off screen PinmemberFade (Amit BS)23:33 9 May '06  
GeneralRe: Images PinmemberFade (Amit BS)23:38 9 May '06  
GeneralFix for Memory Leak Pinmemberhazlema15:42 30 Dec '03  
GeneralRe: Fix for Memory Leak PineditorHeath Stewart10:53 2 Jan '04  
GeneralBitBlt vs DrawImage PinmemberStan Shannon14:45 30 Dec '03  
GeneralRe: BitBlt vs DrawImage Pinmemberhazlema15:43 30 Dec '03  
GeneralRe: BitBlt vs DrawImage PinsussWeenerpoo6:14 16 Jan '04  
GeneralRe: BitBlt vs DrawImage PinmemberFade (Amit BS)14:17 18 Apr '04  
GeneralRe: BitBlt vs DrawImage Pinmemberyvdh15:31 22 Jul '05  

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

PermaLink | Privacy | Terms of Use
Last Updated: 29 Dec 2003
Editor: Smitha Vijayan
Copyright 2003 by Matthew Hazlett
Everything else Copyright © CodeProject, 1999-2009
Web13 | Advertise on the Code Project