How to resize an image
How to resize an image
For resizing an image, we are using the method Load of the class Image to load an image into a cache memory. Then we apply Resize method to the image with a new size by specifying desired height and width `300x300` in pixels and save the result to a new file.
Example C# code:
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
using (Image image = Image.Load(dataDir + "template.jpg")) | |
{ | |
image.Resize(300, 300); | |
image.Save(dataDir + "result.jpg"); | |
} | |
File.Delete(dataDir + "result.jpg"); |
Additionally, you can make photo sizing proportionally the picture width or height and specify the resizing type by selecting `ResizeType.LanczosResample` parameter:
using Aspose.Imaging; | |
using Aspose.Imaging.FileFormats.Png; | |
using Aspose.Imaging.FileFormats.Svg; | |
using Aspose.Imaging.FileFormats.Webp; | |
using Aspose.Imaging.ImageOptions; | |
using System; | |
using System.IO; | |
string templatesFolder = @"c:\Users\USER\Downloads\templates\"; | |
string dataDir = templatesFolder; | |
// Load an image from disk | |
using (Image image = Image.Load(dataDir + "template.jpg")) | |
{ | |
if (!image.IsCached) | |
{ | |
image.CacheData(); | |
} | |
// Specifying only height, width and ResizeType | |
int newWidth = image.Width / 2; | |
image.ResizeWidthProportionally(newWidth, ResizeType.LanczosResample); | |
int newHeight = image.Height / 2; | |
image.ResizeHeightProportionally(newHeight, ResizeType.NearestNeighbourResample); | |
image.Save(dataDir + "result.png"); | |
} | |
File.Delete(dataDir + "result.png"); |
There are several Resize types available for resizing photos. Please see the link to the table which describes the resize type parameters.