CreatePictureConfiguration Method

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

Description

Use the CreatePictureConfiguration method to create an ImageConfiguration object. The object defines the format of images you save to a file by using the Picture.SaveToFile method.

After you have created the ImageConfiguration object, use its properties to specify the desired format settings. To apply the configuration, pass it to the Picture.SaveToFile method.

Declaration

PictureObj.CreatePictureConfiguration(ImageFormat)

PictureObj An expression, variable or parameter that specifies a reference to a Picture object
ImageFormat [in]    Required    String    
Result An ImageConfiguration object

Applies To

The method is applied to the following object:

Parameters

The method has the following parameter:

ImageFormat

A string that specifies the image format that you would like to configure. Possible values are —

Constant Description
"BMP" BMP image configuration
"JPEG" JPEG image configuration
"PNG" PNG image configuration
"TIFF" TIFF image configuration
"GIF" GIF image configuration
"ICO" ICO image configuration
Note: You can use custom image adapters to get support for additional image formats.

The set of properties, which the created ImageConfiguration object will have, depends on the specified image format. For example, if you create a configuration for BMP images, the object will hold properties specific for the BMP format, if you create a configuration for PNG images, the object will contain properties specific to the PNG format. To learn more, see the ImageConfiguration object description.

Result Value

An ImageConfiguration object that can be used to configure image format settings.

Example

The following example demonstrates how you can save an image to a file in the GIF format with the color reduction set to grayscale:

JavaScript, JScript

function SaveImageToGIF()
{
  var source = "C:\\Data\\img.png";
  var target = "C:\\Data\\img_new";

  var pic = Utils.Picture;
  if (pic.LoadFromFile(source))
  {
    // Creates a GIF format configuration
    config = pic.CreatePictureConfiguration("gif");
    config.ColorReduction = "Grayscale (256 shades)";

    // Saves the image in the specified format
    if (! pic.SaveToFile(target, config))
      Log.Warning("Failed to save the image to the specified file");
  }
  else
    Log.Warning("The specified file " + source + " does not exist.");
}

Python

def SaveImageToGIF():
  source = "C:\\Data\\img.png"
  target = "C:\\Data\\img_new"
   
  pic = Utils.Picture()
  if pic.LoadFromFile(source):
    # Creates a GIF format configuration 
    config = pic.CreatePictureConfiguration("gif")
    config.ColorReduction = "Grayscale (256 shades)"

    # Saves the image with the specified format
    if not pic.SaveToFile(target, config):
      Log.Warning("Failed to save the image to the specified file") 
  
  else:
    Log.Warning("The specified file " + source + " does not exist.")

VBScript

Sub SaveImageToGIF

  source = "C:\\Data\\img.png"
  target = "C:\\Data\\img_new"

  Set pic = Utils.Picture
  If pic.LoadFromFile(source) Then
    ' Creates a GIF format configuration
    Set config = pic.CreatePictureConfiguration("gif")
    config.ColorReduction = "Grayscale (256 shades)"

    ' Saves the image in the specified format
    If Not pic.SaveToFile(target, config) Then
      Log.Warning("Failed to save the image to the specified file")
    End If

  Else
    Log.Warning("The specified file " + source + " does not exist.")
  End If

End Sub

DelphiScript

procedure SaveImageToGIF();
  var source, target : string;
  var pic, config : OleVariant;
  begin

  source := 'C:\Data\img.png';
  target := 'C:\Data\img_new';

  pic := Utils.Picture;
  if pic.LoadFromFile(source) then
  begin
    // Creates a GIF format configuration
    config := pic.CreatePictureConfiguration('gif');
    config.ColorReduction := 'Grayscale (256 shades)';

    // Saves the image in the specified format
    if not pic.SaveToFile(target, config) then
      Log.Warning('Failed to save the image to the specified file.');
  end

  else
    Log.Warning('The specified file ' + source + ' does not exist.');
end;

C++Script, C#Script

function SaveImageToGIF()
{
  var source = "C:\\Data\\img.png";
  var target = "C:\\Data\\img_new";

  var pic = Utils["Picture"];
  if (pic["LoadFromFile"](source))
  {
    // Creates a GIF format configuration
    config = pic["CreatePictureConfiguration"]("gif");
    config["ColorReduction"] = "Grayscale (256 shades)";

    // Saves the image in the specified format
    if (! pic["SaveToFile"](target, config))
      Log["Warning"]("Failed to save the image to the specified file");
  }
  else
    Log["Warning"]("The specified file " + source + " does not exist.");
}

The following example demonstrates how you can save an image to a file in the PNG format with the compression level set to 9 (the smallest file size):

JavaScript, JScript

function SaveImageToPNG()
{
  var source = "C:\\Data\\img.png";
  var target = "C:\\Data\\img_new";

  var pic = Utils.Picture;
  if (pic.LoadFromFile(source))
  {
    // Creates a PNG format configuration
    config = pic.CreatePictureConfiguration("png");
    config.CompressionLevel = 9;

    // Saves the image in the specified format
    if (! pic.SaveToFile(target, config))
      Log.Warning("Failed to save the image to the specified file");
  }
  else
    Log.Warning("The specified file " + source + " does not exist.");
}

Python

def SaveImageToPNG():
  source = "C:\\Data\\img.png"
  target = "C:\\Data\\img_new"
   
  pic = Utils.Picture()
  if pic.LoadFromFile(source):
    # Creates a GIF format configuration 
    config = pic.CreatePictureConfiguration("png")
    config.CompressionLevel = 9;
  
    # Saves the image with the specified format
    if not pic.SaveToFile(target, config):
      Log.Warning("Failed to save the image to the specified file") 
  
  else:
    Log.Warning("The specified file " + source + " does not exist.")

VBScript

Sub SaveImageToPNG

  source = "C:\\Data\\img.png"
  target = "C:\\Data\\img_new"

  Set pic = Utils.Picture
  If pic.LoadFromFile(source) Then
    ' Creates a PNG format configuration
    Set config = pic.CreatePictureConfiguration("png")
    config.CompressionLevel = 9

    ' Saves the image in the specified format
    If Not pic.SaveToFile(target, config) Then
      Log.Warning("Failed to save the image to the specified file")
    End If

  Else
    Log.Warning("The specified file " + source + " does not exist.")
  End If

End Sub

DelphiScript

procedure SaveImageToPNG();
  var source, target : string;
  var pic, config : OleVariant;
  begin

  source := 'C:\Data\img.png';
  target := 'C:\Data\img_new';

  pic := Utils.Picture;
  if pic.LoadFromFile(source) then
  begin
    // Creates a PNG format configuration
    config := pic.CreatePictureConfiguration('png');
    config.CompressionLevel := 9;

    // Saves the image in the specified format
    if not pic.SaveToFile(target, config) then
      Log.Warning('Failed to save the image to the specified file.');
  end

  else
    Log.Warning('The specified file ' + source + ' does not exist.');
end;

C++Script, C#Script

function SaveImageToPNG()
{
  var source = "C:\\Data\\img.png";
  var target = "C:\\Data\\img_new";

  var pic = Utils["Picture"];
  if (pic["LoadFromFile"](source))
  {
    // Creates a PNG format configuration
    config = pic["CreatePictureConfiguration"]("png");
    config["CompressionLevel"] = 9;

    // Saves the image in the specified format
    if (! pic["SaveToFile"](target, config))
      Log["Warning"]("Failed to save the image to the specified file");
  }
  else
    Log["Warning"]("The specified file " + source + " does not exist.");
}

See Also

SaveToFile Method
Specifying Log Images Format
Image Adapters

Highlight search results