/* * Greenshot - a free and open source screenshot tool * Copyright (C) 2007-2011 Thomas Braun, Jens Klingen, Robin Krom * * For more information see: http://getgreenshot.org/ * The Greenshot project is hosted on Sourceforge: http://sourceforge.net/projects/greenshot/ * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 1 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ using System; using System.Drawing; namespace Greenshot.Helpers { /// /// Offers a few helper functions for scaling/aligning an element with another element /// public class ScaleHelper { private ScaleHelper() { } /// /// calculates the Size an element must be resized to, in order to fit another element, keeping aspect ratio /// /// the size of the element to be resized /// the target size of the element /// in case the aspect ratio of currentSize and targetSize differs: shall the scaled size fit into targetSize (i.e. that one of its dimensions is smaller - false) or vice versa (true) /// a new SizeF object indicating the width and height the element should be scaled to public static SizeF GetScaledSize(SizeF currentSize, SizeF targetSize, bool crop) { float wFactor = targetSize.Width/currentSize.Width; float hFactor = targetSize.Height/currentSize.Height; float factor = crop ? Math.Max(wFactor, hFactor) : Math.Min(wFactor, hFactor); //System.Diagnostics.Debug.WriteLine(currentSize.Width+"..."+targetSize.Width); //System.Diagnostics.Debug.WriteLine(wFactor+"..."+hFactor+">>>"+factor); return new SizeF(currentSize.Width * factor, currentSize.Height * factor); } /// /// calculates the position of an element depending on the desired alignment within a RectangleF /// /// the bounds of the element to be aligned /// the rectangle reference for aligment of the element /// the System.Drawing.ContentAlignment value indicating how the element is to be aligned should the width or height differ from targetSize /// a new RectangleF object with Location aligned aligned to targetRect public static RectangleF GetAlignedRectangle(RectangleF currentRect, RectangleF targetRect, ContentAlignment alignment) { RectangleF newRect = new RectangleF(targetRect.Location, currentRect.Size); switch(alignment) { case ContentAlignment.TopCenter: newRect.X = (targetRect.Width - currentRect.Width) / 2; break; case ContentAlignment.TopRight: newRect.X = (targetRect.Width - currentRect.Width); break; case ContentAlignment.MiddleLeft: newRect.Y = (targetRect.Height - currentRect.Height) / 2; break; case ContentAlignment.MiddleCenter: newRect.Y = (targetRect.Height - currentRect.Height) / 2; newRect.X = (targetRect.Width - currentRect.Width) / 2; break; case ContentAlignment.MiddleRight: newRect.Y = (targetRect.Height - currentRect.Height) / 2; newRect.X = (targetRect.Width - currentRect.Width); break; case ContentAlignment.BottomLeft: newRect.Y = (targetRect.Height - currentRect.Height); break; case ContentAlignment.BottomCenter: newRect.Y = (targetRect.Height - currentRect.Height); newRect.X = (targetRect.Width - currentRect.Width) / 2; break; case ContentAlignment.BottomRight: newRect.Y = (targetRect.Height - currentRect.Height); newRect.X = (targetRect.Width - currentRect.Width); break; } return newRect; } /// /// calculates the Rectangle an element must be resized an positioned to, in ordder to fit another element, keeping aspect ratio /// /// the rectangle of the element to be resized/repositioned /// the target size/position of the element /// in case the aspect ratio of currentSize and targetSize differs: shall the scaled size fit into targetSize (i.e. that one of its dimensions is smaller - false) or vice versa (true) /// the System.Drawing.ContentAlignment value indicating how the element is to be aligned should the width or height differ from targetSize /// a new RectangleF object indicating the width and height the element should be scaled to and the position that should be applied to it for proper alignment public static RectangleF GetScaledRectangle(RectangleF currentRect, RectangleF targetRect, bool crop, ContentAlignment alignment) { SizeF newSize = GetScaledSize(currentRect.Size, targetRect.Size, crop); RectangleF newRect = new RectangleF(new Point(0,0), newSize); return GetAlignedRectangle(newRect, targetRect, alignment); } } }