Skip to content

ScaleImageToFill

jdubs edited this page Oct 21, 2016 · 1 revision

Table of Contents

Overview

Calculates the best possible image scale dimensions, without distorting ratio, and applies the new dimensions to the image. The image must be contained in an area template. The area template will crop the image.

Parameters

ImageElement (constant) The name of the image that will be scaled.

ErrorMessage (multiple | optional) The error message to return when any error occurs.

Special Note

The anchor point plays a key role in this action. If you wish the image cropping to be in a particular section of the image use the anchor point property.

Source Code

public static void ScaleImageToFill(object ImageElement, object ErrorMessage)
{
	Break();
	try
	{
		Image img = (Image)Application.CurrentDocument.FindElement((string)ImageElement);

		Shape frame = (Shape)img.ParentElement;
		Measurement frameWidth = frame.Width;
		Measurement frameHeight = frame.Height;

		Measurement cWidth = img.Width;
		Measurement cHeight = img.Height;

		double hRatio = frameHeight / cHeight;
		double wRatio = frameWidth / cWidth;

		if (hRatio > wRatio)
		{
			Measurement newHeight = cHeight * hRatio;
			Measurement newWidth = (cWidth / cHeight) * newHeight;

			img.Height = newHeight;
			img.Width = newWidth;
		}
		else
		{
			Measurement newWidth = cWidth * wRatio;
			Measurement newHeight = (cHeight / cWidth) * newWidth;
			img.Width = newWidth;
			img.Height = newHeight;
		}

		cWidth = img.Width;
		cHeight = img.Height;
	}
	catch (Exception ex)
	{
		throw new Four51ActionsException(ex.Message, (string)ErrorMessage);
	}
}

Referenced Methods

Clone this wiki locally