2 // System.Drawing.Size.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // (C) 2001 Mike Kestner
11 using System.Runtime.Serialization;
13 namespace System.Drawing {
18 // Private Height and width fields.
21 // -----------------------
22 // Public Shared Members
23 // -----------------------
26 /// Empty Shared Field
30 /// An uninitialized Size Structure.
33 public static readonly Size Empty;
36 /// Ceiling Shared Method
40 /// Produces a Size structure from a SizeF structure by
41 /// taking the ceiling of the Width and Height properties.
44 public static Size Ceiling (SizeF value)
48 w = (int) Math.Ceiling (value.Width);
49 h = (int) Math.Ceiling (value.Height);
52 return new Size (w, h);
56 /// Round Shared Method
60 /// Produces a Size structure from a SizeF structure by
61 /// rounding the Width and Height properties.
64 public static Size Round (SizeF value)
68 w = (int) Math.Round (value.Width);
69 h = (int) Math.Round (value.Height);
72 return new Size (w, h);
76 /// Truncate Shared Method
80 /// Produces a Size structure from a SizeF structure by
81 /// truncating the Width and Height properties.
84 public static Size Truncate (SizeF value)
88 w = (int) value.Width;
89 h = (int) value.Height;
92 return new Size (w, h);
100 /// Addition of two Size structures.
103 public static Size operator + (Size sz1, Size sz2)
105 return new Size (sz1.Width + sz2.Width,
106 sz1.Height + sz2.Height);
110 /// Equality Operator
114 /// Compares two Size objects. The return value is
115 /// based on the equivalence of the Width and Height
116 /// properties of the two Sizes.
119 public static bool operator == (Size sz_a, Size sz_b)
121 return ((sz_a.Width == sz_b.Width) &&
122 (sz_a.Height == sz_b.Height));
126 /// Inequality Operator
130 /// Compares two Size objects. The return value is
131 /// based on the equivalence of the Width and Height
132 /// properties of the two Sizes.
135 public static bool operator != (Size sz_a, Size sz_b)
137 return ((sz_a.Width != sz_b.Width) ||
138 (sz_a.Height != sz_b.Height));
142 /// Subtraction Operator
146 /// Subtracts two Size structures.
149 public static Size operator - (Size sz1, Size sz2)
151 return new Size (sz1.Width - sz2.Width,
152 sz1.Height - sz2.Height);
156 /// Size to Point Conversion
160 /// Returns a Point based on the dimensions of a given
161 /// Size. Requires explicit cast.
164 public static explicit operator Point (Size sz)
166 return new Point (sz.Width, sz.Height);
170 /// Size to SizeF Conversion
174 /// Creates a SizeF based on the dimensions of a given
175 /// Size. No explicit cast is required.
178 public static implicit operator SizeF (Size sz)
180 return new SizeF (sz.Width, sz.Height);
184 // -----------------------
185 // Public Constructors
186 // -----------------------
193 /// Creates a Size from a Point value.
196 public Size (Point pt)
207 /// Creates a Size from specified dimensions.
210 public Size (int width, int height)
213 this.height = height;
216 // -----------------------
217 // Public Instance Members
218 // -----------------------
225 /// Indicates if both Width and Height are zero.
228 public bool IsEmpty {
230 return ((Width == 0) && (Height == 0));
239 /// The Width coordinate of the Size.
256 /// The Height coordinate of the Size.
273 /// Checks equivalence of this Size and another object.
276 public override bool Equals (object o)
281 return (this == (Size) o);
285 /// GetHashCode Method
289 /// Calculates a hashing value.
292 public override int GetHashCode ()
302 /// Formats the Size as a string in coordinate notation.
305 public override string ToString ()
307 return String.Format ("[{0},{1}]", Width, Height);