Skip to content

Instantly share code, notes, and snippets.

@julesx
Created January 13, 2014 19:56
Show Gist options
  • Save julesx/8406825 to your computer and use it in GitHub Desktop.
Save julesx/8406825 to your computer and use it in GitHub Desktop.
public static Image ResizeImage(Image initImage, int templateWidth, int templateHeight)
{
Image templateImage = null;
try
{
if (initImage.Width <= templateWidth && initImage.Height <= templateHeight)
templateImage = initImage;
else
{
double templateRate = double.Parse(templateWidth.ToString()) / templateHeight;
double initRate = double.Parse(initImage.Width.ToString()) / initImage.Height;
if (templateRate == initRate)
{
templateImage = new Bitmap(templateWidth, templateHeight);
using (Graphics templateG = Graphics.FromImage(templateImage))
{
templateG.InterpolationMode = InterpolationMode.High;
templateG.SmoothingMode = SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(initImage, new Rectangle(0, 0, templateWidth, templateHeight), new Rectangle(0, 0, initImage.Width, initImage.Height), System.Drawing.GraphicsUnit.Pixel);
}
}
else
{
Image pickedImage = null;
Graphics pickedG = null;
var fromR = new Rectangle(0, 0, 0, 0);
var toR = new Rectangle(0, 0, 0, 0);
if (templateRate > initRate)
{
pickedImage = new Bitmap(initImage.Width, int.Parse(Math.Floor(initImage.Width / templateRate).ToString()));
pickedG = Graphics.FromImage(pickedImage);
fromR.X = 0;
fromR.Y = int.Parse(Math.Floor((initImage.Height - initImage.Width / templateRate) / 2).ToString());
fromR.Width = initImage.Width;
fromR.Height = int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
toR.X = 0;
toR.Y = 0;
toR.Width = initImage.Width;
toR.Height = int.Parse(Math.Floor(initImage.Width / templateRate).ToString());
}
else
{
pickedImage = new Bitmap(int.Parse(Math.Floor(initImage.Height * templateRate).ToString()), initImage.Height);
pickedG = Graphics.FromImage(pickedImage);
fromR.X = int.Parse(Math.Floor((initImage.Width - initImage.Height * templateRate) / 2).ToString());
fromR.Y = 0;
fromR.Width = int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
fromR.Height = initImage.Height;
toR.X = 0;
toR.Y = 0;
toR.Width = int.Parse(Math.Floor(initImage.Height * templateRate).ToString());
toR.Height = initImage.Height;
}
pickedG.InterpolationMode = InterpolationMode.HighQualityBicubic;
pickedG.SmoothingMode = SmoothingMode.HighQuality;
pickedG.DrawImage(initImage, toR, fromR, GraphicsUnit.Pixel);
templateImage = new Bitmap(templateWidth, templateHeight);
Graphics templateG = Graphics.FromImage(templateImage);
templateG.InterpolationMode = InterpolationMode.High;
templateG.SmoothingMode = SmoothingMode.HighQuality;
templateG.Clear(Color.White);
templateG.DrawImage(pickedImage, new Rectangle(0, 0, templateWidth, templateHeight), new Rectangle(0, 0, pickedImage.Width, pickedImage.Height), GraphicsUnit.Pixel);
templateG.Dispose();
pickedG.Dispose();
pickedImage.Dispose();
}
}
}
catch (Exception ex)
{
Logger.LogMessage("ImageHelper:ResizeImage ex: " + ex.Message);
}
return templateImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment