Open samir-syed opened 6 years ago
I found a snippet online which does what I need:
public static System.Drawing.Bitmap ResizeImage(Image image, int width, int height)
{
var destRect = new Rectangle(0, 0, width, height);
var destImage = new Bitmap(width, height);
destImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
using (var graphics = Graphics.FromImage(destImage))
{
graphics.CompositingMode = CompositingMode.SourceCopy;
graphics.CompositingQuality = CompositingQuality.HighQuality;
graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = SmoothingMode.HighQuality;
graphics.PixelOffsetMode = PixelOffsetMode.HighQuality;
using (var wrapMode = new ImageAttributes())
{
wrapMode.SetWrapMode(WrapMode.TileFlipXY);
graphics.DrawImage(image, destRect, 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, wrapMode);
}
}
return destImage;
}
And calling the above method from this:
private static void Resize(string path, int width, int height, string destPath)
{
//var resizer = new ImageResizer(path);
//var byteArray = resizer.Resize(width, height, false, ImageEncoding.Jpg100);
//resizer.SaveToFile(destPath);
Image imgPhoto = Image.FromFile(path);
Bitmap image = ResizeImage(imgPhoto, width, height);
image.Save(destPath);
}
I understand the images might end up being skewed but an optional parameter would help people like me to use this plugin.
Below is the function I use to resize:
The image I am trying to resize is 2500 x 2500 in size (px) is as below:
For example I am having issues resizing the above image to the following dimensions (in px):
1024 x 500 (Note the company name being cropped):
720 x 1280 (Note the label being cropped):
Am I doing something wrong here? Is there an option to ignore what happens to the resulting image and just resize the image. I mean an option to just ignore the aspect ratio and just resize it to the provided dimensions?