Select Page
Codoid Blog

Capturing Screen Region

Being a leading QA Company, we write blogs on all prominent software testing topics and tools using our real-world experience. So stay sharp by subscribing to our Newsletter.

Using QTP we can capture an object or Desktop using CaptureBitmap method. How to capture a region of screen?

It can be done using System.Drawing.Graphics.CopyFromScreen method. Let us see how it can be done using this method.

Code

'Function Name: CaptureRegion
'Argument 1: strPath- File path where you want to store the captured image.
'Argument 2: inX- X co-ordinate
'Argument 3: inY- Y co-ordinate
'Argument 4: inHeight- Image Height
'Argument 5: inWidth- Image Width

Function CaptureRegion(ByVal strPath,ByVal inX,ByVal inY,ByVal inHeight,ByVal inWidth)

'Declaring all the required Local variables
Dim oBitmap,oPixelFormat,oGraphic,oPoint,oSize

'Creating Pixel Format object
Set oPixelFormat=DotNetFactory.CreateInstance("System.Drawing.Imaging.PixelFormat","System.Drawing")

'Point structure to specify x and y to capture a region
Set oPoint=DotNetFactory.CreateInstance("System.Drawing.Point","System.Drawing",inX,inY)

'Size structure for new image
Set oSize=DotNetFactory.CreateInstance("System.Drawing.Size","System.Drawing",inWidth,inHeight)

'Creating Bitmap object with Height, Width and Format32bppArgb pixel format
Set oBitmap=DotNetFactory.CreateInstance("System.Drawing.Bitmap","System.Drawing",inWidth,inHeight,oPixelFormat.Format32bppArgb)

'Graphic object
Set oGraphic=DotNetFactory.CreateInstance("System.Drawing.Graphics","System.Drawing")

'Mapping Bitmap object with Graphic object
Set oGraphic=oGraphic.FromImage(oBitmap)

'Capturing the screen region using CopyFromScreen method
oGraphic.CopyFromScreen oPoint,oPoint,oSize

'Saving the file
oBitmap.Save(strPath)

Set oPixelFormat=Nothing
Set oPoint=Nothing
Set oSize=Nothing
Set oBitmap=Nothing
Set oGraphic=Nothing
End Function

Dim strPath
strPath="C:regionCapture.png"

'Bringing the screen to be captured to foreground
Browser("name:=iGoogle").Page("title:=iGoogle").Highlight

'Calling CaptureRegion function
Call CaptureRegion(strPath,0,0,20,40)
  

Synopsis

This technique is very useful as our testing might require capturing region of a screen. Keep visiting for more imaging techniques articles.
Comments(0)

Submit a Comment

Your email address will not be published. Required fields are marked *

Talk to our Experts

Amazing clients who
trust us


poloatto
ABB
polaris
ooredo
stryker
mobility