2 // System.Drawing.Size.cs
5 // Mike Kestner (mkestner@speakeasy.net)
7 // Copyright (C) 2001 Mike Kestner
8 // Copyright (C) 2004 Novell, Inc. http://www.novell.com
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Runtime.Serialization;
36 using System.Runtime.InteropServices;
37 using System.ComponentModel;
39 namespace System.Drawing
43 [TypeConverter (typeof (SizeConverter))]
47 // Private Height and width fields.
48 private int width, height;
50 // -----------------------
51 // Public Shared Members
52 // -----------------------
55 /// Empty Shared Field
59 /// An uninitialized Size Structure.
62 public static readonly Size Empty;
65 /// Ceiling Shared Method
69 /// Produces a Size structure from a SizeF structure by
70 /// taking the ceiling of the Width and Height properties.
73 public static Size Ceiling (SizeF value)
77 w = (int) Math.Ceiling (value.Width);
78 h = (int) Math.Ceiling (value.Height);
81 return new Size (w, h);
85 /// Round Shared Method
89 /// Produces a Size structure from a SizeF structure by
90 /// rounding the Width and Height properties.
93 public static Size Round (SizeF value)
97 w = (int) Math.Round (value.Width);
98 h = (int) Math.Round (value.Height);
101 return new Size (w, h);
105 /// Truncate Shared Method
109 /// Produces a Size structure from a SizeF structure by
110 /// truncating the Width and Height properties.
113 public static Size Truncate (SizeF value)
117 w = (int) value.Width;
118 h = (int) value.Height;
121 return new Size (w, h);
125 /// Addition Operator
129 /// Addition of two Size structures.
132 public static Size operator + (Size sz1, Size sz2)
134 return new Size (sz1.Width + sz2.Width,
135 sz1.Height + sz2.Height);
139 /// Equality Operator
143 /// Compares two Size objects. The return value is
144 /// based on the equivalence of the Width and Height
145 /// properties of the two Sizes.
148 public static bool operator == (Size sz1, Size sz2)
150 return ((sz1.Width == sz2.Width) &&
151 (sz1.Height == sz2.Height));
155 /// Inequality Operator
159 /// Compares two Size objects. The return value is
160 /// based on the equivalence of the Width and Height
161 /// properties of the two Sizes.
164 public static bool operator != (Size sz1, Size sz2)
166 return ((sz1.Width != sz2.Width) ||
167 (sz1.Height != sz2.Height));
171 /// Subtraction Operator
175 /// Subtracts two Size structures.
178 public static Size operator - (Size sz1, Size sz2)
180 return new Size (sz1.Width - sz2.Width,
181 sz1.Height - sz2.Height);
185 /// Size to Point Conversion
189 /// Returns a Point based on the dimensions of a given
190 /// Size. Requires explicit cast.
193 public static explicit operator Point (Size size)
195 return new Point (size.Width, size.Height);
199 /// Size to SizeF Conversion
203 /// Creates a SizeF based on the dimensions of a given
204 /// Size. No explicit cast is required.
207 public static implicit operator SizeF (Size p)
209 return new SizeF (p.Width, p.Height);
213 // -----------------------
214 // Public Constructors
215 // -----------------------
222 /// Creates a Size from a Point value.
225 public Size (Point pt)
236 /// Creates a Size from specified dimensions.
239 public Size (int width, int height)
242 this.height = height;
245 // -----------------------
246 // Public Instance Members
247 // -----------------------
254 /// Indicates if both Width and Height are zero.
258 public bool IsEmpty {
260 return ((width == 0) && (height == 0));
269 /// The Width coordinate of the Size.
286 /// The Height coordinate of the Size.
303 /// Checks equivalence of this Size and another object.
306 public override bool Equals (object obj)
311 return (this == (Size) obj);
315 /// GetHashCode Method
319 /// Calculates a hashing value.
322 public override int GetHashCode ()
332 /// Formats the Size as a string in coordinate notation.
335 public override string ToString ()
337 return String.Format ("{{Width={0}, Height={1}}}", width, height);
340 public static Size Add (Size sz1, Size sz2)
342 return new Size (sz1.Width + sz2.Width,
343 sz1.Height + sz2.Height);
347 public static Size Subtract (Size sz1, Size sz2)
349 return new Size (sz1.Width - sz2.Width,
350 sz1.Height - sz2.Height);