GetRect Method

Applies to TestComplete 15.47, last modified on January 20, 2023

Description

The Picture.GetRect method returns the specified rectangular area of a given picture as a new Picture object.

Declaration

PictureObj.GetRect(X, Y, Width, Height)

PictureObj An expression, variable or parameter that specifies a reference to a Picture object
X [in]    Required    Integer    
Y [in]    Required    Integer    
Width [in]    Optional    Integer Default value: -1   
Height [in]    Optional    Integer Default value: -1   
Result A Picture object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameters:

X

The X coordinate of the top-left corner of the desired rectangular area within the image.

Y

The Y coordinate of the top-left corner of the desired rectangular area within the image.

Width

The width of the desired rectangular area within the image. The default value is -1, which means that the rectangle ends at the left side of the given image.

Height

The height of the desired rectangular area within the image. The default value is -1, which means that the rectangle ends at the bottom side of the given image.

Result Value

The Picture object holding the specified rectangular area of the given image.

Example

The following example demonstrates how to create an empty Picture object using the Utils.Picture method and how to work with images using methods of the Picture object.

JavaScript

function Test1()
{
  var Pict, NewPict, x, y;

  // Creates a new empty Picture object
  Pict = Utils.Picture;

  // Loads the image from a file and posts it to the log
  Pict.LoadFromFile("D:\\Data\\my_image.png");
  Log.Picture(Pict);

  // Obtains the bounds of the new image
  x = Pict.Size.Width - 10;
  y = Pict.Size.Height - 10;

  // Obtains the new Picture object containing the specified rectangular area of the loaded image
  NewPict = Pict.GetRect(0, 0, x, y);

  // Changes the color of the top left pixel of the new image to the red color
  NewPict.$set("Pixels", 0, 0, clRed);

  // Saves the obtained picture to the new file and posts the image to the log
  NewPict.SaveToFile("D:\\Data\\my_new_image.png");
  Log.Picture(NewPict);
}

JScript

function Test1()
{
  var Pict, NewPict, x, y;

  // Creates a new empty Picture object
  Pict = Utils.Picture;

  // Loads the image from a file and posts it to the log
  Pict.LoadFromFile("D:\\Data\\my_image.png");
  Log.Picture(Pict);

  // Obtains the bounds of the new image
  x = Pict.Size.Width - 10;
  y = Pict.Size.Height - 10;

  // Obtains the new Picture object containing the specified rectangular area of the loaded image
  NewPict = Pict.GetRect(0, 0, x, y);

  // Changes the color of the top left pixel of the new image to the red color
  NewPict.Pixels(0,0)= clRed;

  // Saves the obtained picture to the new file and posts the image to the log
  NewPict.SaveToFile("D:\\Data\\my_new_image.png");
  Log.Picture(NewPict);
}

Python

def Test1():
  
  # Creates a new empty Picture object
  Pict = Utils.Picture;

  # Loads the image from a file and posts it to the log
  Pict.LoadFromFile("D:\\Data\\my_image.png")
  Log.Picture(Pict)

  # Obtains the bounds of the new image
  x = Pict.Size.Width - 10
  y = Pict.Size.Height - 10

  # Obtains the new Picture object containing the specified rectangular area of the loaded image
  NewPict = Pict.GetRect(0, 0, x, y)

  # Changes the color of the top left pixel of the new image to the red color
  NewPict.Pixels[0,0] = clRed

  # Saves the obtained picture to the new file and posts the image to the log
  NewPict.SaveToFile("D:\\Data\\my_new_image.png")
  Log.Picture(NewPict)

VBScript

Sub Test1
  Dim Pict, NewPict, x, y

  ' Creates a new empty Picture object
  Set Pict = Utils.Picture

  ' Loads the image from a file and posts it to the log
  Call Pict.LoadFromFile("D:\Data\my_image.png")
  Log.Picture(Pict)

  ' Obtains the bounds of the new image
  x = Pict.Size.Width - 10
  y = Pict.Size.Height - 10

  ' Obtains the new Picture object containing the specified rectangular area of the loaded image
  Set NewPict = Pict.GetRect(0, 0, x, y)

  ' Changes the color of the top left pixel of the new image to the red color
  NewPict.Pixels(0,0)= clRed

  ' Saves the obtained picture to the new file and posts the image to the log
  Call NewPict.SaveToFile("D:\Data\my_new_image.png")
  Log.Picture(NewPict)
End Sub

DelphiScript

procedure Test1;
var Pict, NewPict, x, y;
begin
  // Creates a new empty Picture object
  Pict := Utils.Picture;

  // Loads the image from a file and posts it to the log
  Pict.LoadFromFile('D:\Data\my_image.png');
  Log.Picture(Pict);

  // Obtains the bounds of the new image
  x := Pict.Size.Width - 10;
  y := Pict.Size.Height - 10;

  // Obtains the new Picture object containing the specified rectangular area of the loaded image
  NewPict := Pict.GetRect(0, 0, x, y);

  // Changes the color of the top left pixel of the new image to the red color
  NewPict.Pixels(0,0) := clRed;

  // Saves the obtained picture to the new file and posts the image to the log
  NewPict.SaveToFile('D:\Data\my_new_image.png');
  Log.Picture(NewPict);
end;

C++Script, C#Script

function Test1()
{
  var Pict, NewPict, x, y;

  // Creates a new empty Picture object
  Pict = Utils["Picture"];

  // Loads the image from a file and posts it to the log
  Pict["LoadFromFile"]("D:\\Data\\my_image.png");
  Log["Picture"](Pict);

  // Obtains the bounds of the new image
  x = Pict["Size"]["Width"] - 10;
  y = Pict["Size"]["Height"] - 10;

  // Obtains the new Picture object containing the specified rectangular area of the loaded image
  NewPict = Pict["GetRect"](0, 0, x, y);

  // Changes the color of the top left pixel of the new image to the red color
  NewPict["Pixels"](0,0)= clRed;

  // Saves the obtained picture to the new file and posts the image to the log
  NewPict["SaveToFile"]("D:\\Data\\my_new_image.png");
  Log["Picture"](NewPict);
}

See Also

About Region Checkpoints
Picture.Compare
Picture.Difference
Picture.Find

Highlight search results