Skip to content

Instantly share code, notes, and snippets.

@rodolfofadino
Created April 9, 2012 17:03
Show Gist options
  • Save rodolfofadino/2344761 to your computer and use it in GitHub Desktop.
Save rodolfofadino/2344761 to your computer and use it in GitHub Desktop.
PadImage
public static System.Drawing.Image PadImage(System.Drawing.Image originalImage)
{
int largestDimension = Math.Max(originalImage.Height, originalImage.Width);
int h = originalImage.Size.Height;
int w = originalImage.Size.Width;
int final = 0;
if (w >= h)
final = h;
else
final = w;
Size squareSize = new Size(final, final);
Bitmap squareImage = new Bitmap(squareSize.Width, squareSize.Height);
using (Graphics graphics = Graphics.FromImage(squareImage))
{
graphics.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighQuality;
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
graphics.DrawImage(originalImage, (squareSize.Width / 2) - (originalImage.Width / 2), (squareSize.Height / 2) - (originalImage.Height / 2), originalImage.Width, originalImage.Height);
}
return squareImage;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment