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.
///
/// 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;
}
}
}