using System;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace SpritePacker.SBRLUtilities
{
public static partial class ImageTools
{
///
/// Resizes a Bitmap such that it fits within the target dimensions.
///
///
/// v0.1, by Starbeamrainbowlabs
/// Last updated on 2nd August 2016
/// Licensed under MPL v2.0.
///
/// Changelog:
/// v0.1 (2nd August 2016):
/// - Initial Release.
///
/// The image to resize.
/// The target dimensions.
/// The resized image.
public static Bitmap ResizeImage(Bitmap sourceImage, Size targetBox)
{
float scaleFactor = Math.Min(
targetBox.Width / (float)sourceImage.Width,
targetBox.Height / (float)sourceImage.Height
);
Size thumbnailSize = new Size(
(int)(sourceImage.Width * scaleFactor),
(int)(sourceImage.Height * scaleFactor)
);
Bitmap resultImage = new Bitmap(thumbnailSize.Width, thumbnailSize.Height);
using (Graphics context = Graphics.FromImage(resultImage)) {
context.CompositingMode = CompositingMode.SourceCopy;
context.InterpolationMode = InterpolationMode.HighQualityBicubic;
context.DrawImage(sourceImage, new Rectangle(
Point.Empty,
thumbnailSize
));
}
return resultImage;
}
}
}