Skip to content

Instantly share code, notes, and snippets.

@Larry57
Created September 15, 2015 10:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Larry57/ae06d979987d07677483 to your computer and use it in GitHub Desktop.
Save Larry57/ae06d979987d07677483 to your computer and use it in GitHub Desktop.
Calculate a zoom
// sourceSize = source control size
// targetSize = target control size
// center of the zoom
// zoom radius
static Rectangle Zoom(Size sourceSize, Size targetSize, Point center, int radius)
{
var f = Math.Min((double)targetSize.Width / ((double)radius * 2d), (double)targetSize.Height / ((double)radius * 2d));
var loc = new Point((int)((-center.X + radius) * f), (int)((-center.Y + radius) * f));
var size = new Size((int)(sourceSize.Width * f), (int)(sourceSize.Height * f));
loc.X += (int)(((double)targetSize.Width - (double)radius * f * 2d) / 2d);
loc.Y += (int)(((double)targetSize.Height - (double)radius * f * 2d) / 2d);
return new Rectangle(loc, size);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment