Click here to Skip to main content
Click here to Skip to main content

Copying graphics with BitBlt (.NET Style)

, 29 Dec 2003
Rate this:
Please Sign up or sign in to vote.
When Graphics.Clone() just won't do.

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
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) PinmemberMember 366877515-Aug-08 14:12 
Questioncoloring images in picture box Pinmembersurgunsum13-Nov-07 0:16 
hello,
 

I have a image in picturebox, and the other image in the background of the picturebox.
 
the background image is the same image which is in picturebox1.image but it is coloured with block of dark colors for each parts.
 
now if i click on the picture box , i have given the code to retrive the color (ARGB) of background image where i clicked and then i have to colour the foreground image with the selected color from palette ,
but its coloring the background itself
...
i am using this code...
for coloring
 
Sleepy | :zzz:
 
Private Sub DoFill(ByVal sender As Object, ByVal ea As EventArgs)
 
' translate point to client co-ordinates
 
Dim pt As Point = m_pb.PointToClient(Control.MousePosition)
 

 
Dim x As Integer = pt.X
 
Dim y As Integer = pt.Y
 
Dim hDC1, hDC, hBrush As IntPtr
 
Dim g As Graphics = m_pb.CreateGraphics()
 

 
' get color of pixel clicked on
 
Dim clr As Color = CType(m_pb.BackgroundImage, Bitmap).GetPixel(x, y)
 
Dim cl As Color = CType(m_pb.Image, Bitmap).GetPixel(x, y)

Dim gdi_clr As Color = Color.FromArgb(0, clr.B, clr.G, clr.R)
Dim gdi_cl As Color = Color.FromArgb(0, cl.B, cl.G, cl.R)
Try
 

' get our device context
 

hDC = g.GetHdc()

 

 
' create a solid brush
 
' NOTE: When using GDI, pass "A,B,G,R" to Color.FromARGB()
 
hBrush = CreateSolidBrush(Color.FromArgb(0, 0, 0, 255).ToArgb())
 
' select into the DC
 

 

 
Dim last As IntPtr = SelectObject(hDC, hBrush)
 
' fill it
 
ExtFloodFill(hDC, x, y, gdi_clr.ToArgb(), FLOODFILLSURFACE)
 

 

' restore last object
 
SelectObject(hDC, last)
 
' delete the brush
 
DeleteObject(hBrush)
 
'Catch e As Exception
 
'Throw (e)
 
Finally
 

' release the DC and dispose of the object
 
g.ReleaseHdc(hDC)
 

 
g.Dispose()
 
End Try
 

 
End Sub
 
thanks a lot

GeneralPrint Screen PinmemberAliBong20-Aug-06 0:06 
GeneralConverting the Code Above for Layered Windows and Cmd Line Arguments [modified] PinmemberMeskibob20-Jul-06 19:01 
Generalcode doesn't work with my version of vb.net Pinmemberserendipity 223-May-06 14:10 
GeneralMore memory problems with this example PinmemberMike.Anderson20-Dec-05 7:47 
GeneralRe: More memory problems with this example PinmemberMatthew Hazlett20-Dec-05 8:53 
GeneralGood work, thanks! PinmemberPinhead7512-May-05 20:59 
Generalcopy invisible images Pinmemberthecopymaster29-Sep-04 4:20 
GeneralRe: copy invisible images Pinmemberbskirkman26-Jan-06 12:37 
GeneralRe: copy invisible images Pinmemberdi~v~inci11-Sep-08 5:03 
GeneralRe: copy invisible images PinmemberWan--Vevi20-Oct-08 17:22 
GeneralImages PinmemberFade (Amit BS)18-Apr-04 13:22 
GeneralRe: Images Pinmemberserendipity 223-May-06 14:14 
AnswerRe: Images- off screen PinmemberFade (Amit BS)9-May-06 22:33 
GeneralRe: Images PinmemberFade (Amit BS)9-May-06 22:38 
GeneralFix for Memory Leak Pinmemberhazlema30-Dec-03 14:42 
GeneralRe: Fix for Memory Leak PineditorHeath Stewart2-Jan-04 9:53 
GeneralBitBlt vs DrawImage PinmemberStan Shannon30-Dec-03 13:45 
GeneralRe: BitBlt vs DrawImage Pinmemberhazlema30-Dec-03 14:43 
GeneralRe: BitBlt vs DrawImage PinsussWeenerpoo16-Jan-04 5:14 
GeneralRe: BitBlt vs DrawImage PinmemberFade (Amit BS)18-Apr-04 13:17 
GeneralRe: BitBlt vs DrawImage Pinmemberyvdh22-Jul-05 14:31 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 30 Dec 2003
Article Copyright 2003 by Matthew Hazlett
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid