KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Scaling an image to fit in a picture object
PRODUCT: 4D | VERSION: 13.2 | PLATFORM: Mac & Win
Published On: January 7, 2013

This Technical Tip provides a utility method to resize a picture to fit proportionally in a picture object. If a picture is larger than a picture object, the picture displays something like this:
However, using the utility method provided in this Technical Tip will display the picture as shown:

The method takes in two parameter, a pointer to the source picture (the picture to be scaled), and a pointer to the destination picture object that the image needs to be scaled to fit into. The method uses two 4D commands, OBJECT GET COORDINATES and CREATE THUMBNAIL. The command OBJECT GET COORDINATES is used to get the coordinates of the picture object, this way the width and height can be determined. Once the width and height are determined, the command CREATE THUMBNAIL is used to fit the image into the picture variable.

Here is the code for the method:

// ----------------------------------------------------
// Method: image_thumbnail
// Description
// Scale image to fit in picture object
//
// Parameters
// $1 (Pointer) - Pointer to the picture variable to be scaled
// $2 (Pointer) - Pointer to the picture object the picture variable will be scaled
//                to fit into

// ----------------------------------------------------

C_POINTER($1;$origPic_p)
C_POINTER($2;$pictureVar_p)
C_LONGINT($left_l;$top_l;$right_l;$bottom_l)
C_LONGINT($width_l;$height_l)

$origPic_p:=$1
$pictureVar_p:=$2

OBJECT GET COORDINATES($pictureVar_p->;$left_l;$top_l;$right_l;$bottom_l)
$width_l:=$right_l-$left_l
$height_l:=$bottom_l-$top_l

CREATE THUMBNAIL($origPic_p->;$pictureVar_p->;$width_l;$height_l;Scaled to fit prop centered)