Welcome Guest! To enable all features, please Login or Register.

Notification

Icon
Error

Options
View
Last Go to last post Unread Go to first unread post
#1 Posted : Thursday, June 1, 2017 4:18:09 PM(UTC)

Aaron  
Aaron

Groups: Registered, Tech Support, Administrators
Posts: 71

Was thanked: 4 time(s) in 3 post(s)

One of the most pressing needs in publishing today is converting a document of one size to another. Even even going as far as turning a portrait page sideways to landscape orientation or vice versa. For instance, some avatars that are comprised of uploaded images require you to have the uploaded image in a certain orientation (Landscape or Portrait).

The attached Visual Studio project developed in C# demonstrates how developers can use LEADTOOLS to convert their images' orientation from landscape to portrait or vice versa.

The project demonstrates how to perform the conversion in multiple ways:

1. Using the CombineCommand to create a new "background" image which uses the target orientation and pasting the original image on top of it. This would create a new image that appears to have margins around it:

Landscape -> Portrait
Code:

// Generate the new portrait width and height based on the landscape width and height ratio
double ratio = (double)image.Width / (double)image.Height;
double scaledWidth = image.Height * ratio;
double scaledHeight = image.Width * ratio;

using (RasterImage newImage = RasterImage.Create((int)scaledWidth, (int)scaledHeight, image.BitsPerPixel, image.XResolution, RasterColor.White))
{
   CombineCommand cmbCmd = new CombineCommand()
   {
      SourceImage = image.Clone(),
      DestinationRectangle = new LeadRect(0, 0, newImage.Width, newImage.Height),
      SourcePoint = new LeadPoint(0, (image.Height / 2) - (newImage.Height / 2)),
      Flags = CombineCommandFlags.SourceMaster
   };
   cmbCmd.Run(newImage);

   codecs.Save(newImage, @"..\..\output_portrait_combine.jpg", RasterImageFormat.Jpeg, 0);
}


Portrait -> Landscape
Code:

// Generate the new portrait width and height based on the landscape width and height ratio
double ratio = (double)image.Height / (double)image.Width;
double scaledWidth = image.Height * ratio;
double scaledHeight = image.Width * ratio;

using (RasterImage newImage = RasterImage.Create((int)scaledWidth, (int)scaledHeight, image.BitsPerPixel, image.XResolution, RasterColor.White))
{
   CombineCommand cmbCmd = new CombineCommand()
   {
      SourceImage = image.Clone(),
      DestinationRectangle = new LeadRect(0, 0, newImage.Width, newImage.Height),
      SourcePoint = new LeadPoint((image.Width / 2) - (newImage.Width/ 2), 0),
      Flags = CombineCommandFlags.SourceMaster
   };
   cmbCmd.Run(newImage);

   codecs.Save(newImage, @"..\..\output_landscape_combine.jpg", RasterImageFormat.Jpeg, 0);
}


2. Using the CropCommand to crop a section of the image to the target orientation:

Landscape -> Portrait
Code:

using (RasterImage newImage = image.Clone())
{
   // Generate the new portrait width and height based on the landscape width and height ratio
   ratio = (double)image.Width / (double)image.Height;
   scaledWidth = image.Height / ratio;
   scaledHeight = image.Width / ratio;

   CropCommand cropCmd = new CropCommand()
   {
      Rectangle = new LeadRect((int)((image.Width / 2) - (scaledWidth / 2)), 0, (int)scaledWidth, (int)scaledHeight)
   };
   cropCmd.Run(newImage);

   codecs.Save(newImage, @"..\..\output_portrait_crop.jpg", RasterImageFormat.Jpeg, 0);
}


Portrait -> Landscape
Code:

using (RasterImage newImage = image.Clone())
{
   // Generate the new portrait width and height based on the landscape width and height ratio
   ratio = (double)image.Height / (double)image.Width;
   scaledWidth = image.Height / ratio;
   scaledHeight = image.Width / ratio;

   CropCommand cropCmd = new CropCommand()
   {
      Rectangle = new LeadRect(0, (int)((image.Height / 2) - (scaledHeight / 2)), (int)scaledWidth, (int)scaledHeight)
   };
   cropCmd.Run(newImage);

   codecs.Save(newImage, @"..\..\output_landscape_crop.jpg", RasterImageFormat.Jpeg, 0);
}


Here are before and after screen shots of the images:

Landscape -> Portrait
Landscape to Portrait

Portrait -> Landscape
Portrait to Landscape

File Attachment(s):
CSPortraitOrLandscape.zip (691kb) downloaded 384 time(s).

Aaron Brasington
Developer Support Engineer
LEAD Technologies, Inc.

LEAD Logo
 

Try the latest version of LEADTOOLS for free for 60 days by downloading the evaluation: https://www.leadtools.com/downloads

Wanna join the discussion? Login to your LEADTOOLS Support accountor Register a new forum account.

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.

Powered by YAF.NET | YAF.NET © 2003-2024, Yet Another Forum.NET
This page was generated in 0.086 seconds.