Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Creating Print Screen Function using Window’s API’s

Author: DevASP
Download Source Code : 303_CaptureScreen.zip

In this article I will try to explain you how you can create a "Print Screen" function in Visual Basic.Net (VB.NET)

You have seen many times “Print Screen/Sys Rq” button on your keyboard. It is used the capture your desktop screen shot as an image. You can use this screen shot in many application as you required.

 

  • Steps you will do.
  • Open visual studio and start a new window application.
  • Set the following properties of Form using properties window.

Text = Capture, FormBorderStyle = FixedSingle, MaximizeBox = False, Size = 800,600

 

  • Drop a picture box control from the toolbox and set its size to 800,600.
  • Now open the code window and declare the following functions that use window API’s to perfume the functionality of print screen button.

Private Declare Function CreateDC Lib "gdi32" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, ByVal lpInitData As String) As Integer

 

 

Private Declare Function CreateCompatibleDC Lib "GDI32" (ByVal hDC As Integer) As Integer

 

 

Private Declare Function CreateCompatibleBitmap Lib "GDI32" (ByVal hDC As Integer, ByVal nWidth As Integer, ByVal nHeight As Integer) As Integer

 

 

Private Declare Function GetDeviceCaps Lib "gdi32" Alias "GetDeviceCaps" (ByVal hdc As Integer, ByVal nIndex As Integer) As Integer

 

 

Private Declare Function SelectObject Lib "GDI32" (ByVal hDC As Integer, ByVal hObject As Integer) As Integer

 

 

Private Declare Function BitBlt Lib "GDI32" (ByVal srchDC As Integer, ByVal srcX As Integer, ByVal srcY As Integer, ByVal srcW As Integer, ByVal srcH As Integer, ByVal desthDC As Integer, ByVal destX As Integer, ByVal destY As Integer, ByVal op As Integer) As Integer

 

 

Private Declare Function DeleteDC Lib "GDI32" (ByVal hDC As Integer) As Integer

 

 

Private Declare Function DeleteObject Lib "GDI32" (ByVal hObj As Integer) As Integer

 

Const SRCCOPY As Integer = &HCC0020

 

Private Background As Bitmap

Private fw, fh As Integer

 

  • Now write a sub procedure that will capture the screen using above functions.

Protected Sub CaptureScreen()

 

Dim hsdc, hmdc As Integer

Dim hbmp, hbmpold As Integer

Dim r As Integer

 

hsdc = CreateDC("DISPLAY", "", "", "")

hmdc = CreateCompatibleDC(hsdc)

 

fw = GetDeviceCaps(hsdc, 8)

fh = GetDeviceCaps(hsdc, 10)

hbmp = CreateCompatibleBitmap(hsdc, fw, fh)

 

hbmpold = SelectObject(hmdc, hbmp)

r = BitBlt(hmdc, 0, 0, fw, fh, hsdc, 0, 0, 13369376)

 

hbmp = SelectObject(hmdc, hbmpold)

 

r = DeleteDC(hsdc)

r = DeleteDC(hmdc)

 

Background = Image.FromHbitmap(New IntPtr(hbmp))

 

DeleteObject(hbmp)

 

End Sub

In the Load event of form call the procedure, assign the image to picture box so that it will be displayed to the user. Use the save function of Picture Box to save the image. The image will be saved in your bin folder of project. You can change the path or can also show the save file dialog box to ask user save the file anywhere he wants.

Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load

       

CaptureScreen()

PictureBox1.Image = Background        PictureBox1.Image.Save("Capture.jpg")

 

End Sub

Article Comments
It was very well explained and hence helpful! Good work.

Posted on 12/29/2006 12:35:58 AM by Sanjay

It was very well explained and hence helpful! Good work.

Posted on 12/29/2006 12:35:59 AM by Sanjay

I want code to taking print screen of the selected part only not the entire screen.

Posted on 2/1/2007 5:28:24 AM by arun

I need to print the Image of a selected part too.I'll be very thankful if u send me the code.

thanks for ur help

Posted on 3/30/2007 5:16:49 AM by hana

Thank u so muck
it Was very well explained!

Posted on 4/20/2007 2:56:29 PM by Ammar -yemen

how to assign the print screen key and paste them.it is possible in vb.net?

Posted on 2/12/2008 8:32:20 AM by guna vignesh

its excellent and it will help learner too.. to grap the stic in their oops

Posted on 1/10/2010 2:44:18 AM by jagadish nayak

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How To Create An Analog Clock in Visual Basic.Net (VB.Net)

Disclaimer - Privacy
© 2002-2017 DevASP.net