In this article I will explain about Constructing a Rectangle Object in GDI+.
This article has been excerpted from book "Graphics Programming with GDI+".There are several ways to create a Rectangle object. For example, you can create a Rectangle object from four integer values representing the starting point and size of the rectangle, or from Point and Size structures. Listing 6.1 creates Rectangle objects from Size, Point, and direct values. As this code shows a Rectangle constructor can take a Point and a Size object or, alternatively, the starting point (as separate variables x and y), width, and height.LISTING 6.1: Constructing Rectangle objects int x = 20; int y = 30; int height = 30; int width = 30; //Create a starting point Point pt = new Point(10, 10); //Create a size Size sz = new Size(60, 40); //Create a rectangle from a point and a size Rectangle rect1 = new Rectangle(pt, sz); Rectangle rect2 = new Rectangle(x, y, width, height);Constructing a RectangleF ObjectYou can also create a RectangleF object in several ways: from four floating point numbers with the starting and ending points and height and width of the rectangle, or from a point and a size. RectangleF is a mirror of Rectangle, including properties and methods. The only difference is that RectangleF takes floating point values. For example, instead of Size and Point, RectangleF uses SizeF and PointF. Listing 6.2 creates RectangleF object in two different ways.LISTING 6.2: Constructing RectangleF objects //Create a starting point PointF pt = new PointF(30.08f, 20.7f); //Create a Size SizeF sz = new SizeF(60.0f, 40.0f); //Create a rectangle from a point and a size RectangleF rect1 = new RectangleF(pt, sz); //Create a rectangle from floating points RectangleF rect2 = new RectangleF(40.2f, 40.6f, 100.5f, 100.0f);Rectangle Properties and MethodsThe Rectangle structure provides properties that include Bottom, Top, Left, Right, Height, Width, IsEmpty, Location, Size, X, and Y. Listing 6.3 create two rectangle (rect1 and rect2), reads these properties, and displays their values in a message box.LISTING 6.3: Using the Rectangle Structure propertiesprivate void PropertiesMenu_Click(object sender, System.EventArgs e) { //Create a point PointF pt = new PointF(30.8f, 20.7f); //Create a Size SizeF sz = new SizeF(60.0f, 40.0f); //Create rectangle from a point and //a size RectangleF rect1 = new RectangleF(pt, sz); RectangleF rect2 = new RectangleF(40.2f, 40.6f, 100.5f, 100.0f); //If rectangle is empty //set its Location, Width, and Height properties if (rect1.IsEmpty) { rect1.Location = pt; rect1.Width = sz.Width; rect1.Height = sz.Height; } //Read properties and display string str = "Location:" + rect1.Location.ToString(); str += "X:" + rect1.X.ToString() + "\n"; str += "Y:" + rect1.Y.ToString() + "\n"; str += "Left:" + rect1.Left.ToString() + "\n"; str += "Right:" + rect1.Right.ToString() + "\n"; str += "Top:" + rect1.Top.ToString() + "\n"; str += "Bottom:" + rect1.Bottom.ToString() + "\n"; MessageBox.Show(str); }The Rectangle structure provides methods that include Round, Truncate, Inflate, Ceiling, Intersect, and Union.
Object Oriented Programming Using C#