How to scale a Size variable in C# or VB.NET

To scale a Size variable in C# or VB.NET you can use the following Extension Method.

Sample C#

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

	var height = (int)(size.Height * scaleInPercent);
	var width = (int)(size.Width * scaleInPercent);

	return new Size(width, height);
}

Sample VB.NET

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

	Dim height = CInt(size.Height * scaleInPercent)
	Dim width = CInt(size.Width * scaleInPercent)

	Return New Size(width, height)
End Function

One thought on “How to scale a Size variable in C# or VB.NET

Leave a Reply