LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: Resize image and maintain aspect ratio (if needed)
#1
Posted
:
Wednesday, June 12, 2019 10:02:09 AM(UTC)
Groups: Manager, Tech Support, Administrators
Posts: 218
Was thanked: 12 time(s) in 12 post(s)
If you need to determine if an image needs to be resized to fit within a rectangle, then you can use the following method:
Code: public static void ResizeAspectRatio(RasterImage img, int canvasWidth, int canvasHeight)
{
// image is smaller than the canvas so no need to resize
if (img.Width < canvasWidth && img.Height < canvasHeight)
return;
// Figure out the ratio
double ratioX = (double)canvasWidth / (double)img.Width;
double ratioY = (double)canvasHeight / (double)img.Height;
// use whichever multiplier is smaller
double ratio = ratioX < ratioY ? ratioX : ratioY;
// now we can get the new height and width
int newHeight = Convert.ToInt32(img.Height * ratio);
int newWidth = Convert.ToInt32(img.Width * ratio);
//Resize the image
SizeCommand sizeCommand = new SizeCommand(newWidth, newHeight, RasterSizeFlags.Resample);
sizeCommand.Run(img);
}
Hadi Chami
Developer Support Manager
LEAD Technologies, Inc.
LEADTOOLS Support
General
LEADTOOLS SDK Examples
HOW TO: Resize image and maintain aspect ratio (if needed)
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.