5,427,303 members and growing! (15,328 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.
VBWindows, .NET, .NET 1.0, Win2K, WinXP, Win2003VS.NET2002, Visual Studio, Dev

Posted: 29 Dec 2003
Updated: 29 Dec 2003
Views: 74,744
Bookmarked: 15 times
Announcements
Want a new Job?



Search    
Advanced Search
Sitemap
14 votes for this Article.
Popularity: 3.67 Rating: 3.20 out of 5
3 votes, 21.4%
1
0 votes, 0.0%
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


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
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 21 of 21 (Total in Forum: 21) (Refresh)FirstPrevNext
Subject  Author Date 
GeneralClon BitBlt Vb to .Net (Sub BitBlt2)memberMember 366877515:12 15 Aug '08  
Questioncoloring images in picture boxmembersurgunsum1:16 13 Nov '07  
GeneralPrint ScreenmemberAliBong1:06 20 Aug '06  
GeneralConverting the Code Above for Layered Windows and Cmd Line Arguments [modified]memberMeskibob20:01 20 Jul '06  
Generalcode doesn't work with my version of vb.netmemberserendipity 2215:10 3 May '06  
GeneralMore memory problems with this examplememberMike.Anderson8:47 20 Dec '05  
GeneralRe: More memory problems with this examplememberMatthew Hazlett9:53 20 Dec '05  
GeneralGood work, thanks!memberPinhead7521:59 12 May '05  
Generalcopy invisible imagesmemberthecopymaster5:20 29 Sep '04  
GeneralRe: copy invisible imagesmemberbskirkman13:37 26 Jan '06  
GeneralImagesmemberFade (Amit BS)14:22 18 Apr '04  
GeneralRe: Imagesmemberserendipity 2215:14 3 May '06  
AnswerRe: Images- off screenmemberFade (Amit BS)23:33 9 May '06  
GeneralRe: ImagesmemberFade (Amit BS)23:38 9 May '06  
GeneralFix for Memory Leakmemberhazlema15:42 30 Dec '03  
GeneralRe: Fix for Memory LeakeditorHeath Stewart10:53 2 Jan '04  
GeneralBitBlt vs DrawImagememberStan Shannon14:45 30 Dec '03  
GeneralRe: BitBlt vs DrawImagememberhazlema15:43 30 Dec '03  
GeneralRe: BitBlt vs DrawImagesussWeenerpoo6:14 16 Jan '04  
GeneralRe: BitBlt vs DrawImagememberFade (Amit BS)14:17 18 Apr '04  
GeneralRe: BitBlt vs DrawImagememberyvdh15: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-2008
Web08 | Advertise on the Code Project