TheDeveloperBlog.com

Home | Contact Us

CSharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript

C# Size, SizeF Structs

This C# article covers the Size type from the System.Drawing namespace. It provides example code.

Size, SizeF. How can you create, read and modify the Size struct?

The Size struct is found in the System.Drawing namespace. We set the Width and Height properties on Size. We also see if it is empty with IsEmpty.

Struct

Size. First, any Size instance that has zero height and zero width is empty: IsEmpty will return true. You can get or set the Width and Height properties on Size at any time. You can initialize a Size with no arguments—it will be empty.

True, False

Also: You can use two arguments (width and height) or a Point (which stores width and height).

Tip: If the System.Drawing namespace is not found, add a reference to the System.Drawing.dll file.

C# program that uses Size struct

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	Size size = new Size();
	Console.WriteLine(size.IsEmpty);
	Console.WriteLine(size);
	Console.WriteLine(size.Width);
	Console.WriteLine(size.Height);
	size.Width = 1;
	Console.WriteLine(size);

	size = new Size(5, 10);
	Console.WriteLine(size.IsEmpty);
	Console.WriteLine(size);

	Point point = new Point(5, 10);
	size = new Size(point);
	Console.WriteLine(size.IsEmpty);
	Console.WriteLine(size);

	size = new Size(-1, -1);
	Console.WriteLine(size.IsEmpty);
	Console.WriteLine(size);
    }
}

Output

True
{Width=0, Height=0}
0
0
{Width=1, Height=0}
False
{Width=5, Height=10}
False
{Width=5, Height=10}
False
{Width=-1, Height=-1}

Operators. With IL Disassembler, I looked through the System.Drawing.dll file and the Size type. The type implements addition and subtraction operators. Addition adds both widths and both heights. Subtraction takes the difference.

IL Disassembler Tutorial

Note: There also exists the Size.Add and Size.Subtract static methods. These are not shown in this program.

C# program that uses Size addition and subtraction

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	Size a = new Size(5, 5);
	Size b = new Size(3, 2);

	Size c = a + b;
	Size d = a - b;

	Console.WriteLine(c);
	Console.WriteLine(d);
    }
}

Output

{Width=8, Height=7}
{Width=2, Height=3}

Convert. Next we examine ways to convert Size structs. SizeF stands for Size Float, and the Size type has several conversion methods available on it. You will need to choose between Ceiling, Truncate, and Round.

Note: Ceiling raises to the next integer.
Truncate removes everything past the decimal.
Round rounds up or down.

C# program that converts from SizeF

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	SizeF sizeF = new SizeF(4.5f, 5.6f);
	Size ceiling = Size.Ceiling(sizeF);
	Size truncate = Size.Truncate(sizeF);
	Size round = Size.Round(sizeF);

	Console.WriteLine(sizeF);
	Console.WriteLine(ceiling);
	Console.WriteLine(truncate);
	Console.WriteLine(round);
    }
}

Output

{Width=4.5, Height=5.6}
{Width=5, Height=6}
{Width=4, Height=5}
{Width=4, Height=6}

SizeF provides two floating point values: width and height. There are several constructors to the SizeF struct. If you use the parameterless constructor, you get an empty SizeF. You can also specify the width and height as a PointF.

PointF

C# program that uses SizeF

using System;
using System.Drawing;

class Program
{
    static void Main()
    {
	SizeF sizeF = new SizeF();
	Display(sizeF);

	PointF pointF = new PointF(4.5f, 6.5f);
	sizeF = new SizeF(pointF);
	Display(sizeF);

	sizeF = new SizeF(sizeF);
	Display(sizeF);

	sizeF = new SizeF(6.5f, 10.5f);
	Display(sizeF);
    }

    static void Display(SizeF sizeF)
    {
	Console.WriteLine(sizeF);
	Console.WriteLine(sizeF.Width);
	Console.WriteLine(sizeF.Height);
	Console.WriteLine(sizeF.IsEmpty);
    }
}

Output

{Width=0, Height=0}
0
0
True
{Width=4.5, Height=6.5}
4.5
6.5
False
{Width=4.5, Height=6.5}
4.5
6.5
False
{Width=6.5, Height=10.5}
6.5
10.5
False

Summary. The Size struct stores a width and a height in int fields. The useful part of Size, though, is that it has properties, methods, static methods, and operators that can simplify the logic in programs that handle size information.