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 #if !MONOTOUCH && !MONOMAC
44 [TypeConverter (typeof (SizeConverter))]
49 // Private Height and width fields.
50 private int width, height;
52 // -----------------------
53 // Public Shared Members
54 // -----------------------
57 /// Empty Shared Field
61 /// An uninitialized Size Structure.
64 public static readonly Size Empty;
67 /// Ceiling Shared Method
71 /// Produces a Size structure from a SizeF structure by
72 /// taking the ceiling of the Width and Height properties.
75 public static Size Ceiling (SizeF value)
79 w = (int) Math.Ceiling (value.Width);
80 h = (int) Math.Ceiling (value.Height);
83 return new Size (w, h);
87 /// Round Shared Method
91 /// Produces a Size structure from a SizeF structure by
92 /// rounding the Width and Height properties.
95 public static Size Round (SizeF value)
99 w = (int) Math.Round (value.Width);
100 h = (int) Math.Round (value.Height);
103 return new Size (w, h);
107 /// Truncate Shared Method
111 /// Produces a Size structure from a SizeF structure by
112 /// truncating the Width and Height properties.
115 public static Size Truncate (SizeF value)
119 w = (int) value.Width;
120 h = (int) value.Height;
123 return new Size (w, h);
127 /// Addition Operator
131 /// Addition of two Size structures.
134 public static Size operator + (Size sz1, Size sz2)
136 return new Size (sz1.Width + sz2.Width,
137 sz1.Height + sz2.Height);
141 /// Equality Operator
145 /// Compares two Size objects. The return value is
146 /// based on the equivalence of the Width and Height
147 /// properties of the two Sizes.
150 public static bool operator == (Size sz1, Size sz2)
152 return ((sz1.Width == sz2.Width) &&
153 (sz1.Height == sz2.Height));
157 /// Inequality Operator
161 /// Compares two Size objects. The return value is
162 /// based on the equivalence of the Width and Height
163 /// properties of the two Sizes.
166 public static bool operator != (Size sz1, Size sz2)
168 return ((sz1.Width != sz2.Width) ||
169 (sz1.Height != sz2.Height));
173 /// Subtraction Operator
177 /// Subtracts two Size structures.
180 public static Size operator - (Size sz1, Size sz2)
182 return new Size (sz1.Width - sz2.Width,
183 sz1.Height - sz2.Height);
187 /// Size to Point Conversion
191 /// Returns a Point based on the dimensions of a given
192 /// Size. Requires explicit cast.
195 public static explicit operator Point (Size size)
197 return new Point (size.Width, size.Height);
201 /// Size to SizeF Conversion
205 /// Creates a SizeF based on the dimensions of a given
206 /// Size. No explicit cast is required.
209 public static implicit operator SizeF (Size p)
211 return new SizeF (p.Width, p.Height);
215 // -----------------------
216 // Public Constructors
217 // -----------------------
224 /// Creates a Size from a Point value.
227 public Size (Point pt)
238 /// Creates a Size from specified dimensions.
241 public Size (int width, int height)
244 this.height = height;
247 // -----------------------
248 // Public Instance Members
249 // -----------------------
256 /// Indicates if both Width and Height are zero.
260 public bool IsEmpty {
262 return ((width == 0) && (height == 0));
271 /// The Width coordinate of the Size.
288 /// The Height coordinate of the Size.
305 /// Checks equivalence of this Size and another object.
308 public override bool Equals (object obj)
313 return (this == (Size) obj);
317 /// GetHashCode Method
321 /// Calculates a hashing value.
324 public override int GetHashCode ()
334 /// Formats the Size as a string in coordinate notation.
337 public override string ToString ()
339 return String.Format ("{{Width={0}, Height={1}}}", width, height);
342 public static Size Add (Size sz1, Size sz2)
344 return new Size (sz1.Width + sz2.Width,
345 sz1.Height + sz2.Height);
349 public static Size Subtract (Size sz1, Size sz2)
351 return new Size (sz1.Width - sz2.Width,
352 sz1.Height - sz2.Height);