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)
Comments(0)