Skip to content

Instantly share code, notes, and snippets.

@jmcd
Created May 31, 2013 13:26
Show Gist options
  • Save jmcd/5684950 to your computer and use it in GitHub Desktop.
Save jmcd/5684950 to your computer and use it in GitHub Desktop.
Make a square image
private Image MakeSquareImage(Image source, int targetLength)
{
var sourceBitmap = new Bitmap(source);
var sourceLength = Math.Min(source.Size.Height, source.Size.Width);
var cropRect = new Rectangle((source.Size.Width - sourceLength)/2, (source.Size.Height - sourceLength)/2, sourceLength, sourceLength);
var squaredSource = sourceBitmap.Clone(cropRect, sourceBitmap.PixelFormat);
var result = new Bitmap(squaredSource, new Size(targetLength, targetLength));
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment