MonoMobile.Views.UIImageExtensions.ImageToFitSize C# (CSharp) Method

ImageToFitSize() public static method

public static ImageToFitSize ( this image, SizeF fitSize ) : UIImage
image this
fitSize System.Drawing.SizeF
return UIImage
		public static UIImage ImageToFitSize(this UIImage image, SizeF fitSize)
		{
			double imageScaleFactor = 1.0;
			imageScaleFactor = image.CurrentScale;
			
			double sourceWidth = image.Size.Width * imageScaleFactor;
			double sourceHeight = image.Size.Height * imageScaleFactor;
			double targetWidth = fitSize.Width;
			double targetHeight = fitSize.Height;
			
			double sourceRatio = sourceWidth / sourceHeight;
			double targetRatio = targetWidth / targetHeight;
			
			bool scaleWidth = (sourceRatio <= targetRatio);
			scaleWidth = !scaleWidth;
			
			double scalingFactor, scaledWidth, scaledHeight;
			
			if (scaleWidth)
			{
				scalingFactor = 1.0 / sourceRatio;
				scaledWidth = targetWidth;
				scaledHeight = Math.Round(targetWidth * scalingFactor);
			} 
			else
			{
				scalingFactor = sourceRatio;
				scaledWidth = Math.Round(targetHeight * scalingFactor);
				scaledHeight = targetHeight;
			}
			
			RectangleF destRect = new RectangleF(0, 0, (float)scaledWidth, (float)scaledHeight);
			
			UIGraphics.BeginImageContextWithOptions(destRect.Size, false, 0.0f);
			image.Draw(destRect);
			UIImage newImage = UIGraphics.GetImageFromCurrentImageContext();
			UIGraphics.EndImageContext();
			
			return newImage;
		}
	}

Same methods

UIImageExtensions::ImageToFitSize ( this image, float width, float height ) : UIImage