How to scale a Size Object by percentage in C# and VB.NET

To scale a Size Object by percentage in C# and VB.NET you can use the following snippet.

Sample C#

public static Size Scale(this Size size, float scalePercent)
{
	if (Math.Abs(scalePercent - 1) < float.Epsilon)
	{
		return size;
	}

	var height = (int) (size.Height*(scalePercent/100));
	var width = (int)(size.Width * (scalePercent / 100));

	return new Size(width, height);
}

Sample VB.NET

<System.Runtime.CompilerServices.Extension> _
Public Shared Function Scale(size As Size, scalePercent As Single) As Size
	If Math.Abs(scalePercent - 1) < Single.Epsilon Then
		Return size
	End If

	Dim height = CInt(size.Height * (scalePercent / 100))
	Dim width = CInt(size.Width * (scalePercent / 100))

	Return New Size(width, height)
End Function

Please Share us:
Please rate this snippet

One thought on “How to scale a Size Object by percentage in C# and VB.NET

Leave a Reply