2008-12-23 Sebastien Pouliot <sebastien@ximian.com>
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestGraphics.cs
1 //
2 // Graphics class testing unit
3 //
4 // Authors:
5 //   Jordi Mas, jordi@ximian.com
6 //   Sebastien Pouliot  <sebastien@ximian.com>
7 //
8 // Copyright (C) 2005-2008 Novell, Inc (http://www.novell.com)
9 //
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
17 //
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
20 //
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 //
29
30 using NUnit.Framework;
31 using System;
32 using System.Drawing;
33 using System.Drawing.Drawing2D;
34 using System.Drawing.Imaging;
35 using System.Drawing.Text;
36 using System.IO;
37 using System.Reflection;
38 using System.Security.Permissions;
39
40 namespace MonoTests.System.Drawing {
41
42         [TestFixture]
43         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
44         public class GraphicsTest {
45
46                 private RectangleF[] rects;
47                 private Font font;
48
49                 [TestFixtureSetUp]
50                 public void FixtureSetUp ()
51                 {
52                         try {
53                                 font = new Font ("Arial", 12);
54                         }
55                         catch {
56                         }
57                 }
58
59                 [TestFixtureTearDown]
60                 public void FixtureTearDown ()
61                 {
62                         if (font != null)
63                                 font.Dispose ();
64                 }
65
66
67                 private bool IsEmptyBitmap (Bitmap bitmap, out int x, out int y)
68                 {
69                         bool result = true;
70                         int empty = Color.Empty.ToArgb ();
71 #if false
72                         for (y = 0; y < bitmap.Height; y++) {
73                                 for (x = 0; x < bitmap.Width; x++) {
74                                         if (bitmap.GetPixel (x, y).ToArgb () != empty) {
75                                                 Console.Write ("X");
76                                                 result = false;
77                                         } else
78                                                 Console.Write (" ");
79                                 }
80                                 Console.WriteLine ();
81                         }
82 #else
83                         for (y = 0; y < bitmap.Height; y++) {
84                                 for (x = 0; x < bitmap.Width; x++) {
85                                         if (bitmap.GetPixel (x, y).ToArgb () != empty)
86                                                 return false;
87                                 }
88                         }
89 #endif
90                         x = -1;
91                         y = -1;
92                         return result;
93                 }
94
95                 private void CheckForEmptyBitmap (Bitmap bitmap)
96                 {
97                         int x, y;
98                         if (!IsEmptyBitmap (bitmap, out x, out y))
99                                 Assert.Fail (String.Format ("Position {0},{1}", x, y));
100                 }
101
102                 private void CheckForNonEmptyBitmap (Bitmap bitmap)
103                 {
104                         int x, y;
105                         if (IsEmptyBitmap (bitmap, out x, out y))
106                                 Assert.Fail ("Bitmap was empty");
107                 }
108
109                 private void AssertEquals (string msg, object expected, object actual)
110                 {
111                         Assert.AreEqual (expected, actual, msg);
112                 }
113
114                 private void AssertEquals (string msg, double expected, double actual, double delta)
115                 {
116                         Assert.AreEqual (expected, actual, delta, msg);
117                 }
118
119                 [Test]
120                 public void DefaultProperties ()
121                 {
122                         Bitmap bmp = new Bitmap (200, 200);
123                         Graphics g = Graphics.FromImage (bmp);
124                         Region r = new Region ();
125
126                         AssertEquals ("DefaultProperties1", r.GetBounds (g), g.ClipBounds);
127                         AssertEquals ("DefaultProperties2", CompositingMode.SourceOver, g.CompositingMode);
128                         AssertEquals ("DefaultProperties3", CompositingQuality.Default, g.CompositingQuality);
129                         AssertEquals ("DefaultProperties4", InterpolationMode.Bilinear, g.InterpolationMode);
130                         AssertEquals ("DefaultProperties5", 1, g.PageScale);
131                         AssertEquals ("DefaultProperties6", GraphicsUnit.Display, g.PageUnit);
132                         AssertEquals ("DefaultProperties7", PixelOffsetMode.Default, g.PixelOffsetMode);
133                         AssertEquals ("DefaultProperties8", new Point (0, 0), g.RenderingOrigin);
134                         AssertEquals ("DefaultProperties9", SmoothingMode.None, g.SmoothingMode);
135                         AssertEquals ("DefaultProperties10", TextRenderingHint.SystemDefault, g.TextRenderingHint);
136
137                         r.Dispose ();
138                 }
139
140                 [Test]
141                 public void SetGetProperties ()
142                 {
143                         Bitmap bmp = new Bitmap (200, 200);
144                         Graphics g = Graphics.FromImage (bmp);
145
146                         g.CompositingMode = CompositingMode.SourceCopy;
147                         g.CompositingQuality = CompositingQuality.GammaCorrected;
148                         g.InterpolationMode = InterpolationMode.HighQualityBilinear;
149                         g.PageScale = 2;
150                         g.PageUnit = GraphicsUnit.Inch;
151                         g.PixelOffsetMode = PixelOffsetMode.Half;
152                         g.RenderingOrigin = new Point (10, 20);
153                         g.SmoothingMode = SmoothingMode.AntiAlias;
154                         g.TextRenderingHint = TextRenderingHint.SystemDefault;
155
156                         //Clipping set/get tested in clipping functions                 
157                         AssertEquals ("SetGetProperties2", CompositingMode.SourceCopy, g.CompositingMode);
158                         AssertEquals ("SetGetProperties3", CompositingQuality.GammaCorrected, g.CompositingQuality);
159                         AssertEquals ("SetGetProperties4", InterpolationMode.HighQualityBilinear, g.InterpolationMode);
160                         AssertEquals ("SetGetProperties5", 2, g.PageScale);
161                         AssertEquals ("SetGetProperties6", GraphicsUnit.Inch, g.PageUnit);
162                         AssertEquals ("SetGetProperties7", PixelOffsetMode.Half, g.PixelOffsetMode);
163                         AssertEquals ("SetGetProperties8", new Point (10, 20), g.RenderingOrigin);
164                         AssertEquals ("SetGetProperties9", SmoothingMode.AntiAlias, g.SmoothingMode);
165                         AssertEquals ("SetGetProperties10", TextRenderingHint.SystemDefault, g.TextRenderingHint);
166                 }
167
168                 // Properties
169                 [Test]
170                 public void Clip ()
171                 {
172                         RectangleF[] rects;
173                         Bitmap bmp = new Bitmap (200, 200);
174                         Graphics g = Graphics.FromImage (bmp);
175                         g.Clip = new Region (new Rectangle (50, 40, 210, 220));
176                         rects = g.Clip.GetRegionScans (new Matrix ());
177
178                         AssertEquals ("Clip1", 1, rects.Length);
179                         AssertEquals ("Clip2", 50, rects[0].X);
180                         AssertEquals ("Clip3", 40, rects[0].Y);
181                         AssertEquals ("Clip4", 210, rects[0].Width);
182                         AssertEquals ("Clip5", 220, rects[0].Height);
183                 }
184
185                 [Test]
186                 public void Clip_NotAReference ()
187                 {
188                         Bitmap bmp = new Bitmap (200, 200);
189                         Graphics g = Graphics.FromImage (bmp);
190                         Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite");
191                         g.Clip.IsEmpty (g);
192                         Assert.IsFalse (g.Clip.IsEmpty (g), "!IsEmpty");
193                         Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite-2");
194                 }
195
196                 [Test]
197                 public void ExcludeClip ()
198                 {
199                         Bitmap bmp = new Bitmap (200, 200);
200                         Graphics g = Graphics.FromImage (bmp);
201
202                         g.Clip = new Region (new RectangleF (10, 10, 100, 100));
203                         g.ExcludeClip (new Rectangle (40, 60, 100, 20));
204                         rects = g.Clip.GetRegionScans (new Matrix ());
205
206                         AssertEquals ("ExcludeClip1", 3, rects.Length);
207
208                         AssertEquals ("ExcludeClip2", 10, rects[0].X);
209                         AssertEquals ("ExcludeClip3", 10, rects[0].Y);
210                         AssertEquals ("ExcludeClip4", 100, rects[0].Width);
211                         AssertEquals ("ExcludeClip5", 50, rects[0].Height);
212
213                         AssertEquals ("ExcludeClip6", 10, rects[1].X);
214                         AssertEquals ("ExcludeClip7", 60, rects[1].Y);
215                         AssertEquals ("ExcludeClip8", 30, rects[1].Width);
216                         AssertEquals ("ExcludeClip9", 20, rects[1].Height);
217
218                         AssertEquals ("ExcludeClip10", 10, rects[2].X);
219                         AssertEquals ("ExcludeClip11", 80, rects[2].Y);
220                         AssertEquals ("ExcludeClip12", 100, rects[2].Width);
221                         AssertEquals ("ExcludeClip13", 30, rects[2].Height);
222                 }
223
224                 [Test]
225                 public void IntersectClip ()
226                 {
227                         Bitmap bmp = new Bitmap (200, 200);
228                         Graphics g = Graphics.FromImage (bmp);
229
230                         g.Clip = new Region (new RectangleF (260, 30, 60, 80));
231                         g.IntersectClip (new Rectangle (290, 40, 60, 80));
232                         rects = g.Clip.GetRegionScans (new Matrix ());
233
234                         AssertEquals ("IntersectClip", 1, rects.Length);
235
236                         AssertEquals ("IntersectClip", 290, rects[0].X);
237                         AssertEquals ("IntersectClip", 40, rects[0].Y);
238                         AssertEquals ("IntersectClip", 30, rects[0].Width);
239                         AssertEquals ("IntersectClip", 70, rects[0].Height);
240                 }
241
242                 [Test]
243                 public void ResetClip ()
244                 {
245                         Bitmap bmp = new Bitmap (200, 200);
246                         Graphics g = Graphics.FromImage (bmp);
247
248                         g.Clip = new Region (new RectangleF (260, 30, 60, 80));
249                         g.IntersectClip (new Rectangle (290, 40, 60, 80));
250                         g.ResetClip ();
251                         rects = g.Clip.GetRegionScans (new Matrix ());
252
253                         AssertEquals ("ResetClip", 1, rects.Length);
254
255                         AssertEquals ("ResetClip", -4194304, rects[0].X);
256                         AssertEquals ("ResetClip", -4194304, rects[0].Y);
257                         AssertEquals ("ResetClip", 8388608, rects[0].Width);
258                         AssertEquals ("ResetClip", 8388608, rects[0].Height);
259                 }
260
261                 [Test]
262                 public void SetClip ()
263                 {
264                         RectangleF[] rects;
265                         Bitmap bmp = new Bitmap (200, 200);
266                         Graphics g = Graphics.FromImage (bmp);
267
268                         // Region
269                         g.SetClip (new Region (new Rectangle (50, 40, 210, 220)), CombineMode.Replace);
270                         rects = g.Clip.GetRegionScans (new Matrix ());
271                         AssertEquals ("SetClip1", 1, rects.Length);
272                         AssertEquals ("SetClip2", 50, rects[0].X);
273                         AssertEquals ("SetClip3", 40, rects[0].Y);
274                         AssertEquals ("SetClip4", 210, rects[0].Width);
275                         AssertEquals ("SetClip5", 220, rects[0].Height);
276
277                         // RectangleF
278                         g = Graphics.FromImage (bmp);
279                         g.SetClip (new RectangleF (50, 40, 210, 220));
280                         rects = g.Clip.GetRegionScans (new Matrix ());
281                         AssertEquals ("SetClip6", 1, rects.Length);
282                         AssertEquals ("SetClip7", 50, rects[0].X);
283                         AssertEquals ("SetClip8", 40, rects[0].Y);
284                         AssertEquals ("SetClip9", 210, rects[0].Width);
285                         AssertEquals ("SetClip10", 220, rects[0].Height);
286
287                         // Rectangle
288                         g = Graphics.FromImage (bmp);
289                         g.SetClip (new Rectangle (50, 40, 210, 220));
290                         rects = g.Clip.GetRegionScans (new Matrix ());
291                         AssertEquals ("SetClip10", 1, rects.Length);
292                         AssertEquals ("SetClip11", 50, rects[0].X);
293                         AssertEquals ("SetClip12", 40, rects[0].Y);
294                         AssertEquals ("SetClip13", 210, rects[0].Width);
295                         AssertEquals ("SetClip14", 220, rects[0].Height);
296                 }
297
298                 [Test]
299                 public void SetSaveReset ()
300                 {
301                         Bitmap bmp = new Bitmap (200, 200);
302                         Graphics g = Graphics.FromImage (bmp);
303                         GraphicsState state_default, state_modified;
304
305                         state_default = g.Save (); // Default
306
307                         g.CompositingMode = CompositingMode.SourceCopy;
308                         g.CompositingQuality = CompositingQuality.GammaCorrected;
309                         g.InterpolationMode = InterpolationMode.HighQualityBilinear;
310                         g.PageScale = 2;
311                         g.PageUnit = GraphicsUnit.Inch;
312                         g.PixelOffsetMode = PixelOffsetMode.Half;
313                         g.Clip = new Region (new Rectangle (0, 0, 100, 100));
314                         g.RenderingOrigin = new Point (10, 20);
315                         g.SmoothingMode = SmoothingMode.AntiAlias;
316                         g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
317
318
319                         state_modified = g.Save (); // Modified
320
321                         g.CompositingMode = CompositingMode.SourceOver;
322                         g.CompositingQuality = CompositingQuality.Default;
323                         g.InterpolationMode = InterpolationMode.Bilinear;
324                         g.PageScale = 5;
325                         g.PageUnit = GraphicsUnit.Display;
326                         g.PixelOffsetMode = PixelOffsetMode.Default;
327                         g.Clip = new Region (new Rectangle (1, 2, 20, 25));
328                         g.RenderingOrigin = new Point (5, 6);
329                         g.SmoothingMode = SmoothingMode.None;
330                         g.TextRenderingHint = TextRenderingHint.SystemDefault;
331
332                         g.Restore (state_modified);
333
334                         AssertEquals ("SetSaveReset1", CompositingMode.SourceCopy, g.CompositingMode);
335                         AssertEquals ("SetSaveReset2", CompositingQuality.GammaCorrected, g.CompositingQuality);
336                         AssertEquals ("SetSaveReset3", InterpolationMode.HighQualityBilinear, g.InterpolationMode);
337                         AssertEquals ("SetSaveReset4", 2, g.PageScale);
338                         AssertEquals ("SetSaveReset5", GraphicsUnit.Inch, g.PageUnit);
339                         AssertEquals ("SetSaveReset6", PixelOffsetMode.Half, g.PixelOffsetMode);
340                         AssertEquals ("SetSaveReset7", new Point (10, 20), g.RenderingOrigin);
341                         AssertEquals ("SetSaveReset8", SmoothingMode.AntiAlias, g.SmoothingMode);
342                         AssertEquals ("SetSaveReset9", TextRenderingHint.ClearTypeGridFit, g.TextRenderingHint);
343                         AssertEquals ("SetSaveReset10", 0, (int) g.ClipBounds.X);
344                         AssertEquals ("SetSaveReset10", 0, (int) g.ClipBounds.Y);
345
346                         g.Restore (state_default);
347
348                         AssertEquals ("SetSaveReset11", CompositingMode.SourceOver, g.CompositingMode);
349                         AssertEquals ("SetSaveReset12", CompositingQuality.Default, g.CompositingQuality);
350                         AssertEquals ("SetSaveReset13", InterpolationMode.Bilinear, g.InterpolationMode);
351                         AssertEquals ("SetSaveReset14", 1, g.PageScale);
352                         AssertEquals ("SetSaveReset15", GraphicsUnit.Display, g.PageUnit);
353                         AssertEquals ("SetSaveReset16", PixelOffsetMode.Default, g.PixelOffsetMode);
354                         AssertEquals ("SetSaveReset17", new Point (0, 0), g.RenderingOrigin);
355                         AssertEquals ("SetSaveReset18", SmoothingMode.None, g.SmoothingMode);
356                         AssertEquals ("SetSaveReset19", TextRenderingHint.SystemDefault, g.TextRenderingHint);
357
358                         Region r = new Region ();
359                         AssertEquals ("SetSaveReset20", r.GetBounds (g), g.ClipBounds);
360
361                         g.Dispose ();
362                 }
363
364                 [Test]
365                 [Category ("NotWorking")] // looks like MS PNG codec promote indexed format to 32bpp ARGB
366                 public void LoadIndexed_PngStream ()
367                 {
368                         // Tests that we can load an indexed file
369                         using (Stream s = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("indexed.png")) {
370                                 using (Image img = Image.FromStream (s)) {
371                                         // however it's no more indexed once loaded
372                                         Assert.AreEqual (PixelFormat.Format32bppArgb, img.PixelFormat, "PixelFormat");
373                                         using (Graphics g = Graphics.FromImage (img)) {
374                                                 Assert.AreEqual (img.Height, g.VisibleClipBounds.Height, "Height");
375                                                 Assert.AreEqual (img.Width, g.VisibleClipBounds.Width, "Width");
376                                         }
377                                 }
378                         }
379                 }
380
381                 [Test]
382                 [ExpectedException (typeof (Exception))]
383                 public void LoadIndexed_BmpFile ()
384                 {
385                         // Tests that we can load an indexed file, but...
386                         string sInFile = TestBitmap.getInFile ("bitmaps/almogaver1bit.bmp");
387                         // note: file is misnamed (it's a 4bpp bitmap)
388                         using (Image img = Image.FromFile (sInFile)) {
389                                 Assert.AreEqual (PixelFormat.Format4bppIndexed, img.PixelFormat, "PixelFormat");
390                                 Graphics.FromImage (img);
391                         }
392                 }
393
394                 [Test]
395                 [ExpectedException (typeof (ArgumentNullException))]
396                 public void FromImage ()
397                 {
398                         Graphics g = Graphics.FromImage (null);
399                 }
400
401                 private Graphics Get (int w, int h)
402                 {
403                         Bitmap bitmap = new Bitmap (w, h);
404                         Graphics g = Graphics.FromImage (bitmap);
405                         g.Clip = new Region (new Rectangle (0, 0, w, h));
406                         return g;
407                 }
408
409                 private void Compare (string msg, RectangleF b1, RectangleF b2)
410                 {
411                         AssertEquals (msg + ".compare.X", b1.X, b2.X);
412                         AssertEquals (msg + ".compare.Y", b1.Y, b2.Y);
413                         AssertEquals (msg + ".compare.Width", b1.Width, b2.Width);
414                         AssertEquals (msg + ".compare.Height", b1.Height, b2.Height);
415                 }
416
417                 [Test]
418                 public void Clip_GetBounds ()
419                 {
420                         Graphics g = Get (16, 16);
421                         RectangleF bounds = g.Clip.GetBounds (g);
422                         AssertEquals ("X", 0, bounds.X);
423                         AssertEquals ("Y", 0, bounds.Y);
424                         AssertEquals ("Width", 16, bounds.Width);
425                         AssertEquals ("Height", 16, bounds.Height);
426                         Assert.IsTrue (g.Transform.IsIdentity, "Identity");
427                         g.Dispose ();
428                 }
429
430                 [Test]
431                 public void Clip_TranslateTransform ()
432                 {
433                         Graphics g = Get (16, 16);
434                         g.TranslateTransform (12.22f, 10.10f);
435                         RectangleF bounds = g.Clip.GetBounds (g);
436                         Compare ("translate", bounds, g.ClipBounds);
437                         AssertEquals ("translate.X", -12.2200003f, bounds.X);
438                         AssertEquals ("translate.Y", -10.1000004f, bounds.Y);
439                         AssertEquals ("translate.Width", 16, bounds.Width);
440                         AssertEquals ("translate.Height", 16, bounds.Height);
441                         float[] elements = g.Transform.Elements;
442                         AssertEquals ("translate.0", 1, elements[0]);
443                         AssertEquals ("translate.1", 0, elements[1]);
444                         AssertEquals ("translate.2", 0, elements[2]);
445                         AssertEquals ("translate.3", 1, elements[3]);
446                         AssertEquals ("translate.4", 12.2200003f, elements[4]);
447                         AssertEquals ("translate.5", 10.1000004f, elements[5]);
448
449                         g.ResetTransform ();
450                         bounds = g.Clip.GetBounds (g);
451                         Compare ("reset", bounds, g.ClipBounds);
452                         AssertEquals ("reset.X", 0, bounds.X);
453                         AssertEquals ("reset.Y", 0, bounds.Y);
454                         AssertEquals ("reset.Width", 16, bounds.Width);
455                         AssertEquals ("reset.Height", 16, bounds.Height);
456                         Assert.IsTrue (g.Transform.IsIdentity, "Identity");
457                         g.Dispose ();
458                 }
459
460                 [Test]
461                 [ExpectedException (typeof (ArgumentException))]
462                 public void Transform_NonInvertibleMatrix ()
463                 {
464                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
465                         Assert.IsFalse (matrix.IsInvertible, "IsInvertible");
466                         Graphics g = Get (16, 16);
467                         g.Transform = matrix;
468                 }
469
470
471                 [Test]
472                 [ExpectedException (typeof (ArgumentException))]
473                 public void Multiply_NonInvertibleMatrix ()
474                 {
475                         Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
476                         Assert.IsFalse (matrix.IsInvertible, "IsInvertible");
477                         Graphics g = Get (16, 16);
478                         g.MultiplyTransform (matrix);
479                 }
480
481                 [Test]
482                 [ExpectedException (typeof (ArgumentNullException))]
483                 public void Multiply_Null ()
484                 {
485                         Graphics g = Get (16, 16);
486                         g.MultiplyTransform (null);
487                 }
488
489                 private void CheckBounds (string msg, RectangleF bounds, float x, float y, float w, float h)
490                 {
491                         AssertEquals (msg + ".X", x, bounds.X, 0.1);
492                         AssertEquals (msg + ".Y", y, bounds.Y, 0.1);
493                         AssertEquals (msg + ".Width", w, bounds.Width, 0.1);
494                         AssertEquals (msg + ".Height", h, bounds.Height, 0.1);
495                 }
496
497                 [Test]
498                 public void ClipBounds ()
499                 {
500                         Graphics g = Get (16, 16);
501                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
502                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
503
504                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
505                         CheckBounds ("clip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
506                         CheckBounds ("clip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
507                 }
508
509                 [Test]
510                 public void ClipBounds_Rotate ()
511                 {
512                         Graphics g = Get (16, 16);
513                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
514                         g.RotateTransform (90);
515                         CheckBounds ("rotate.ClipBounds", g.ClipBounds, 0, -8, 8, 8);
516                         CheckBounds ("rotate.Clip.GetBounds", g.Clip.GetBounds (g), 0, -8, 8, 8);
517
518                         g.Transform = new Matrix ();
519                         CheckBounds ("identity.ClipBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
520                         CheckBounds ("identity.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
521                 }
522
523                 [Test]
524                 public void ClipBounds_Scale ()
525                 {
526                         RectangleF clip = new Rectangle (0, 0, 8, 8);
527                         Graphics g = Get (16, 16);
528                         g.Clip = new Region (clip);
529                         g.ScaleTransform (0.25f, 0.5f);
530                         CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 32, 16);
531                         CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 32, 16);
532
533                         g.SetClip (clip);
534                         CheckBounds ("setclip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
535                         CheckBounds ("setclip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
536                 }
537
538                 [Test]
539                 public void ClipBounds_Translate ()
540                 {
541                         Graphics g = Get (16, 16);
542                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
543                         Region clone = g.Clip.Clone ();
544                         g.TranslateTransform (8, 8);
545                         CheckBounds ("translate.ClipBounds", g.ClipBounds, -8, -8, 8, 8);
546                         CheckBounds ("translate.Clip.GetBounds", g.Clip.GetBounds (g), -8, -8, 8, 8);
547
548                         g.SetClip (clone, CombineMode.Replace);
549                         CheckBounds ("setclip.ClipBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
550                         CheckBounds ("setclip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
551                 }
552
553                 [Test]
554                 public void ClipBounds_Transform_Translation ()
555                 {
556                         Graphics g = Get (16, 16);
557                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
558                         g.Transform = new Matrix (1, 0, 0, 1, 8, 8);
559                         CheckBounds ("transform.ClipBounds", g.ClipBounds, -8, -8, 8, 8);
560                         CheckBounds ("transform.Clip.GetBounds", g.Clip.GetBounds (g), -8, -8, 8, 8);
561
562                         g.ResetTransform ();
563                         CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
564                         CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
565                 }
566
567                 [Test]
568                 public void ClipBounds_Transform_Scale ()
569                 {
570                         Graphics g = Get (16, 16);
571                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
572                         g.Transform = new Matrix (0.5f, 0, 0, 0.25f, 0, 0);
573                         CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 16, 32);
574                         CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 32);
575
576                         g.ResetClip ();
577                         // see next test for ClipBounds
578                         CheckBounds ("resetclip.Clip.GetBounds", g.Clip.GetBounds (g), -4194304, -4194304, 8388608, 8388608);
579                         Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite");
580                 }
581
582                 [Test]
583                 [Category ("NotWorking")]
584                 public void ClipBounds_Transform_Scale_Strange ()
585                 {
586                         Graphics g = Get (16, 16);
587                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
588                         g.Transform = new Matrix (0.5f, 0, 0, 0.25f, 0, 0);
589                         CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 16, 32);
590                         CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 32);
591
592                         g.ResetClip ();
593                         // note: strange case where g.ClipBounds and g.Clip.GetBounds are different
594                         CheckBounds ("resetclip.ClipBounds", g.ClipBounds, -8388608, -16777216, 16777216, 33554432);
595                 }
596
597                 [Test]
598                 public void ClipBounds_Multiply ()
599                 {
600                         Graphics g = Get (16, 16);
601                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
602                         g.Transform = new Matrix (1, 0, 0, 1, 8, 8);
603                         g.MultiplyTransform (g.Transform);
604                         CheckBounds ("multiply.ClipBounds", g.ClipBounds, -16, -16, 8, 8);
605                         CheckBounds ("multiply.Clip.GetBounds", g.Clip.GetBounds (g), -16, -16, 8, 8);
606
607                         g.ResetTransform ();
608                         CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
609                         CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
610                 }
611
612                 [Test]
613                 public void ClipBounds_Cumulative_Effects ()
614                 {
615                         Graphics g = Get (16, 16);
616                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
617                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
618
619                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
620                         CheckBounds ("clip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
621                         CheckBounds ("clip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
622
623                         g.RotateTransform (90);
624                         CheckBounds ("rotate.ClipBounds", g.ClipBounds, 0, -8, 8, 8);
625                         CheckBounds ("rotate.Clip.GetBounds", g.Clip.GetBounds (g), 0, -8, 8, 8);
626
627                         g.ScaleTransform (0.25f, 0.5f);
628                         CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, -16, 32, 16);
629                         CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, -16, 32, 16);
630
631                         g.TranslateTransform (8, 8);
632                         CheckBounds ("translate.ClipBounds", g.ClipBounds, -8, -24, 32, 16);
633                         CheckBounds ("translate.Clip.GetBounds", g.Clip.GetBounds (g), -8, -24, 32, 16);
634
635                         g.MultiplyTransform (g.Transform);
636                         CheckBounds ("multiply.ClipBounds", g.ClipBounds, -104, -56, 64, 64);
637                         CheckBounds ("multiply.Clip.GetBounds", g.Clip.GetBounds (g), -104, -56, 64, 64);
638
639                         g.ResetTransform ();
640                         CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
641                         CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
642                 }
643
644                 [Test]
645                 public void Clip_TranslateTransform_BoundsChange ()
646                 {
647                         Graphics g = Get (16, 16);
648                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
649                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
650                         g.TranslateTransform (-16, -16);
651                         CheckBounds ("translated.ClipBounds", g.ClipBounds, 16, 16, 16, 16);
652                         CheckBounds ("translated.Clip.GetBounds", g.Clip.GetBounds (g), 16, 16, 16, 16);
653
654                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
655                         // ClipBounds isn't affected by a previous translation
656                         CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
657                         // Clip.GetBounds isn't affected by a previous translation
658                         CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
659
660                         g.ResetTransform ();
661                         CheckBounds ("reseted.ClipBounds", g.ClipBounds, -16, -16, 8, 8);
662                         CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -16, -16, 8, 8);
663                 }
664
665                 [Test]
666                 public void Clip_RotateTransform_BoundsChange ()
667                 {
668                         Graphics g = Get (16, 16);
669                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
670                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
671                         // we select a "simple" angle because the region will be converted into
672                         // a bitmap (well for libgdiplus) and we would lose precision after that
673                         g.RotateTransform (90);
674                         CheckBounds ("rotated.ClipBounds", g.ClipBounds, 0, -16, 16, 16);
675                         CheckBounds ("rotated.Clip.GetBounds", g.Clip.GetBounds (g), 0, -16, 16, 16);
676                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
677                         // ClipBounds isn't affected by a previous rotation (90)
678                         CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
679                         // Clip.GetBounds isn't affected by a previous rotation
680                         CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
681
682                         g.ResetTransform ();
683                         CheckBounds ("reseted.ClipBounds", g.ClipBounds, -8, 0, 8, 8);
684                         CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -8, 0, 8, 8);
685                 }
686
687                 private void CheckBoundsInt (string msg, RectangleF bounds, int x, int y, int w, int h)
688                 {
689                         // currently bounds are rounded at 8 pixels (FIXME - we can go down to 1 pixel)
690                         AssertEquals (msg + ".X", x, bounds.X, 4f);
691                         AssertEquals (msg + ".Y", y, bounds.Y, 4f);
692                         AssertEquals (msg + ".Width", w, bounds.Width, 4f);
693                         AssertEquals (msg + ".Height", h, bounds.Height, 4f);
694                 }
695
696                 [Test]
697                 [Category ("NotWorking")]
698                 public void Clip_RotateTransform_BoundsChange_45 ()
699                 {
700                         Graphics g = Get (16, 16);
701                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
702                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
703                         g.RotateTransform (45);
704                         // we can't use the "normal" CheckBound here because of libgdiplus crude rounding
705                         CheckBoundsInt ("rotated.ClipBounds", g.ClipBounds, 0, -11, 24, 24);
706                         CheckBoundsInt ("rotated.Clip.GetBounds", g.Clip.GetBounds (g), 0, -11, 24, 24);
707                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
708                         // ClipBounds IS affected by a previous rotation (45)
709                         CheckBoundsInt ("rectangle.ClipBounds", g.ClipBounds, -3, -4, 16, 16);
710                         // Clip.GetBounds isn't affected by a previous rotation
711                         CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
712
713                         g.ResetTransform ();
714                         CheckBounds ("reseted.ClipBounds", g.ClipBounds, -5, 1, 11, 11);
715                         CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -5.6f, 0, 11.3f, 11.3f);
716                 }
717
718                 [Test]
719                 public void Clip_ScaleTransform_NoBoundsChange ()
720                 {
721                         Graphics g = Get (16, 16);
722                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
723                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
724                         g.ScaleTransform (2, 0.5f);
725                         CheckBounds ("scaled.ClipBounds", g.ClipBounds, 0, 0, 8, 32);
726                         CheckBounds ("scaled.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 32);
727                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
728                         // ClipBounds isn't affected by a previous scaling
729                         CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
730                         // Clip.GetBounds isn't affected by a previous scaling
731                         CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
732
733                         g.ResetTransform ();
734                         CheckBounds ("reseted.ClipBounds", g.ClipBounds, 0, 0, 16, 4);
735                         CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 4);
736                 }
737
738                 [Test]
739                 [Category ("NotWorking")]
740                 public void Clip_MultiplyTransform_NoBoundsChange ()
741                 {
742                         Graphics g = Get (16, 16);
743                         CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
744                         CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
745                         g.MultiplyTransform (new Matrix (2.5f, 0.5f, -2.5f, 0.5f, 4, -4));
746                         CheckBounds ("multiplied.ClipBounds", g.ClipBounds, 3.2f, 1.6f, 19.2f, 19.2f);
747                         CheckBounds ("multiplied.Clip.GetBounds", g.Clip.GetBounds (g), 3.2f, 1.6f, 19.2f, 19.2f);
748                         g.Clip = new Region (new Rectangle (0, 0, 8, 8));
749                         // ClipBounds IS affected by the previous multiplication
750                         CheckBounds ("rectangle.ClipBounds", g.ClipBounds, -3, -3, 15, 15);
751                         // Clip.GetBounds isn't affected by the previous multiplication
752                         CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
753
754                         g.ResetTransform ();
755                         CheckBounds ("reseted.ClipBounds", g.ClipBounds, -16, -3, 40, 7);
756                         CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -16, -4, 40, 8);
757                 }
758
759                 [Test]
760                 [ExpectedException (typeof (ArgumentException))]
761                 public void ScaleTransform_X0 ()
762                 {
763                         Graphics g = Get (16, 16);
764                         g.ScaleTransform (0, 1);
765                 }
766
767                 [Test]
768                 [ExpectedException (typeof (ArgumentException))]
769                 public void ScaleTransform_Y0 ()
770                 {
771                         Graphics g = Get (16, 16);
772                         g.ScaleTransform (1, 0);
773                 }
774
775                 [Test]
776                 public void TranslateTransform_Order ()
777                 {
778                         Graphics g = Get (16, 16);
779                         g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
780                         g.TranslateTransform (3, -3);
781                         float[] elements = g.Transform.Elements;
782                         AssertEquals ("default.0", 1, elements[0]);
783                         AssertEquals ("default.1", 2, elements[1]);
784                         AssertEquals ("default.2", 3, elements[2]);
785                         AssertEquals ("default.3", 4, elements[3]);
786                         AssertEquals ("default.4", -1, elements[4]);
787                         AssertEquals ("default.5", 0, elements[5]);
788
789                         g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
790                         g.TranslateTransform (3, -3, MatrixOrder.Prepend);
791                         elements = g.Transform.Elements;
792                         AssertEquals ("prepend.0", 1, elements[0]);
793                         AssertEquals ("prepend.1", 2, elements[1]);
794                         AssertEquals ("prepend.2", 3, elements[2]);
795                         AssertEquals ("prepend.3", 4, elements[3]);
796                         AssertEquals ("prepend.4", -1, elements[4]);
797                         AssertEquals ("prepend.5", 0, elements[5]);
798
799                         g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
800                         g.TranslateTransform (3, -3, MatrixOrder.Append);
801                         elements = g.Transform.Elements;
802                         AssertEquals ("append.0", 1, elements[0]);
803                         AssertEquals ("append.1", 2, elements[1]);
804                         AssertEquals ("append.2", 3, elements[2]);
805                         AssertEquals ("append.3", 4, elements[3]);
806                         AssertEquals ("append.4", 8, elements[4]);
807                         AssertEquals ("append.5", 3, elements[5]);
808                 }
809
810                 static Point[] SmallCurve = new Point[3] { new Point (0, 0), new Point (15, 5), new Point (5, 15) };
811                 static PointF[] SmallCurveF = new PointF[3] { new PointF (0, 0), new PointF (15, 5), new PointF (5, 15) };
812
813                 static Point[] TooSmallCurve = new Point[2] { new Point (0, 0), new Point (15, 5) };
814                 static PointF[] LargeCurveF = new PointF[4] { new PointF (0, 0), new PointF (15, 5), new PointF (5, 15), new PointF (0, 20) };
815
816                 [Test]
817                 [ExpectedException (typeof (ArgumentNullException))]
818                 public void DrawCurve_PenNull ()
819                 {
820                         Bitmap bitmap = new Bitmap (20, 20);
821                         Graphics g = Graphics.FromImage (bitmap);
822                         g.DrawCurve (null, SmallCurveF);
823                 }
824
825                 [Test]
826                 [ExpectedException (typeof (ArgumentNullException))]
827                 public void DrawCurve_PointFNull ()
828                 {
829                         Bitmap bitmap = new Bitmap (20, 20);
830                         Graphics g = Graphics.FromImage (bitmap);
831                         g.DrawCurve (Pens.Black, (PointF[]) null);
832                 }
833
834                 [Test]
835                 [ExpectedException (typeof (ArgumentNullException))]
836                 public void DrawCurve_PointNull ()
837                 {
838                         Bitmap bitmap = new Bitmap (20, 20);
839                         Graphics g = Graphics.FromImage (bitmap);
840                         g.DrawCurve (Pens.Black, (Point[]) null);
841                 }
842
843                 [Test]
844                 public void DrawCurve_NotEnoughPoints ()
845                 {
846                         Bitmap bitmap = new Bitmap (20, 20);
847                         Graphics g = Graphics.FromImage (bitmap);
848                         CheckForEmptyBitmap (bitmap);
849                         g.DrawCurve (Pens.Black, TooSmallCurve, 0.5f);
850                         CheckForNonEmptyBitmap (bitmap);
851                         // so a "curve" can be drawn with less than 3 points!
852                         // actually I used to call that a line... (and it's not related to tension)
853                         g.Dispose ();
854                         bitmap.Dispose ();
855                 }
856
857                 [Test]
858                 [ExpectedException (typeof (ArgumentException))]
859                 public void DrawCurve_SinglePoint ()
860                 {
861                         Bitmap bitmap = new Bitmap (20, 20);
862                         Graphics g = Graphics.FromImage (bitmap);
863                         g.DrawCurve (Pens.Black, new Point[1] { new Point (10, 10) }, 0.5f);
864                         // a single point isn't enough
865                 }
866
867                 [Test]
868                 [ExpectedException (typeof (ArgumentException))]
869                 public void DrawCurve3_NotEnoughPoints ()
870                 {
871                         Bitmap bitmap = new Bitmap (20, 20);
872                         Graphics g = Graphics.FromImage (bitmap);
873                         g.DrawCurve (Pens.Black, TooSmallCurve, 0, 2, 0.5f);
874                         // aha, this is API dependent
875                 }
876
877                 [Test]
878                 public void DrawCurve_NegativeTension ()
879                 {
880                         Bitmap bitmap = new Bitmap (20, 20);
881                         Graphics g = Graphics.FromImage (bitmap);
882                         // documented as bigger (or equals) to 0
883                         g.DrawCurve (Pens.Black, SmallCurveF, -0.9f);
884                         CheckForNonEmptyBitmap (bitmap);
885                         g.Dispose ();
886                         bitmap.Dispose ();
887                 }
888
889                 [Test]
890                 public void DrawCurve_PositiveTension ()
891                 {
892                         Bitmap bitmap = new Bitmap (20, 20);
893                         Graphics g = Graphics.FromImage (bitmap);
894                         g.DrawCurve (Pens.Black, SmallCurveF, 0.9f);
895                         // this is not the same as -1
896                         CheckForNonEmptyBitmap (bitmap);
897                         g.Dispose ();
898                         bitmap.Dispose ();
899                 }
900
901                 [Test]
902                 [Category ("NotWorking")] // libgdiplus is drawing something
903                 public void DrawCurve_LargeTension ()
904                 {
905                         Bitmap bitmap = new Bitmap (20, 20);
906                         Graphics g = Graphics.FromImage (bitmap);
907                         g.DrawCurve (Pens.Black, SmallCurve, Single.MaxValue);
908                         CheckForEmptyBitmap (bitmap);
909                         // too much tension ;)
910                         g.Dispose ();
911                         bitmap.Dispose ();
912                 }
913
914                 [Test]
915                 [ExpectedException (typeof (ArgumentException))]
916                 public void DrawCurve_ZeroSegments ()
917                 {
918                         Bitmap bitmap = new Bitmap (20, 20);
919                         Graphics g = Graphics.FromImage (bitmap);
920                         g.DrawCurve (Pens.Black, SmallCurveF, 0, 0);
921                 }
922
923                 [Test]
924                 [ExpectedException (typeof (ArgumentException))]
925                 public void DrawCurve_NegativeSegments ()
926                 {
927                         Bitmap bitmap = new Bitmap (20, 20);
928                         Graphics g = Graphics.FromImage (bitmap);
929                         g.DrawCurve (Pens.Black, SmallCurveF, 0, -1);
930                 }
931
932                 [Test]
933                 [ExpectedException (typeof (ArgumentException))]
934                 public void DrawCurve_OffsetTooLarge ()
935                 {
936                         Bitmap bitmap = new Bitmap (20, 20);
937                         Graphics g = Graphics.FromImage (bitmap);
938                         // starting offset 1 doesn't give 3 points to make a curve
939                         g.DrawCurve (Pens.Black, SmallCurveF, 1, 2);
940                         // and in this case 2 points aren't enough to draw something
941                 }
942
943                 [Test]
944                 public void DrawCurve_Offset_0 ()
945                 {
946                         Bitmap bitmap = new Bitmap (20, 20);
947                         Graphics g = Graphics.FromImage (bitmap);
948                         g.DrawCurve (Pens.Black, LargeCurveF, 0, 2, 0.5f);
949                         CheckForNonEmptyBitmap (bitmap);
950                         g.Dispose ();
951                         bitmap.Dispose ();
952                 }
953
954                 [Test]
955                 public void DrawCurve_Offset_1 ()
956                 {
957                         Bitmap bitmap = new Bitmap (20, 20);
958                         Graphics g = Graphics.FromImage (bitmap);
959                         g.DrawCurve (Pens.Black, LargeCurveF, 1, 2, 0.5f);
960                         CheckForNonEmptyBitmap (bitmap);
961                         g.Dispose ();
962                         bitmap.Dispose ();
963                 }
964
965                 [Test]
966                 public void DrawCurve_Offset_2 ()
967                 {
968                         Bitmap bitmap = new Bitmap (20, 20);
969                         Graphics g = Graphics.FromImage (bitmap);
970                         // it works even with two points because we know the previous ones
971                         g.DrawCurve (Pens.Black, LargeCurveF, 2, 1, 0.5f);
972                         CheckForNonEmptyBitmap (bitmap);
973                         g.Dispose ();
974                         bitmap.Dispose ();
975                 }
976
977                 [Test]
978                 public void DrawRectangle_Negative ()
979                 {
980                         Bitmap bitmap = new Bitmap (20, 20);
981                         Graphics g = Graphics.FromImage (bitmap);
982                         Pen pen = new Pen (Color.Red);
983                         g.DrawRectangle (pen, 5, 5, -10, -10);
984                         g.DrawRectangle (pen, 0.0f, 0.0f, 5.0f, -10.0f);
985                         g.DrawRectangle (pen, new Rectangle (15, 0, -10, 5));
986                         CheckForEmptyBitmap (bitmap);
987                         pen.Dispose ();
988                         g.Dispose ();
989                         bitmap.Dispose ();
990                 }
991
992                 [Test]
993                 public void DrawRectangles_Negative ()
994                 {
995                         Bitmap bitmap = new Bitmap (20, 20);
996                         Graphics g = Graphics.FromImage (bitmap);
997                         Pen pen = new Pen (Color.Red);
998                         Rectangle[] rects = new Rectangle[2] {
999                                 new Rectangle (5, 5, -10, -10), new Rectangle (0, 0, 5, -10)
1000                         };
1001                         RectangleF[] rectf = new RectangleF[2] {
1002                                 new RectangleF (0.0f, 5.0f, -10.0f, -10.0f), new RectangleF (15.0f, 0.0f, -10.0f, 5.0f)
1003                         };
1004                         g.DrawRectangles (pen, rects);
1005                         g.DrawRectangles (pen, rectf);
1006                         CheckForEmptyBitmap (bitmap);
1007                         pen.Dispose ();
1008                         g.Dispose ();
1009                         bitmap.Dispose ();
1010                 }
1011
1012                 [Test]
1013                 public void FillRectangle_Negative ()
1014                 {
1015                         Bitmap bitmap = new Bitmap (20, 20);
1016                         Graphics g = Graphics.FromImage (bitmap);
1017                         SolidBrush brush = new SolidBrush (Color.Red);
1018                         g.FillRectangle (brush, 5, 5, -10, -10);
1019                         g.FillRectangle (brush, 0.0f, 0.0f, 5.0f, -10.0f);
1020                         g.FillRectangle (brush, new Rectangle (15, 0, -10, 5));
1021                         CheckForEmptyBitmap (bitmap);
1022                         brush.Dispose ();
1023                         g.Dispose ();
1024                         bitmap.Dispose ();
1025                 }
1026
1027                 [Test]
1028                 public void FillRectangles_Negative ()
1029                 {
1030                         Bitmap bitmap = new Bitmap (20, 20);
1031                         Graphics g = Graphics.FromImage (bitmap);
1032                         SolidBrush brush = new SolidBrush (Color.Red);
1033                         Rectangle[] rects = new Rectangle[2] {
1034                                 new Rectangle (5, 5, -10, -10), new Rectangle (0, 0, 5, -10)
1035                         };
1036                         RectangleF[] rectf = new RectangleF[2] {
1037                                 new RectangleF (0.0f, 5.0f, -10.0f, -10.0f), new RectangleF (15.0f, 0.0f, -10.0f, 5.0f)
1038                         };
1039                         g.FillRectangles (brush, rects);
1040                         g.FillRectangles (brush, rectf);
1041                         CheckForEmptyBitmap (bitmap);
1042                         brush.Dispose ();
1043                         g.Dispose ();
1044                         bitmap.Dispose ();
1045                 }
1046
1047                 [Test] // bug #355141
1048                 [Category ("CAS")]
1049                 public void FromHwnd_Zero ()
1050                 {
1051                         Graphics g = Graphics.FromHwnd (IntPtr.Zero);
1052                         Assert.IsNotNull (g);
1053                 }
1054
1055                 private void CheckDefaultProperties (string message, Graphics g)
1056                 {
1057                         Assert.IsTrue (g.Clip.IsInfinite (g), message + ".Clip.IsInfinite");
1058                         AssertEquals (message + ".CompositingMode", CompositingMode.SourceOver, g.CompositingMode);
1059                         AssertEquals (message + ".CompositingQuality", CompositingQuality.Default, g.CompositingQuality);
1060                         AssertEquals (message + ".InterpolationMode", InterpolationMode.Bilinear, g.InterpolationMode);
1061                         AssertEquals (message + ".PageScale", 1.0f, g.PageScale);
1062                         AssertEquals (message + ".PageUnit", GraphicsUnit.Display, g.PageUnit);
1063                         AssertEquals (message + ".PixelOffsetMode", PixelOffsetMode.Default, g.PixelOffsetMode);
1064                         AssertEquals (message + ".SmoothingMode", SmoothingMode.None, g.SmoothingMode);
1065                         AssertEquals (message + ".TextContrast", 4, g.TextContrast);
1066                         AssertEquals (message + ".TextRenderingHint", TextRenderingHint.SystemDefault, g.TextRenderingHint);
1067                         Assert.IsTrue (g.Transform.IsIdentity, message + ".Transform.IsIdentity");
1068                 }
1069
1070                 private void CheckCustomProperties (string message, Graphics g)
1071                 {
1072                         Assert.IsFalse (g.Clip.IsInfinite (g), message + ".Clip.IsInfinite");
1073                         AssertEquals (message + ".CompositingMode", CompositingMode.SourceCopy, g.CompositingMode);
1074                         AssertEquals (message + ".CompositingQuality", CompositingQuality.HighQuality, g.CompositingQuality);
1075                         AssertEquals (message + ".InterpolationMode", InterpolationMode.HighQualityBicubic, g.InterpolationMode);
1076                         AssertEquals (message + ".PageScale", 0.5f, g.PageScale);
1077                         AssertEquals (message + ".PageUnit", GraphicsUnit.Inch, g.PageUnit);
1078                         AssertEquals (message + ".PixelOffsetMode", PixelOffsetMode.Half, g.PixelOffsetMode);
1079                         AssertEquals (message + ".RenderingOrigin", new Point (-1, -1), g.RenderingOrigin);
1080                         AssertEquals (message + ".SmoothingMode", SmoothingMode.AntiAlias, g.SmoothingMode);
1081                         AssertEquals (message + ".TextContrast", 0, g.TextContrast);
1082                         AssertEquals (message + ".TextRenderingHint", TextRenderingHint.AntiAlias, g.TextRenderingHint);
1083                         Assert.IsFalse (g.Transform.IsIdentity, message + ".Transform.IsIdentity");
1084                 }
1085
1086                 private void CheckMatrix (string message, Matrix m, float xx, float yx, float xy, float yy, float x0, float y0)
1087                 {
1088                         float[] elements = m.Elements;
1089                         AssertEquals (message + ".Matrix.xx", xx, elements[0], 0.01);
1090                         AssertEquals (message + ".Matrix.yx", yx, elements[1], 0.01);
1091                         AssertEquals (message + ".Matrix.xy", xy, elements[2], 0.01);
1092                         AssertEquals (message + ".Matrix.yy", yy, elements[3], 0.01);
1093                         AssertEquals (message + ".Matrix.x0", x0, elements[4], 0.01);
1094                         AssertEquals (message + ".Matrix.y0", y0, elements[5], 0.01);
1095                 }
1096
1097                 [Test]
1098                 public void BeginContainer ()
1099                 {
1100                         Bitmap bitmap = new Bitmap (20, 20);
1101                         Graphics g = Graphics.FromImage (bitmap);
1102
1103                         CheckDefaultProperties ("default", g);
1104                         AssertEquals ("default.RenderingOrigin", new Point (0, 0), g.RenderingOrigin);
1105
1106                         g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1107                         g.CompositingMode = CompositingMode.SourceCopy;
1108                         g.CompositingQuality = CompositingQuality.HighQuality;
1109                         g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1110                         g.PageScale = 0.5f;
1111                         g.PageUnit = GraphicsUnit.Inch;
1112                         g.PixelOffsetMode = PixelOffsetMode.Half;
1113                         g.RenderingOrigin = new Point (-1, -1);
1114                         g.RotateTransform (45);
1115                         g.SmoothingMode = SmoothingMode.AntiAlias;
1116                         g.TextContrast = 0;
1117                         g.TextRenderingHint = TextRenderingHint.AntiAlias;
1118                         CheckCustomProperties ("modified", g);
1119                         CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1120
1121                         GraphicsContainer gc = g.BeginContainer ();
1122                         // things gets reseted after calling BeginContainer
1123                         CheckDefaultProperties ("BeginContainer", g);
1124                         // but not everything 
1125                         AssertEquals ("BeginContainer.RenderingOrigin", new Point (-1, -1), g.RenderingOrigin);
1126
1127                         g.EndContainer (gc);
1128                         CheckCustomProperties ("EndContainer", g);
1129                 }
1130
1131                 [Test]
1132                 public void BeginContainer_Rect ()
1133                 {
1134                         Bitmap bitmap = new Bitmap (20, 20);
1135                         Graphics g = Graphics.FromImage (bitmap);
1136
1137                         CheckDefaultProperties ("default", g);
1138                         AssertEquals ("default.RenderingOrigin", new Point (0, 0), g.RenderingOrigin);
1139
1140                         g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1141                         g.CompositingMode = CompositingMode.SourceCopy;
1142                         g.CompositingQuality = CompositingQuality.HighQuality;
1143                         g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1144                         g.PageScale = 0.5f;
1145                         g.PageUnit = GraphicsUnit.Inch;
1146                         g.PixelOffsetMode = PixelOffsetMode.Half;
1147                         g.RenderingOrigin = new Point (-1, -1);
1148                         g.RotateTransform (45);
1149                         g.SmoothingMode = SmoothingMode.AntiAlias;
1150                         g.TextContrast = 0;
1151                         g.TextRenderingHint = TextRenderingHint.AntiAlias;
1152                         CheckCustomProperties ("modified", g);
1153                         CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1154
1155                         GraphicsContainer gc = g.BeginContainer (new Rectangle (10, 20, 30, 40), new Rectangle (10, 20, 300, 400), GraphicsUnit.Millimeter);
1156                         // things gets reseted after calling BeginContainer
1157                         CheckDefaultProperties ("BeginContainer", g);
1158                         // but not everything 
1159                         AssertEquals ("BeginContainer.RenderingOrigin", new Point (-1, -1), g.RenderingOrigin);
1160
1161                         g.EndContainer (gc);
1162                         CheckCustomProperties ("EndContainer", g);
1163                         CheckMatrix ("EndContainer.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1164                 }
1165
1166                 [Test]
1167                 public void BeginContainer_RectF ()
1168                 {
1169                         Bitmap bitmap = new Bitmap (20, 20);
1170                         Graphics g = Graphics.FromImage (bitmap);
1171
1172                         CheckDefaultProperties ("default", g);
1173                         AssertEquals ("default.RenderingOrigin", new Point (0, 0), g.RenderingOrigin);
1174
1175                         g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1176                         g.CompositingMode = CompositingMode.SourceCopy;
1177                         g.CompositingQuality = CompositingQuality.HighQuality;
1178                         g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1179                         g.PageScale = 0.5f;
1180                         g.PageUnit = GraphicsUnit.Inch;
1181                         g.PixelOffsetMode = PixelOffsetMode.Half;
1182                         g.RenderingOrigin = new Point (-1, -1);
1183                         g.RotateTransform (45);
1184                         g.SmoothingMode = SmoothingMode.AntiAlias;
1185                         g.TextContrast = 0;
1186                         g.TextRenderingHint = TextRenderingHint.AntiAlias;
1187                         CheckCustomProperties ("modified", g);
1188                         CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1189
1190                         GraphicsContainer gc = g.BeginContainer (new RectangleF (40, 30, 20, 10), new RectangleF (10, 20, 30, 40), GraphicsUnit.Inch);
1191                         // things gets reseted after calling BeginContainer
1192                         CheckDefaultProperties ("BeginContainer", g);
1193                         // but not everything 
1194                         AssertEquals ("BeginContainer.RenderingOrigin", new Point (-1, -1), g.RenderingOrigin);
1195
1196                         g.EndContainer (gc);
1197                         CheckCustomProperties ("EndContainer", g);
1198                 }
1199
1200                 private void BeginContainer_GraphicsUnit (GraphicsUnit unit)
1201                 {
1202                         Bitmap bitmap = new Bitmap (20, 20);
1203                         Graphics g = Graphics.FromImage (bitmap);
1204                         g.BeginContainer (new RectangleF (40, 30, 20, 10), new RectangleF (10, 20, 30, 40), unit);
1205                 }
1206
1207                 [Test]
1208                 [ExpectedException (typeof (ArgumentException))]
1209                 public void BeginContainer_GraphicsUnit_Display ()
1210                 {
1211                         BeginContainer_GraphicsUnit (GraphicsUnit.Display);
1212                 }
1213
1214                 [Test]
1215                 public void BeginContainer_GraphicsUnit_Valid ()
1216                 {
1217                         BeginContainer_GraphicsUnit (GraphicsUnit.Document);
1218                         BeginContainer_GraphicsUnit (GraphicsUnit.Inch);
1219                         BeginContainer_GraphicsUnit (GraphicsUnit.Millimeter);
1220                         BeginContainer_GraphicsUnit (GraphicsUnit.Pixel);
1221                         BeginContainer_GraphicsUnit (GraphicsUnit.Point);
1222                 }
1223
1224                 [Test]
1225                 [ExpectedException (typeof (ArgumentException))]
1226                 public void BeginContainer_GraphicsUnit_World ()
1227                 {
1228                         BeginContainer_GraphicsUnit (GraphicsUnit.World);
1229                 }
1230
1231                 [Test]
1232                 [ExpectedException (typeof (ArgumentException))]
1233                 public void BeginContainer_GraphicsUnit_Bad ()
1234                 {
1235                         BeginContainer_GraphicsUnit ((GraphicsUnit) Int32.MinValue);
1236                 }
1237
1238                 [Test]
1239 #if NET_2_0
1240                 [ExpectedException (typeof (ArgumentNullException))]
1241 #else
1242                 [ExpectedException (typeof (NullReferenceException))]
1243 #endif
1244                 public void EndContainer_Null ()
1245                 {
1246                         Bitmap bitmap = new Bitmap (20, 20);
1247                         Graphics g = Graphics.FromImage (bitmap);
1248                         g.EndContainer (null);
1249                 }
1250
1251                 [Test]
1252                 public void Save ()
1253                 {
1254                         Bitmap bitmap = new Bitmap (20, 20);
1255                         Graphics g = Graphics.FromImage (bitmap);
1256
1257                         CheckDefaultProperties ("default", g);
1258                         AssertEquals ("default.RenderingOrigin", new Point (0, 0), g.RenderingOrigin);
1259
1260                         GraphicsState gs1 = g.Save ();
1261                         // nothing is changed after a save
1262                         CheckDefaultProperties ("save1", g);
1263                         AssertEquals ("save1.RenderingOrigin", new Point (0, 0), g.RenderingOrigin);
1264
1265                         g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1266                         g.CompositingMode = CompositingMode.SourceCopy;
1267                         g.CompositingQuality = CompositingQuality.HighQuality;
1268                         g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1269                         g.PageScale = 0.5f;
1270                         g.PageUnit = GraphicsUnit.Inch;
1271                         g.PixelOffsetMode = PixelOffsetMode.Half;
1272                         g.RenderingOrigin = new Point (-1, -1);
1273                         g.RotateTransform (45);
1274                         g.SmoothingMode = SmoothingMode.AntiAlias;
1275                         g.TextContrast = 0;
1276                         g.TextRenderingHint = TextRenderingHint.AntiAlias;
1277                         CheckCustomProperties ("modified", g);
1278                         CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1279
1280                         GraphicsState gs2 = g.Save ();
1281                         CheckCustomProperties ("save2", g);
1282
1283                         g.Restore (gs2);
1284                         CheckCustomProperties ("restored1", g);
1285                         CheckMatrix ("restored1.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1286
1287                         g.Restore (gs1);
1288                         CheckDefaultProperties ("restored2", g);
1289                         AssertEquals ("restored2.RenderingOrigin", new Point (0, 0), g.RenderingOrigin);
1290                 }
1291
1292                 [Test]
1293                 [ExpectedException (typeof (NullReferenceException))]
1294                 public void Restore_Null ()
1295                 {
1296                         Bitmap bitmap = new Bitmap (20, 20);
1297                         Graphics g = Graphics.FromImage (bitmap);
1298                         g.Restore (null);
1299                 }
1300
1301                 [Test]
1302                 [ExpectedException (typeof (ArgumentNullException))]
1303                 public void FillRectangles_BrushNull_Rectangle ()
1304                 {
1305                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1306                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1307                                         g.FillRectangles (null, new Rectangle[1]);
1308                                 }
1309                         }
1310                 }
1311
1312                 [Test]
1313                 [ExpectedException (typeof (ArgumentNullException))]
1314                 public void FillRectangles_Rectangle_Null ()
1315                 {
1316                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1317                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1318                                         g.FillRectangles (Brushes.Red, (Rectangle[]) null);
1319                                 }
1320                         }
1321                 }
1322
1323                 [Test] // see bug #78408
1324                 [ExpectedException (typeof (ArgumentException))]
1325                 public void FillRectanglesZeroRectangle ()
1326                 {
1327                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1328                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1329                                         g.FillRectangles (Brushes.Red, new Rectangle[0]);
1330                                 }
1331                         }
1332                 }
1333
1334                 [Test]
1335                 [ExpectedException (typeof (ArgumentNullException))]
1336                 public void FillRectangles_BrushNull_RectangleF ()
1337                 {
1338                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1339                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1340                                         g.FillRectangles (null, new RectangleF[1]);
1341                                 }
1342                         }
1343                 }
1344
1345                 [Test]
1346                 [ExpectedException (typeof (ArgumentNullException))]
1347                 public void FillRectangles_RectangleF_Null ()
1348                 {
1349                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1350                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1351                                         g.FillRectangles (Brushes.Red, (RectangleF[]) null);
1352                                 }
1353                         }
1354                 }
1355
1356                 [Test] // see bug #78408
1357                 [ExpectedException (typeof (ArgumentException))]
1358                 public void FillRectanglesZeroRectangleF ()
1359                 {
1360                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1361                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1362                                         g.FillRectangles (Brushes.Red, new RectangleF[0]);
1363                                 }
1364                         }
1365                 }
1366
1367                 [Test]
1368                 public void FillRectangles_NormalBehavior ()
1369                 {
1370                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1371                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1372                                         g.Clear (Color.Fuchsia);
1373                                         Rectangle rect = new Rectangle (5, 5, 10, 10);
1374                                         g.Clip = new Region (rect);
1375                                         g.FillRectangle (Brushes.Red, rect);
1376                                 }
1377                                 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1378                                 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1379                                 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (5, 14).ToArgb (), "5,14");
1380                                 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1381
1382                                 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1383                                 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1384                                 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1385                         }
1386                 }
1387
1388                 // see bug #81737 for details
1389                 private Bitmap FillDrawRectangle (float width)
1390                 {
1391                         Bitmap bitmap = new Bitmap (20, 20);
1392                         using (Graphics g = Graphics.FromImage (bitmap)) {
1393                                 g.Clear (Color.Red);
1394                                 Rectangle rect = new Rectangle (5, 5, 10, 10);
1395                                 g.FillRectangle (Brushes.Green, rect);
1396                                 if (width >= 0) {
1397                                         using (Pen pen = new Pen (Color.Blue, width)) {
1398                                                 g.DrawRectangle (pen, rect);
1399                                         }
1400                                 } else {
1401                                         g.DrawRectangle (Pens.Blue, rect);
1402                                 }
1403                         }
1404                         return bitmap;
1405                 }
1406
1407                 [Test]
1408                 public void FillDrawRectangle_Width_Default ()
1409                 {
1410                         // default pen size
1411                         using (Bitmap bitmap = FillDrawRectangle (Single.MinValue)) {
1412                                 // NW
1413                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1414                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1415                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1416                                 // N
1417                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1418                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1419                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1420                                 // NE
1421                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1422                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1423                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1424                                 // E
1425                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1426                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1427                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1428                                 // SE
1429                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1430                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1431                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1432                                 // S
1433                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1434                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1435                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1436                                 // SW
1437                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1438                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1439                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1440                                 // W
1441                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1442                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1443                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1444                         }
1445                 }
1446
1447                 [Test]
1448                 public void FillDrawRectangle_Width_2 ()
1449                 {
1450                         // even pen size
1451                         using (Bitmap bitmap = FillDrawRectangle (2.0f)) {
1452                                 // NW
1453                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1454                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1455                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1456                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1457                                 // N
1458                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1459                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1460                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1461                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1462                                 // NE
1463                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1464                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1465                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1466                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1467                                 // E
1468                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1469                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1470                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1471                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 9).ToArgb (), "13,9");
1472                                 // SE
1473                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1474                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1475                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1476                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 13).ToArgb (), "13,13");
1477                                 // S
1478                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1479                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1480                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1481                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 13).ToArgb (), "9,13");
1482                                 // SW
1483                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 16).ToArgb (), "3,16");
1484                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 15).ToArgb (), "4,15");
1485                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 14).ToArgb (), "5,14");
1486                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 13).ToArgb (), "6,13");
1487                                 // W
1488                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1489                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1490                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1491                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1492                         }
1493                 }
1494
1495                 [Test]
1496                 public void FillDrawRectangle_Width_3 ()
1497                 {
1498                         // odd pen size
1499                         using (Bitmap bitmap = FillDrawRectangle (3.0f)) {
1500                                 // NW
1501                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1502                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1503                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1504                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1505                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 7).ToArgb (), "7,7");
1506                                 // N
1507                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1508                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1509                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1510                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1511                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 7).ToArgb (), "9,7");
1512                                 // NE
1513                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1514                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1515                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1516                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1517                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 7).ToArgb (), "13,7");
1518                                 // E
1519                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 9).ToArgb (), "17,9");
1520                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1521                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1522                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1523                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 9).ToArgb (), "13,9");
1524                                 // SE
1525                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 17).ToArgb (), "17,17");
1526                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1527                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1528                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1529                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 13).ToArgb (), "13,13");
1530                                 // S
1531                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 17).ToArgb (), "9,17");
1532                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1533                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1534                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1535                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 13).ToArgb (), "9,13");
1536                                 // SW
1537                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 17).ToArgb (), "3,17");
1538                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1539                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1540                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1541                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 13).ToArgb (), "7,13");
1542                                 // W
1543                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1544                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1545                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1546                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1547                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 9).ToArgb (), "7,9");
1548                         }
1549                 }
1550
1551                 // reverse, draw the fill over
1552                 private Bitmap DrawFillRectangle (float width)
1553                 {
1554                         Bitmap bitmap = new Bitmap (20, 20);
1555                         using (Graphics g = Graphics.FromImage (bitmap)) {
1556                                 g.Clear (Color.Red);
1557                                 Rectangle rect = new Rectangle (5, 5, 10, 10);
1558                                 if (width >= 0) {
1559                                         using (Pen pen = new Pen (Color.Blue, width)) {
1560                                                 g.DrawRectangle (pen, rect);
1561                                         }
1562                                 } else {
1563                                         g.DrawRectangle (Pens.Blue, rect);
1564                                 }
1565                                 g.FillRectangle (Brushes.Green, rect);
1566                         }
1567                         return bitmap;
1568                 }
1569
1570                 [Test]
1571                 public void DrawFillRectangle_Width_Default ()
1572                 {
1573                         // default pen size
1574                         using (Bitmap bitmap = DrawFillRectangle (Single.MinValue)) {
1575                                 // NW - no blue border
1576                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1577                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1578                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1579                                 // N - no blue border
1580                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1581                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1582                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1583                                 // NE
1584                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1585                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1586                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1587                                 // E
1588                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1589                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1590                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1591                                 // SE
1592                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1593                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1594                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1595                                 // S
1596                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1597                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1598                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1599                                 // SW
1600                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1601                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1602                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1603                                 // W - no blue border
1604                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1605                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1606                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1607                         }
1608                 }
1609
1610                 [Test]
1611                 public void DrawFillRectangle_Width_2 ()
1612                 {
1613                         // even pen size
1614                         using (Bitmap bitmap = DrawFillRectangle (2.0f)) {
1615                                 // looks like a one pixel border - but enlarged
1616                                 // NW
1617                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1618                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1619                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1620                                 // N
1621                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1622                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1623                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1624                                 // NE
1625                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1626                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1627                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1628                                 // E
1629                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1630                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1631                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1632                                 // SE
1633                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1634                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1635                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1636                                 // S
1637                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1638                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1639                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1640                                 // SW
1641                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 16).ToArgb (), "4,16");
1642                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 15).ToArgb (), "5,15");
1643                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 14).ToArgb (), "6,14");
1644                                 // W
1645                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1646                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1647                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1648                         }
1649                 }
1650
1651                 [Test]
1652                 public void DrawFillRectangle_Width_3 ()
1653                 {
1654                         // odd pen size
1655                         using (Bitmap bitmap = DrawFillRectangle (3.0f)) {
1656                                 // NW
1657                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1658                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1659                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1660                                 // N
1661                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1662                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1663                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1664                                 // NE
1665                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1666                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1667                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1668                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1669                                 // E
1670                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 9).ToArgb (), "17,9");
1671                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1672                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1673                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1674                                 // SE
1675                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 17).ToArgb (), "17,17");
1676                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1677                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1678                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1679                                 // S
1680                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 17).ToArgb (), "9,17");
1681                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1682                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1683                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1684                                 // SW
1685                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 17).ToArgb (), "3,17");
1686                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1687                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1688                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1689                                 // W
1690                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1691                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1692                                 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1693                         }
1694                 }
1695
1696                 private Bitmap DrawLines (float width)
1697                 {
1698                         Bitmap bitmap = new Bitmap (20, 20);
1699                         using (Graphics g = Graphics.FromImage (bitmap)) {
1700                                 g.Clear (Color.Red);
1701                                 Point[] pts = new Point[3] { new Point (5, 5), new Point (15, 5), new Point (15, 15) };
1702                                 if (width >= 0) {
1703                                         using (Pen pen = new Pen (Color.Blue, width)) {
1704                                                 g.DrawLines (pen, pts);
1705                                         }
1706                                 } else {
1707                                         g.DrawLines (Pens.Blue, pts);
1708                                 }
1709                         }
1710                         return bitmap;
1711                 }
1712
1713                 [Test]
1714                 public void DrawLines_Width_Default ()
1715                 {
1716                         // default pen size
1717                         using (Bitmap bitmap = DrawLines (Single.MinValue)) {
1718                                 // start
1719                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1720                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1721                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1722                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1723                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1724                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1725                                 // middle
1726                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1727                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1728                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1729                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1730                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1731                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1732                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1733                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1734                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1735                                 //end
1736                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1737                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1738                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1739                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 16).ToArgb (), "14,16");
1740                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 16).ToArgb (), "15,16");
1741                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1742                         }
1743                 }
1744
1745                 [Test]
1746                 [Category ("NotWorking")]
1747                 public void DrawLines_Width_2 ()
1748                 {
1749                         // default pen size
1750                         using (Bitmap bitmap = DrawLines (2.0f)) {
1751                                 // start
1752                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 3).ToArgb (), "4,3");
1753                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1754                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1755                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1756                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 3).ToArgb (), "5,3");
1757                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1758                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1759                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1760                                 // middle
1761                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 3).ToArgb (), "13,3");
1762                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 4).ToArgb (), "13,4");
1763                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 5).ToArgb (), "13,5");
1764                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1765                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 3).ToArgb (), "14,3");
1766                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1767                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1768                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1769                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 3).ToArgb (), "15,3");
1770                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1771                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1772                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1773                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1774                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1775                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1776                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1777                                 //end
1778                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 14).ToArgb (), "13,14");
1779                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1780                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 14).ToArgb (), "15,14");
1781                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 14).ToArgb (), "16,14");
1782                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 15).ToArgb (), "13,15");
1783                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1784                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1785                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1786                         }
1787                 }
1788
1789                 [Test]
1790                 [Category ("NotWorking")]
1791                 public void DrawLines_Width_3 ()
1792                 {
1793                         // default pen size
1794                         using (Bitmap bitmap = DrawLines (3.0f)) {
1795                                 // start
1796                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 3).ToArgb (), "4,3");
1797                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1798                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1799                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1800                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 7).ToArgb (), "4,7");
1801                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 3).ToArgb (), "5,3");
1802                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1803                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1804                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1805                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 7).ToArgb (), "5,7");
1806                                 // middle
1807                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 3).ToArgb (), "13,3");
1808                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 4).ToArgb (), "13,4");
1809                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 5).ToArgb (), "13,5");
1810                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1811                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 7).ToArgb (), "13,7");
1812                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 3).ToArgb (), "14,3");
1813                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1814                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1815                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1816                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 7).ToArgb (), "14,7");
1817                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 3).ToArgb (), "15,3");
1818                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1819                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1820                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1821                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 7).ToArgb (), "15,7");
1822                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1823                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1824                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1825                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1826                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 7).ToArgb (), "16,7");
1827                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1828                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 4).ToArgb (), "17,4");
1829                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 5).ToArgb (), "17,5");
1830                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 6).ToArgb (), "17,6");
1831                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 7).ToArgb (), "17,7");
1832                                 //end
1833                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 14).ToArgb (), "13,14");
1834                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1835                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 14).ToArgb (), "15,14");
1836                                 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 14).ToArgb (), "16,14");
1837                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 14).ToArgb (), "17,14");
1838                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 15).ToArgb (), "13,15");
1839                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1840                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1841                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1842                                 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 15).ToArgb (), "17,15");
1843                         }
1844                 }
1845
1846                 [Test]
1847                 public void MeasureString_StringFont ()
1848                 {
1849                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1850                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1851                                         SizeF size = g.MeasureString (null, font);
1852                                         Assert.IsTrue (size.IsEmpty, "MeasureString(null,font)");
1853                                         size = g.MeasureString (String.Empty, font);
1854                                         Assert.IsTrue (size.IsEmpty, "MeasureString(empty,font)");
1855                                         // null font
1856                                         size = g.MeasureString (null, null);
1857                                         Assert.IsTrue (size.IsEmpty, "MeasureString(null,null)");
1858                                         size = g.MeasureString (String.Empty, null);
1859                                         Assert.IsTrue (size.IsEmpty, "MeasureString(empty,null)");
1860                                 }
1861                         }
1862                 }
1863
1864                 [Test]
1865                 [ExpectedException (typeof (ArgumentNullException))]
1866                 public void MeasureString_StringFont_Null ()
1867                 {
1868                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1869                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1870                                         g.MeasureString ("a", null);
1871                                 }
1872                         }
1873                 }
1874
1875                 [Test]
1876                 public void MeasureString_StringFontSizeF ()
1877                 {
1878                         if (font == null)
1879                                 Assert.Ignore ("Couldn't create required font");
1880
1881                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1882                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1883                                         SizeF size = g.MeasureString ("a", font, SizeF.Empty);
1884                                         Assert.IsFalse (size.IsEmpty, "MeasureString(a,font,empty)");
1885
1886                                         size = g.MeasureString (String.Empty, font, SizeF.Empty);
1887                                         Assert.IsTrue (size.IsEmpty, "MeasureString(empty,font,empty)");
1888                                 }
1889                         }
1890                 }
1891
1892                 private void MeasureString_StringFontInt (string s)
1893                 {
1894                         if (font == null)
1895                                 Assert.Ignore ("Couldn't create required font");
1896
1897                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1898                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1899                                         SizeF size0 = g.MeasureString (s, font, 0);
1900                                         SizeF sizeN = g.MeasureString (s, font, Int32.MinValue);
1901                                         SizeF sizeP = g.MeasureString (s, font, Int32.MaxValue);
1902                                         Assert.AreEqual (size0, sizeN, "0-Min");
1903                                         Assert.AreEqual (size0, sizeP, "0-Max");
1904                                 }
1905                         }
1906                 }
1907
1908                 [Test]
1909                 public void MeasureString_StringFontInt_ShortString ()
1910                 {
1911                         MeasureString_StringFontInt ("a");
1912                 }
1913
1914                 [Test]
1915                 public void MeasureString_StringFontInt_LongString ()
1916                 {
1917                         HostIgnoreList.CheckTest ("MonoTests.System.Drawing.GraphicsTest.MeasureString_StringFontInt_LongString");
1918                         MeasureString_StringFontInt ("A very long string..."); // see bug #79643
1919                 }
1920
1921                 [Test]
1922                 public void MeasureString_StringFormat_Alignment ()
1923                 {
1924                         if (font == null)
1925                                 Assert.Ignore ("Couldn't create required font");
1926
1927                         string text = "Hello Mono::";
1928                         StringFormat string_format = new StringFormat ();
1929
1930                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1931                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1932                                         string_format.Alignment = StringAlignment.Near;
1933                                         SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1934
1935                                         string_format.Alignment = StringAlignment.Center;
1936                                         SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1937
1938                                         string_format.Alignment = StringAlignment.Far;
1939                                         SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1940
1941                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1942                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1943
1944                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1945                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1946                                 }
1947                         }
1948                 }
1949
1950                 [Test]
1951                 public void MeasureString_StringFormat_Alignment_DirectionVertical ()
1952                 {
1953                         if (font == null)
1954                                 Assert.Ignore ("Couldn't create required font");
1955
1956                         string text = "Hello Mono::";
1957                         StringFormat string_format = new StringFormat ();
1958                         string_format.FormatFlags = StringFormatFlags.DirectionVertical;
1959
1960                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1961                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1962                                         string_format.Alignment = StringAlignment.Near;
1963                                         SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1964
1965                                         string_format.Alignment = StringAlignment.Center;
1966                                         SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1967
1968                                         string_format.Alignment = StringAlignment.Far;
1969                                         SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1970
1971                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1972                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1973
1974                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1975                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1976                                 }
1977                         }
1978                 }
1979
1980                 [Test]
1981                 public void MeasureString_StringFormat_LineAlignment ()
1982                 {
1983                         if (font == null)
1984                                 Assert.Ignore ("Couldn't create required font");
1985
1986                         string text = "Hello Mono::";
1987                         StringFormat string_format = new StringFormat ();
1988
1989                         using (Bitmap bitmap = new Bitmap (20, 20)) {
1990                                 using (Graphics g = Graphics.FromImage (bitmap)) {
1991                                         string_format.LineAlignment = StringAlignment.Near;
1992                                         SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1993
1994                                         string_format.LineAlignment = StringAlignment.Center;
1995                                         SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1996
1997                                         string_format.LineAlignment = StringAlignment.Far;
1998                                         SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1999
2000                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2001                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2002
2003                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2004                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2005                                 }
2006                         }
2007                 }
2008
2009                 [Test]
2010                 public void MeasureString_StringFormat_LineAlignment_DirectionVertical ()
2011                 {
2012                         if (font == null)
2013                                 Assert.Ignore ("Couldn't create required font");
2014
2015                         string text = "Hello Mono::";
2016                         StringFormat string_format = new StringFormat ();
2017                         string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2018
2019                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2020                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2021                                         string_format.LineAlignment = StringAlignment.Near;
2022                                         SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
2023
2024                                         string_format.LineAlignment = StringAlignment.Center;
2025                                         SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
2026
2027                                         string_format.LineAlignment = StringAlignment.Far;
2028                                         SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
2029
2030                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2031                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2032
2033                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2034                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2035                                 }
2036                         }
2037                 }
2038
2039                 [Test]
2040                 public void MeasureString_MultlineString_Width ()
2041                 {
2042                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2043                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2044                                         StringFormat string_format = new StringFormat ();
2045
2046                                         string text1 = "Test\nTest123\nTest 456\nTest 1,2,3,4,5...";
2047                                         string text2 = "Test 1,2,3,4,5...";
2048
2049                                         SizeF size1 = g.MeasureString (text1, font, SizeF.Empty, string_format);
2050                                         SizeF size2 = g.MeasureString (text2, font, SizeF.Empty, string_format);
2051
2052                                         Assert.AreEqual ((int) size1.Width, (int) size2.Width, "Multiline Text Width");
2053                                 }
2054                         }
2055                 }
2056
2057                 [Test]
2058                 public void MeasureString_Bug76664 ()
2059                 {
2060                         if (font == null)
2061                                 Assert.Ignore ("Couldn't create required font");
2062
2063                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2064                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2065                                         string s = "aaa aa aaaa a aaa";
2066                                         SizeF size = g.MeasureString (s, font);
2067
2068                                         int chars, lines;
2069                                         SizeF size2 = g.MeasureString (s, font, new SizeF (80, size.Height), null, out chars, out lines);
2070
2071                                         // in pixels
2072                                         Assert.IsTrue (size2.Width < size.Width, "Width/pixel");
2073                                         Assert.AreEqual (size2.Height, size.Height, "Height/pixel");
2074
2075                                         Assert.AreEqual (1, lines, "lines fitted");
2076                                         // LAMESPEC: documentation seems to suggest chars is total length
2077                                         Assert.IsTrue (chars < s.Length, "characters fitted");
2078                                 }
2079                         }
2080                 }
2081
2082                 [Test]
2083                 public void MeasureString_Bug80680 ()
2084                 {
2085                         if (font == null)
2086                                 Assert.Ignore ("Couldn't create required font");
2087
2088                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2089                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2090                                         string s = String.Empty;
2091                                         SizeF size = g.MeasureString (s, font);
2092                                         Assert.AreEqual (0, size.Height, "Empty.Height");
2093                                         Assert.AreEqual (0, size.Width, "Empty.Width");
2094
2095                                         s += " ";
2096                                         SizeF expected = g.MeasureString (s, font);
2097                                         for (int i = 1; i < 10; i++) {
2098                                                 s += " ";
2099                                                 size = g.MeasureString (s, font);
2100                                                 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2101                                                 Assert.AreEqual (expected.Width, size.Width, 0.1, ">" + s + "< Width");
2102                                         }
2103
2104                                         s = "a";
2105                                         expected = g.MeasureString (s, font);
2106                                         s = " " + s;
2107                                         size = g.MeasureString (s, font);
2108                                         float space_width = size.Width - expected.Width;
2109                                         for (int i = 1; i < 10; i++) {
2110                                                 size = g.MeasureString (s, font);
2111                                                 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2112                                                 Assert.AreEqual (expected.Width + i * space_width, size.Width, 0.1, ">" + s + "< Width");
2113                                                 s = " " + s;
2114                                         }
2115
2116                                         s = "a";
2117                                         expected = g.MeasureString (s, font);
2118                                         for (int i = 1; i < 10; i++) {
2119                                                 s = s + " ";
2120                                                 size = g.MeasureString (s, font);
2121                                                 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2122                                                 Assert.AreEqual (expected.Width, size.Width, 0.1, ">" + s + "< Width");
2123                                         }
2124                                 }
2125                         }
2126                 }
2127
2128                 [Test]
2129                 public void MeasureCharacterRanges_NullOrEmptyText ()
2130                 {
2131                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2132                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2133                                         Region[] regions = g.MeasureCharacterRanges (null, font, new RectangleF (), null);
2134                                         AssertEquals ("text null", 0, regions.Length);
2135                                         regions = g.MeasureCharacterRanges (String.Empty, font, new RectangleF (), null);
2136                                         AssertEquals ("text empty", 0, regions.Length);
2137                                         // null font is ok with null or empty string
2138                                         regions = g.MeasureCharacterRanges (null, null, new RectangleF (), null);
2139                                         AssertEquals ("text null/null font", 0, regions.Length);
2140                                         regions = g.MeasureCharacterRanges (String.Empty, null, new RectangleF (), null);
2141                                         AssertEquals ("text empty/null font", 0, regions.Length);
2142                                 }
2143                         }
2144                 }
2145
2146                 [Test]
2147                 public void MeasureCharacterRanges_EmptyStringFormat ()
2148                 {
2149                         if (font == null)
2150                                 Assert.Ignore ("Couldn't create required font");
2151
2152                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2153                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2154                                         // string format without character ranges
2155                                         Region[] regions = g.MeasureCharacterRanges ("Mono", font, new RectangleF (), new StringFormat ());
2156                                         AssertEquals ("empty stringformat", 0, regions.Length);
2157                                 }
2158                         }
2159                 }
2160
2161                 [Test]
2162                 [ExpectedException (typeof (ArgumentNullException))]
2163                 public void MeasureCharacterRanges_FontNull ()
2164                 {
2165                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2166                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2167                                         g.MeasureCharacterRanges ("a", null, new RectangleF (), null);
2168                                 }
2169                         }
2170                 }
2171
2172                 [Test] // adapted from bug #78777
2173                 public void MeasureCharacterRanges_TwoLines ()
2174                 {
2175                         if (font == null)
2176                                 Assert.Ignore ("Couldn't create required font");
2177
2178                         string text = "this\nis a test";
2179                         CharacterRange[] ranges = new CharacterRange[2];
2180                         ranges[0] = new CharacterRange (0, 5);
2181                         ranges[1] = new CharacterRange (5, 9);
2182
2183                         StringFormat string_format = new StringFormat ();
2184                         string_format.FormatFlags = StringFormatFlags.NoClip;
2185                         string_format.SetMeasurableCharacterRanges (ranges);
2186
2187                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2188                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2189                                         SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2190                                         RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2191                                         Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2192
2193                                         AssertEquals ("Length", 2, regions.Length);
2194                                         AssertEquals ("Height", regions[0].GetBounds (g).Height, regions[1].GetBounds (g).Height);
2195                                 }
2196                         }
2197                 }
2198
2199                 private void MeasureCharacterRanges (string text, int first, int length)
2200                 {
2201                         if (font == null)
2202                                 Assert.Ignore ("Couldn't create required font");
2203
2204                         CharacterRange[] ranges = new CharacterRange[1];
2205                         ranges[0] = new CharacterRange (first, length);
2206
2207                         StringFormat string_format = new StringFormat ();
2208                         string_format.FormatFlags = StringFormatFlags.NoClip;
2209                         string_format.SetMeasurableCharacterRanges (ranges);
2210
2211                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2212                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2213                                         SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2214                                         RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2215                                         g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2216                                 }
2217                         }
2218                 }
2219
2220                 [Test]
2221                 [ExpectedException (typeof (ArgumentException))]
2222                 public void MeasureCharacterRanges_FirstTooFar ()
2223                 {
2224                         string text = "this\nis a test";
2225                         MeasureCharacterRanges (text, text.Length, 1);
2226                 }
2227
2228                 [Test]
2229                 [ExpectedException (typeof (ArgumentException))]
2230                 public void MeasureCharacterRanges_LengthTooLong ()
2231                 {
2232                         string text = "this\nis a test";
2233                         MeasureCharacterRanges (text, 0, text.Length + 1);
2234                 }
2235
2236                 [Test]
2237                 public void MeasureCharacterRanges_Prefix ()
2238                 {
2239                         if (font == null)
2240                                 Assert.Ignore ("Couldn't create required font");
2241
2242                         string text = "Hello &Mono::";
2243                         CharacterRange[] ranges = new CharacterRange[1];
2244                         ranges[0] = new CharacterRange (5, 4);
2245
2246                         StringFormat string_format = new StringFormat ();
2247                         string_format.SetMeasurableCharacterRanges (ranges);
2248
2249                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2250                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2251                                         SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2252                                         RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2253
2254                                         // here & is part of the measure and visible
2255                                         string_format.HotkeyPrefix = HotkeyPrefix.None;
2256                                         Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2257                                         RectangleF bounds_none = regions[0].GetBounds (g);
2258
2259                                         // here & is part of the measure (range) but visible as an underline
2260                                         string_format.HotkeyPrefix = HotkeyPrefix.Show;
2261                                         regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2262                                         RectangleF bounds_show = regions[0].GetBounds (g);
2263                                         Assert.IsTrue (bounds_show.Width < bounds_none.Width, "Show<None");
2264
2265                                         // here & is part of the measure (range) but invisible
2266                                         string_format.HotkeyPrefix = HotkeyPrefix.Hide;
2267                                         regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2268                                         RectangleF bounds_hide = regions[0].GetBounds (g);
2269                                         AssertEquals ("Hide==None", bounds_hide.Width, bounds_show.Width);
2270                                 }
2271                         }
2272                 }
2273
2274                 [Test]
2275                 [ExpectedException (typeof (ArgumentException))]
2276                 public void MeasureCharacterRanges_NullStringFormat ()
2277                 {
2278                         if (font == null)
2279                                 Assert.Ignore ("Couldn't create required font");
2280
2281                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2282                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2283                                         g.MeasureCharacterRanges ("Mono", font, new RectangleF (), null);
2284                                 }
2285                         }
2286                 }
2287
2288                 [Test]
2289                 [Category ("NotWorking")]
2290                 public void MeasureCharacterRanges_StringFormat_Alignment ()
2291                 {
2292                         if (font == null)
2293                                 Assert.Ignore ("Couldn't create required font");
2294
2295                         string text = "Hello Mono::";
2296                         CharacterRange[] ranges = new CharacterRange[1];
2297                         ranges[0] = new CharacterRange (5, 4);
2298                         StringFormat string_format = new StringFormat ();
2299                         string_format.SetMeasurableCharacterRanges (ranges);
2300
2301                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2302                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2303                                         string_format.Alignment = StringAlignment.Near;
2304                                         Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2305                                         Assert.AreEqual (1, regions.Length, "Near.Region");
2306                                         RectangleF near = regions[0].GetBounds (g);
2307
2308                                         string_format.Alignment = StringAlignment.Center;
2309                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2310                                         Assert.AreEqual (1, regions.Length, "Center.Region");
2311                                         RectangleF center = regions[0].GetBounds (g);
2312
2313                                         string_format.Alignment = StringAlignment.Far;
2314                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2315                                         Assert.AreEqual (1, regions.Length, "Far.Region");
2316                                         RectangleF far = regions[0].GetBounds (g);
2317
2318                                         Assert.IsTrue (near.X < center.X, "near-center/X");
2319                                         Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y");
2320                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2321                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2322
2323                                         Assert.IsTrue (center.X < far.X, "center-far/X");
2324                                         Assert.AreEqual (center.Y, far.Y, "center-far/Y");
2325                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2326                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2327                                 }
2328                         }
2329                 }
2330
2331                 [Test]
2332                 [Category ("NotWorking")]
2333                 public void MeasureCharacterRanges_StringFormat_LineAlignment ()
2334                 {
2335                         if (font == null)
2336                                 Assert.Ignore ("Couldn't create required font");
2337
2338                         string text = "Hello Mono::";
2339                         CharacterRange[] ranges = new CharacterRange[1];
2340                         ranges[0] = new CharacterRange (5, 4);
2341                         StringFormat string_format = new StringFormat ();
2342                         string_format.SetMeasurableCharacterRanges (ranges);
2343
2344                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2345                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2346                                         string_format.LineAlignment = StringAlignment.Near;
2347                                         Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2348                                         Assert.AreEqual (1, regions.Length, "Near.Region");
2349                                         RectangleF near = regions[0].GetBounds (g);
2350
2351                                         string_format.LineAlignment = StringAlignment.Center;
2352                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2353                                         Assert.AreEqual (1, regions.Length, "Center.Region");
2354                                         RectangleF center = regions[0].GetBounds (g);
2355
2356                                         string_format.LineAlignment = StringAlignment.Far;
2357                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2358                                         Assert.AreEqual (1, regions.Length, "Far.Region");
2359                                         RectangleF far = regions[0].GetBounds (g);
2360
2361                                         Assert.AreEqual (near.X, center.X, 0.1, "near-center/X");
2362                                         Assert.IsTrue (near.Y < center.Y, "near-center/Y");
2363                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2364                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2365
2366                                         Assert.AreEqual (center.X, far.X, 0.1, "center-far/X");
2367                                         Assert.IsTrue (center.Y < far.Y, "center-far/Y");
2368                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2369                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2370                                 }
2371                         }
2372                 }
2373
2374                 [Test]
2375                 [Category ("NotWorking")]
2376                 public void MeasureCharacterRanges_StringFormat_Alignment_DirectionVertical ()
2377                 {
2378                         if (font == null)
2379                                 Assert.Ignore ("Couldn't create required font");
2380
2381                         string text = "Hello Mono::";
2382                         CharacterRange[] ranges = new CharacterRange[1];
2383                         ranges[0] = new CharacterRange (5, 4);
2384                         StringFormat string_format = new StringFormat ();
2385                         string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2386                         string_format.SetMeasurableCharacterRanges (ranges);
2387
2388                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2389                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2390                                         string_format.Alignment = StringAlignment.Near;
2391                                         Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2392                                         Assert.AreEqual (1, regions.Length, "Near.Region");
2393                                         RectangleF near = regions[0].GetBounds (g);
2394
2395                                         string_format.Alignment = StringAlignment.Center;
2396                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2397                                         Assert.AreEqual (1, regions.Length, "Center.Region");
2398                                         RectangleF center = regions[0].GetBounds (g);
2399
2400                                         string_format.Alignment = StringAlignment.Far;
2401                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2402                                         Assert.AreEqual (1, regions.Length, "Far.Region");
2403                                         RectangleF far = regions[0].GetBounds (g);
2404
2405                                         Assert.IsTrue (near.X == center.X, "near-center/X"); // ???
2406                                         Assert.IsTrue (near.Y < center.Y, "near-center/Y");
2407                                         Assert.IsTrue (near.Width == center.Width, "near-center/Width"); // ???
2408                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2409
2410                                         Assert.AreEqual (center.X, far.X, 0.1, "center-far/X");
2411                                         Assert.IsTrue (center.Y < far.Y, "center-far/Y");
2412                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2413                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2414                                 }
2415                         }
2416                 }
2417
2418                 [Test]
2419                 [Category ("NotWorking")]
2420                 public void MeasureCharacterRanges_StringFormat_LineAlignment_DirectionVertical ()
2421                 {
2422                         if (font == null)
2423                                 Assert.Ignore ("Couldn't create required font");
2424
2425                         string text = "Hello Mono::";
2426                         CharacterRange[] ranges = new CharacterRange[1];
2427                         ranges[0] = new CharacterRange (5, 4);
2428                         StringFormat string_format = new StringFormat ();
2429                         string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2430                         string_format.SetMeasurableCharacterRanges (ranges);
2431
2432                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2433                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2434                                         string_format.LineAlignment = StringAlignment.Near;
2435                                         Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2436                                         Assert.AreEqual (1, regions.Length, "Near.Region");
2437                                         RectangleF near = regions[0].GetBounds (g);
2438
2439                                         string_format.LineAlignment = StringAlignment.Center;
2440                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2441                                         Assert.AreEqual (1, regions.Length, "Center.Region");
2442                                         RectangleF center = regions[0].GetBounds (g);
2443
2444                                         string_format.LineAlignment = StringAlignment.Far;
2445                                         regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2446                                         Assert.AreEqual (1, regions.Length, "Far.Region");
2447                                         RectangleF far = regions[0].GetBounds (g);
2448
2449                                         Assert.IsTrue (near.X < center.X, "near-center/X");
2450                                         Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y");
2451                                         Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2452                                         Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2453
2454                                         Assert.IsTrue (center.X < far.X, "center-far/X");
2455                                         Assert.AreEqual (center.Y, far.Y, 0.1, "center-far/Y");
2456                                         Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2457                                         Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2458                                 }
2459                         }
2460                 }
2461
2462                 static CharacterRange [] ranges = new CharacterRange [] {
2463                     new CharacterRange (0, 1),
2464                     new CharacterRange (1, 1),
2465                     new CharacterRange (2, 1)
2466                 };
2467
2468                 Region [] Measure (Graphics gfx, RectangleF rect)
2469                 {
2470                         using (StringFormat format = StringFormat.GenericTypographic) {
2471                                 format.SetMeasurableCharacterRanges (ranges);
2472
2473                                 using (Font font = new Font (FontFamily.GenericSerif, 11.0f)) {
2474                                         return gfx.MeasureCharacterRanges ("abc", font, rect, format);
2475                                 }
2476                         }
2477                 }
2478
2479                 [Test]
2480                 public void Measure ()
2481                 {
2482                         using (Graphics gfx = Graphics.FromImage (new Bitmap (1, 1))) {
2483                                 Region [] zero = Measure (gfx, new RectangleF (0, 0, 0, 0));
2484                                 Assert.AreEqual (3, zero.Length, "zero.Length");
2485
2486                                 Region [] small = Measure (gfx, new RectangleF (0, 0, 100, 100));
2487                                 Assert.AreEqual (3, small.Length, "small.Length");
2488                                 for (int i = 0; i < 3; i++ ) {
2489                                         RectangleF zb = zero [i].GetBounds (gfx);
2490                                         RectangleF sb = small [i].GetBounds (gfx);
2491                                         Assert.AreEqual (sb.X, zb.X, "sx" + i.ToString ());
2492                                         Assert.AreEqual (sb.Y, zb.Y, "sy" + i.ToString ());
2493                                         Assert.AreEqual (sb.Width, zb.Width, "sw" + i.ToString ());
2494                                         Assert.AreEqual (sb.Height, zb.Height, "sh" + i.ToString ());
2495                                 }
2496
2497                                 Region [] max = Measure (gfx, new RectangleF (0, 0, Single.MaxValue, Single.MaxValue));
2498                                 Assert.AreEqual (3, max.Length, "empty.Length");
2499                                 for (int i = 0; i < 3; i++) {
2500                                         RectangleF zb = zero [i].GetBounds (gfx);
2501                                         RectangleF mb = max [i].GetBounds (gfx);
2502                                         Assert.AreEqual (mb.X, zb.X, "mx" + i.ToString ());
2503                                         Assert.AreEqual (mb.Y, zb.Y, "my" + i.ToString ());
2504                                         Assert.AreEqual (mb.Width, zb.Width, "mw" + i.ToString ());
2505                                         Assert.AreEqual (mb.Height, zb.Height, "mh" + i.ToString ());
2506                                 }
2507                         }
2508                 }
2509
2510                 [Test]
2511                 public void MeasureLimits ()
2512                 {
2513                         using (Graphics gfx = Graphics.FromImage (new Bitmap (1, 1))) {
2514                                 Region [] min = Measure (gfx, new RectangleF (0, 0, Single.MinValue, Single.MinValue));
2515                                 Assert.AreEqual (3, min.Length, "origin.Length");
2516                                 for (int i = 0; i < 3; i++) {
2517                                         RectangleF mb = min [i].GetBounds (gfx);
2518                                         Assert.AreEqual (-4194304.0f, mb.X, "minx" + i.ToString ());
2519                                         Assert.AreEqual (-4194304.0f, mb.Y, "miny" + i.ToString ());
2520                                         Assert.AreEqual (8388608.0f, mb.Width, "minw" + i.ToString ());
2521                                         Assert.AreEqual (8388608.0f, mb.Height, "minh" + i.ToString ());
2522                                 }
2523
2524                                 Region [] neg = Measure (gfx, new RectangleF (0, 0, -20, -20));
2525                                 Assert.AreEqual (3, neg.Length, "neg.Length");
2526                                 for (int i = 0; i < 3; i++) {
2527                                         RectangleF mb = neg [i].GetBounds (gfx);
2528                                         Assert.AreEqual (-4194304.0f, mb.X, "minx" + i.ToString ());
2529                                         Assert.AreEqual (-4194304.0f, mb.Y, "miny" + i.ToString ());
2530                                         Assert.AreEqual (8388608.0f, mb.Width, "minw" + i.ToString ());
2531                                         Assert.AreEqual (8388608.0f, mb.Height, "minh" + i.ToString ());
2532                                 }
2533                         }
2534                 }
2535
2536                 [Test]
2537                 public void DrawString_EndlessLoop_Bug77699 ()
2538                 {
2539                         if (font == null)
2540                                 Assert.Ignore ("Couldn't create required font");
2541
2542                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2543                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2544                                         Rectangle rect = Rectangle.Empty;
2545                                         rect.Location = new Point (10, 10);
2546                                         rect.Size = new Size (1, 20);
2547                                         StringFormat fmt = new StringFormat ();
2548                                         fmt.Alignment = StringAlignment.Center;
2549                                         fmt.LineAlignment = StringAlignment.Center;
2550                                         fmt.FormatFlags = StringFormatFlags.NoWrap;
2551                                         fmt.Trimming = StringTrimming.EllipsisWord;
2552                                         g.DrawString ("Test String", font, Brushes.Black, rect, fmt);
2553                                 }
2554                         }
2555                 }
2556
2557                 [Test]
2558                 public void DrawString_EndlessLoop_Wrapping ()
2559                 {
2560                         if (font == null)
2561                                 Assert.Ignore ("Couldn't create required font");
2562
2563                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2564                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2565                                         Rectangle rect = Rectangle.Empty;
2566                                         rect.Location = new Point (10, 10);
2567                                         rect.Size = new Size (1, 20);
2568                                         StringFormat fmt = new StringFormat ();
2569                                         fmt.Alignment = StringAlignment.Center;
2570                                         fmt.LineAlignment = StringAlignment.Center;
2571                                         fmt.Trimming = StringTrimming.EllipsisWord;
2572                                         g.DrawString ("Test String", font, Brushes.Black, rect, fmt);
2573                                 }
2574                         }
2575                 }
2576
2577                 [Test]
2578                 public void MeasureString_Wrapping_Dots ()
2579                 {
2580                         HostIgnoreList.CheckTest ("MonoTests.System.Drawing.GraphicsTest.MeasureString_Wrapping_Dots");
2581
2582                         if (font == null)
2583                                 Assert.Ignore ("Couldn't create required font");
2584
2585                         string text = "this is really long text........................................... with a lot o periods.";
2586                         using (Bitmap bitmap = new Bitmap (20, 20)) {
2587                                 using (Graphics g = Graphics.FromImage (bitmap)) {
2588                                         using (StringFormat format = new StringFormat ()) {
2589                                                 format.Alignment = StringAlignment.Center;
2590                                                 SizeF sz = g.MeasureString (text, font, 80, format);
2591                                                 Assert.IsTrue (sz.Width < 80, "Width");
2592                                                 Assert.IsTrue (sz.Height > font.Height * 2, "Height");
2593                                         }
2594                                 }
2595                         }
2596                 }
2597
2598                 [Test]
2599                 public void GetReleaseHdcInternal ()
2600                 {
2601                         using (Bitmap b = new Bitmap (10, 10)) {
2602                                 using (Graphics g = Graphics.FromImage (b)) {
2603                                         IntPtr hdc1 = g.GetHdc ();
2604                                         g.ReleaseHdcInternal (hdc1);
2605                                         IntPtr hdc2 = g.GetHdc ();
2606                                         g.ReleaseHdcInternal (hdc2);
2607                                         Assert.AreEqual (hdc1, hdc2, "hdc");
2608                                 }
2609                         }
2610                 }
2611
2612                 [Test]
2613                 [ExpectedException (typeof (ArgumentException))]
2614                 public void ReleaseHdcInternal_IntPtrZero ()
2615                 {
2616                         using (Bitmap b = new Bitmap (10, 10)) {
2617                                 using (Graphics g = Graphics.FromImage (b)) {
2618                                         g.ReleaseHdcInternal (IntPtr.Zero);
2619                                 }
2620                         }
2621                 }
2622
2623                 [Test]
2624                 [ExpectedException (typeof (ArgumentException))]
2625                 public void ReleaseHdcInternal_TwoTimes ()
2626                 {
2627                         using (Bitmap b = new Bitmap (10, 10)) {
2628                                 using (Graphics g = Graphics.FromImage (b)) {
2629                                         IntPtr hdc = g.GetHdc ();
2630                                         g.ReleaseHdcInternal (hdc);
2631                                         g.ReleaseHdcInternal (hdc);
2632                                 }
2633                         }
2634                 }
2635 #if NET_2_0
2636                 [Test]
2637                 public void TestReleaseHdc ()
2638                 {
2639                         using (Bitmap b = new Bitmap (10, 10)) {
2640                                 using (Graphics g = Graphics.FromImage (b)) {
2641                                         IntPtr hdc1 = g.GetHdc ();
2642                                         g.ReleaseHdc ();
2643                                         IntPtr hdc2 = g.GetHdc ();
2644                                         g.ReleaseHdc ();
2645                                         Assert.AreEqual (hdc1, hdc2, "hdc");
2646                                 }
2647                         }
2648                 }
2649
2650                 [Test]
2651                 [ExpectedException (typeof (ArgumentException))]
2652                 public void TestReleaseHdcException ()
2653                 {
2654                         using (Bitmap b = new Bitmap (10, 10)) {
2655                                 using (Graphics g = Graphics.FromImage (b)) {
2656                                         g.ReleaseHdc ();
2657                                 }
2658                         }
2659                 }
2660
2661                 [Test]
2662                 [ExpectedException (typeof (ArgumentException))]
2663                 public void TestReleaseHdcException2 ()
2664                 {
2665                         using (Bitmap b = new Bitmap (10, 10)) {
2666                                 using (Graphics g = Graphics.FromImage (b)) {
2667                                         g.GetHdc ();
2668                                         g.ReleaseHdc ();
2669                                         g.ReleaseHdc ();
2670                                 }
2671                         }
2672                 }
2673 #endif
2674                 [Test]
2675                 public void VisibleClipBound ()
2676                 {
2677                         // see #78958
2678                         using (Bitmap bmp = new Bitmap (100, 100)) {
2679                                 using (Graphics g = Graphics.FromImage (bmp)) {
2680                                         RectangleF noclip = g.VisibleClipBounds;
2681                                         Assert.AreEqual (0, noclip.X, "noclip.X");
2682                                         Assert.AreEqual (0, noclip.Y, "noclip.Y");
2683                                         Assert.AreEqual (100, noclip.Width, "noclip.Width");
2684                                         Assert.AreEqual (100, noclip.Height, "noclip.Height");
2685
2686                                         // note: libgdiplus regions are precise to multiple of multiple of 8
2687                                         g.Clip = new Region (new RectangleF (0, 0, 32, 32));
2688                                         RectangleF clip = g.VisibleClipBounds;
2689                                         Assert.AreEqual (0, clip.X, "clip.X");
2690                                         Assert.AreEqual (0, clip.Y, "clip.Y");
2691                                         Assert.AreEqual (32, clip.Width, "clip.Width");
2692                                         Assert.AreEqual (32, clip.Height, "clip.Height");
2693
2694                                         g.RotateTransform (90);
2695                                         RectangleF rotclip = g.VisibleClipBounds;
2696                                         Assert.AreEqual (0, rotclip.X, "rotclip.X");
2697                                         Assert.AreEqual (-32, rotclip.Y, "rotclip.Y");
2698                                         Assert.AreEqual (32, rotclip.Width, "rotclip.Width");
2699                                         Assert.AreEqual (32, rotclip.Height, "rotclip.Height");
2700                                 }
2701                         }
2702                 }
2703
2704                 [Test]
2705                 public void VisibleClipBound_BigClip ()
2706                 {
2707                         using (Bitmap bmp = new Bitmap (100, 100)) {
2708                                 using (Graphics g = Graphics.FromImage (bmp)) {
2709                                         RectangleF noclip = g.VisibleClipBounds;
2710                                         Assert.AreEqual (0, noclip.X, "noclip.X");
2711                                         Assert.AreEqual (0, noclip.Y, "noclip.Y");
2712                                         Assert.AreEqual (100, noclip.Width, "noclip.Width");
2713                                         Assert.AreEqual (100, noclip.Height, "noclip.Height");
2714
2715                                         // clip is larger than bitmap
2716                                         g.Clip = new Region (new RectangleF (0, 0, 200, 200));
2717                                         RectangleF clipbound = g.ClipBounds;
2718                                         Assert.AreEqual (0, clipbound.X, "clipbound.X");
2719                                         Assert.AreEqual (0, clipbound.Y, "clipbound.Y");
2720                                         Assert.AreEqual (200, clipbound.Width, "clipbound.Width");
2721                                         Assert.AreEqual (200, clipbound.Height, "clipbound.Height");
2722
2723                                         RectangleF clip = g.VisibleClipBounds;
2724                                         Assert.AreEqual (0, clip.X, "clip.X");
2725                                         Assert.AreEqual (0, clip.Y, "clip.Y");
2726                                         Assert.AreEqual (100, clip.Width, "clip.Width");
2727                                         Assert.AreEqual (100, clip.Height, "clip.Height");
2728
2729                                         g.RotateTransform (90);
2730                                         RectangleF rotclipbound = g.ClipBounds;
2731                                         Assert.AreEqual (0, rotclipbound.X, "rotclipbound.X");
2732                                         Assert.AreEqual (-200, rotclipbound.Y, "rotclipbound.Y");
2733                                         Assert.AreEqual (200, rotclipbound.Width, "rotclipbound.Width");
2734                                         Assert.AreEqual (200, rotclipbound.Height, "rotclipbound.Height");
2735
2736                                         RectangleF rotclip = g.VisibleClipBounds;
2737                                         Assert.AreEqual (0, rotclip.X, "rotclip.X");
2738                                         Assert.AreEqual (-100, rotclip.Y, "rotclip.Y");
2739                                         Assert.AreEqual (100, rotclip.Width, "rotclip.Width");
2740                                         Assert.AreEqual (100, rotclip.Height, "rotclip.Height");
2741                                 }
2742                         }
2743                 }
2744
2745                 [Test]
2746                 public void Rotate ()
2747                 {
2748                         using (Bitmap bmp = new Bitmap (100, 50)) {
2749                                 using (Graphics g = Graphics.FromImage (bmp)) {
2750                                         RectangleF vcb = g.VisibleClipBounds;
2751                                         Assert.AreEqual (0, vcb.X, "vcb.X");
2752                                         Assert.AreEqual (0, vcb.Y, "vcb.Y");
2753                                         Assert.AreEqual (100, vcb.Width, "vcb.Width");
2754                                         Assert.AreEqual (50, vcb.Height, "vcb.Height");
2755
2756                                         g.RotateTransform (90);
2757                                         RectangleF rvcb = g.VisibleClipBounds;
2758                                         Assert.AreEqual (0, rvcb.X, "rvcb.X");
2759                                         Assert.AreEqual (-100, rvcb.Y, "rvcb.Y");
2760                                         Assert.AreEqual (50.0f, rvcb.Width, 0.0001, "rvcb.Width");
2761                                         Assert.AreEqual (100, rvcb.Height, "rvcb.Height");
2762                                 }
2763                         }
2764                 }
2765
2766                 [Test]
2767                 public void Scale ()
2768                 {
2769                         using (Bitmap bmp = new Bitmap (100, 50)) {
2770                                 using (Graphics g = Graphics.FromImage (bmp)) {
2771                                         RectangleF vcb = g.VisibleClipBounds;
2772                                         Assert.AreEqual (0, vcb.X, "vcb.X");
2773                                         Assert.AreEqual (0, vcb.Y, "vcb.Y");
2774                                         Assert.AreEqual (100, vcb.Width, "vcb.Width");
2775                                         Assert.AreEqual (50, vcb.Height, "vcb.Height");
2776
2777                                         g.ScaleTransform (2, 0.5f);
2778                                         RectangleF svcb = g.VisibleClipBounds;
2779                                         Assert.AreEqual (0, svcb.X, "svcb.X");
2780                                         Assert.AreEqual (0, svcb.Y, "svcb.Y");
2781                                         Assert.AreEqual (50, svcb.Width, "svcb.Width");
2782                                         Assert.AreEqual (100, svcb.Height, "svcb.Height");
2783                                 }
2784                         }
2785                 }
2786
2787                 [Test]
2788                 public void Translate ()
2789                 {
2790                         using (Bitmap bmp = new Bitmap (100, 50)) {
2791                                 using (Graphics g = Graphics.FromImage (bmp)) {
2792                                         RectangleF vcb = g.VisibleClipBounds;
2793                                         Assert.AreEqual (0, vcb.X, "vcb.X");
2794                                         Assert.AreEqual (0, vcb.Y, "vcb.Y");
2795                                         Assert.AreEqual (100, vcb.Width, "vcb.Width");
2796                                         Assert.AreEqual (50, vcb.Height, "vcb.Height");
2797
2798                                         g.TranslateTransform (-25, 25);
2799                                         RectangleF tvcb = g.VisibleClipBounds;
2800                                         Assert.AreEqual (25, tvcb.X, "tvcb.X");
2801                                         Assert.AreEqual (-25, tvcb.Y, "tvcb.Y");
2802                                         Assert.AreEqual (100, tvcb.Width, "tvcb.Width");
2803                                         Assert.AreEqual (50, tvcb.Height, "tvcb.Height");
2804                                 }
2805                         }
2806                 }
2807
2808                 [Test]
2809                 [ExpectedException (typeof (ArgumentNullException))]
2810                 public void DrawIcon_NullRectangle ()
2811                 {
2812                         using (Bitmap bmp = new Bitmap (40, 40)) {
2813                                 using (Graphics g = Graphics.FromImage (bmp)) {
2814                                         g.DrawIcon (null, new Rectangle (0, 0, 32, 32));
2815                                 }
2816                         }
2817                 }
2818
2819                 [Test]
2820                 public void DrawIcon_IconRectangle ()
2821                 {
2822                         using (Bitmap bmp = new Bitmap (40, 40)) {
2823                                 using (Graphics g = Graphics.FromImage (bmp)) {
2824                                         g.DrawIcon (SystemIcons.Application, new Rectangle (0, 0, 40, 20));
2825                                         // Rectangle is empty when X, Y, Width and Height == 0 
2826                                         // (yep X and Y too, RectangleF only checks for Width and Height)
2827                                         g.DrawIcon (SystemIcons.Asterisk, new Rectangle (0, 0, 0, 0));
2828                                         // so this one is half-empty ;-)
2829                                         g.DrawIcon (SystemIcons.Error, new Rectangle (20, 40, 0, 0));
2830                                         // negative width or height isn't empty (for Rectangle)
2831                                         g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (10, 20, -1, 0));
2832                                         g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (20, 10, 0, -1));
2833                                 }
2834                         }
2835                 }
2836
2837                 [Test]
2838                 [ExpectedException (typeof (ArgumentNullException))]
2839                 public void DrawIcon_NullIntInt ()
2840                 {
2841                         using (Bitmap bmp = new Bitmap (40, 40)) {
2842                                 using (Graphics g = Graphics.FromImage (bmp)) {
2843                                         g.DrawIcon (null, 4, 2);
2844                                 }
2845                         }
2846                 }
2847
2848                 [Test]
2849                 public void DrawIcon_IconIntInt ()
2850                 {
2851                         using (Bitmap bmp = new Bitmap (40, 40)) {
2852                                 using (Graphics g = Graphics.FromImage (bmp)) {
2853                                         g.DrawIcon (SystemIcons.Exclamation, 4, 2);
2854                                         g.DrawIcon (SystemIcons.Hand, 0, 0);
2855                                 }
2856                         }
2857                 }
2858
2859                 [Test]
2860                 [ExpectedException (typeof (ArgumentNullException))]
2861                 public void DrawIconUnstretched_NullRectangle ()
2862                 {
2863                         using (Bitmap bmp = new Bitmap (40, 40)) {
2864                                 using (Graphics g = Graphics.FromImage (bmp)) {
2865                                         g.DrawIconUnstretched (null, new Rectangle (0, 0, 40, 20));
2866                                 }
2867                         }
2868                 }
2869
2870                 [Test]
2871                 public void DrawIconUnstretched_IconRectangle ()
2872                 {
2873                         using (Bitmap bmp = new Bitmap (40, 40)) {
2874                                 using (Graphics g = Graphics.FromImage (bmp)) {
2875                                         g.DrawIconUnstretched (SystemIcons.Information, new Rectangle (0, 0, 40, 20));
2876                                         // Rectangle is empty when X, Y, Width and Height == 0 
2877                                         // (yep X and Y too, RectangleF only checks for Width and Height)
2878                                         g.DrawIconUnstretched (SystemIcons.Question, new Rectangle (0, 0, 0, 0));
2879                                         // so this one is half-empty ;-)
2880                                         g.DrawIconUnstretched (SystemIcons.Warning, new Rectangle (20, 40, 0, 0));
2881                                         // negative width or height isn't empty (for Rectangle)
2882                                         g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (10, 20, -1, 0));
2883                                         g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (20, 10, 0, -1));
2884                                 }
2885                         }
2886                 }
2887
2888                 [Test]
2889                 [ExpectedException (typeof (ArgumentNullException))]
2890                 public void DrawImage_NullRectangleF ()
2891                 {
2892                         using (Bitmap bmp = new Bitmap (40, 40)) {
2893                                 using (Graphics g = Graphics.FromImage (bmp)) {
2894                                         g.DrawImage (null, new RectangleF (0, 0, 0, 0));
2895                                 }
2896                         }
2897                 }
2898
2899                 [Test]
2900                 public void DrawImage_ImageRectangleF ()
2901                 {
2902                         using (Bitmap bmp = new Bitmap (40, 40)) {
2903                                 using (Graphics g = Graphics.FromImage (bmp)) {
2904                                         g.DrawImage (bmp, new RectangleF (0, 0, 0, 0));
2905                                         g.DrawImage (bmp, new RectangleF (20, 40, 0, 0));
2906                                         g.DrawImage (bmp, new RectangleF (10, 20, -1, 0));
2907                                         g.DrawImage (bmp, new RectangleF (20, 10, 0, -1));
2908                                 }
2909                         }
2910                 }
2911
2912                 [Test]
2913                 [ExpectedException (typeof (ArgumentNullException))]
2914                 public void DrawImage_NullPointF ()
2915                 {
2916                         using (Bitmap bmp = new Bitmap (40, 40)) {
2917                                 using (Graphics g = Graphics.FromImage (bmp)) {
2918                                         g.DrawImage (null, new PointF (0, 0));
2919                                 }
2920                         }
2921                 }
2922
2923                 [Test]
2924                 public void DrawImage_ImagePointF ()
2925                 {
2926                         using (Bitmap bmp = new Bitmap (40, 40)) {
2927                                 using (Graphics g = Graphics.FromImage (bmp)) {
2928                                         g.DrawImage (bmp, new PointF (0, 0));
2929                                 }
2930                         }
2931                 }
2932
2933                 [Test]
2934                 [ExpectedException (typeof (ArgumentNullException))]
2935                 public void DrawImage_NullPointFArray ()
2936                 {
2937                         using (Bitmap bmp = new Bitmap (40, 40)) {
2938                                 using (Graphics g = Graphics.FromImage (bmp)) {
2939                                         g.DrawImage (null, new PointF[0]);
2940                                 }
2941                         }
2942                 }
2943
2944                 [Test]
2945                 [ExpectedException (typeof (ArgumentNullException))]
2946                 public void DrawImage_ImagePointFArrayNull ()
2947                 {
2948                         using (Bitmap bmp = new Bitmap (40, 40)) {
2949                                 using (Graphics g = Graphics.FromImage (bmp)) {
2950                                         g.DrawImage (bmp, (PointF[]) null);
2951                                 }
2952                         }
2953                 }
2954
2955                 [Test]
2956                 [ExpectedException (typeof (ArgumentException))]
2957                 public void DrawImage_ImagePointFArrayEmpty ()
2958                 {
2959                         using (Bitmap bmp = new Bitmap (40, 40)) {
2960                                 using (Graphics g = Graphics.FromImage (bmp)) {
2961                                         g.DrawImage (bmp, new PointF[0]);
2962                                 }
2963                         }
2964                 }
2965
2966                 [Test]
2967                 public void DrawImage_ImagePointFArray ()
2968                 {
2969                         using (Bitmap bmp = new Bitmap (40, 40)) {
2970                                 using (Graphics g = Graphics.FromImage (bmp)) {
2971                                         g.DrawImage (bmp, new PointF[] { 
2972                                                 new PointF (0, 0), new PointF (1, 1), new PointF (2, 2) });
2973                                 }
2974                         }
2975                 }
2976
2977                 [Test]
2978                 [ExpectedException (typeof (ArgumentNullException))]
2979                 public void DrawImage_NullRectangle ()
2980                 {
2981                         using (Bitmap bmp = new Bitmap (40, 40)) {
2982                                 using (Graphics g = Graphics.FromImage (bmp)) {
2983                                         g.DrawImage (null, new Rectangle (0, 0, 0, 0));
2984                                 }
2985                         }
2986                 }
2987
2988                 [Test]
2989                 public void DrawImage_ImageRectangle ()
2990                 {
2991                         using (Bitmap bmp = new Bitmap (40, 40)) {
2992                                 using (Graphics g = Graphics.FromImage (bmp)) {
2993                                         // Rectangle is empty when X, Y, Width and Height == 0 
2994                                         // (yep X and Y too, RectangleF only checks for Width and Height)
2995                                         g.DrawImage (bmp, new Rectangle (0, 0, 0, 0));
2996                                         // so this one is half-empty ;-)
2997                                         g.DrawImage (bmp, new Rectangle (20, 40, 0, 0));
2998                                         // negative width or height isn't empty (for Rectangle)
2999                                         g.DrawImage (bmp, new Rectangle (10, 20, -1, 0));
3000                                         g.DrawImage (bmp, new Rectangle (20, 10, 0, -1));
3001                                 }
3002                         }
3003                 }
3004
3005                 [Test]
3006                 [ExpectedException (typeof (ArgumentNullException))]
3007                 public void DrawImage_NullPoint ()
3008                 {
3009                         using (Bitmap bmp = new Bitmap (40, 40)) {
3010                                 using (Graphics g = Graphics.FromImage (bmp)) {
3011                                         g.DrawImage (null, new Point (0, 0));
3012                                 }
3013                         }
3014                 }
3015
3016                 [Test]
3017                 public void DrawImage_ImagePoint ()
3018                 {
3019                         using (Bitmap bmp = new Bitmap (40, 40)) {
3020                                 using (Graphics g = Graphics.FromImage (bmp)) {
3021                                         g.DrawImage (bmp, new Point (0, 0));
3022                                 }
3023                         }
3024                 }
3025
3026                 [Test]
3027                 [ExpectedException (typeof (ArgumentNullException))]
3028                 public void DrawImage_NullPointArray ()
3029                 {
3030                         using (Bitmap bmp = new Bitmap (40, 40)) {
3031                                 using (Graphics g = Graphics.FromImage (bmp)) {
3032                                         g.DrawImage (null, new Point[0]);
3033                                 }
3034                         }
3035                 }
3036
3037                 [Test]
3038                 [ExpectedException (typeof (ArgumentNullException))]
3039                 public void DrawImage_ImagePointArrayNull ()
3040                 {
3041                         using (Bitmap bmp = new Bitmap (40, 40)) {
3042                                 using (Graphics g = Graphics.FromImage (bmp)) {
3043                                         g.DrawImage (bmp, (Point[]) null);
3044                                 }
3045                         }
3046                 }
3047
3048                 [Test]
3049                 [ExpectedException (typeof (ArgumentException))]
3050                 public void DrawImage_ImagePointArrayEmpty ()
3051                 {
3052                         using (Bitmap bmp = new Bitmap (40, 40)) {
3053                                 using (Graphics g = Graphics.FromImage (bmp)) {
3054                                         g.DrawImage (bmp, new Point[0]);
3055                                 }
3056                         }
3057                 }
3058
3059                 [Test]
3060                 public void DrawImage_ImagePointArray ()
3061                 {
3062                         using (Bitmap bmp = new Bitmap (40, 40)) {
3063                                 using (Graphics g = Graphics.FromImage (bmp)) {
3064                                         g.DrawImage (bmp, new Point[] { 
3065                                                 new Point (0, 0), new Point (1, 1), new Point (2, 2) });
3066                                 }
3067                         }
3068                 }
3069
3070                 [Test]
3071                 [ExpectedException (typeof (ArgumentNullException))]
3072                 public void DrawImage_NullIntInt ()
3073                 {
3074                         using (Bitmap bmp = new Bitmap (40, 40)) {
3075                                 using (Graphics g = Graphics.FromImage (bmp)) {
3076                                         g.DrawImage (null, Int32.MaxValue, Int32.MinValue);
3077                                 }
3078                         }
3079                 }
3080
3081                 [Test]
3082                 [ExpectedException (typeof (OverflowException))]
3083                 public void DrawImage_ImageIntInt_Overflow ()
3084                 {
3085                         using (Bitmap bmp = new Bitmap (40, 40)) {
3086                                 using (Graphics g = Graphics.FromImage (bmp)) {
3087                                         g.DrawImage (bmp, Int32.MaxValue, Int32.MinValue);
3088                                 }
3089                         }
3090                 }
3091
3092                 [Test]
3093                 public void DrawImage_ImageIntInt ()
3094                 {
3095                         using (Bitmap bmp = new Bitmap (40, 40)) {
3096                                 using (Graphics g = Graphics.FromImage (bmp)) {
3097                                         g.DrawImage (bmp, -40, -40);
3098                                 }
3099                         }
3100                 }
3101
3102                 [Test]
3103                 [ExpectedException (typeof (ArgumentNullException))]
3104                 public void DrawImage_NullFloat ()
3105                 {
3106                         using (Bitmap bmp = new Bitmap (40, 40)) {
3107                                 using (Graphics g = Graphics.FromImage (bmp)) {
3108                                         g.DrawImage (null, Single.MaxValue, Single.MinValue);
3109                                 }
3110                         }
3111                 }
3112
3113                 [Test]
3114                 [ExpectedException (typeof (OverflowException))]
3115                 public void DrawImage_ImageFloatFloat_Overflow ()
3116                 {
3117                         using (Bitmap bmp = new Bitmap (40, 40)) {
3118                                 using (Graphics g = Graphics.FromImage (bmp)) {
3119                                         g.DrawImage (bmp, Single.MaxValue, Single.MinValue);
3120                                 }
3121                         }
3122                 }
3123
3124                 [Test]
3125                 public void DrawImage_ImageFloatFloat ()
3126                 {
3127                         using (Bitmap bmp = new Bitmap (40, 40)) {
3128                                 using (Graphics g = Graphics.FromImage (bmp)) {
3129                                         g.DrawImage (bmp, -40.0f, -40.0f);
3130                                 }
3131                         }
3132                 }
3133
3134                 [Test]
3135                 [ExpectedException (typeof (ArgumentNullException))]
3136                 public void DrawImage_NullRectangleRectangleGraphicsUnit ()
3137                 {
3138                         using (Bitmap bmp = new Bitmap (40, 40)) {
3139                                 using (Graphics g = Graphics.FromImage (bmp)) {
3140                                         g.DrawImage (null, new Rectangle (), new Rectangle (), GraphicsUnit.Display);
3141                                 }
3142                         }
3143                 }
3144
3145                 private void DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit unit)
3146                 {
3147                         using (Bitmap bmp = new Bitmap (40, 40)) {
3148                                 using (Graphics g = Graphics.FromImage (bmp)) {
3149                                         Rectangle r = new Rectangle (0, 0, 40, 40);
3150                                         g.DrawImage (bmp, r, r, unit);
3151                                 }
3152                         }
3153                 }
3154
3155                 [Test]
3156                 [ExpectedException (typeof (ArgumentException))]
3157                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Display ()
3158                 {
3159                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Display);
3160                 }
3161
3162                 [Test]
3163                 [ExpectedException (typeof (NotImplementedException))]
3164                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Document ()
3165                 {
3166                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Document);
3167                 }
3168
3169                 [Test]
3170                 [ExpectedException (typeof (NotImplementedException))]
3171                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Inch ()
3172                 {
3173                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Inch);
3174                 }
3175
3176                 [Test]
3177                 [ExpectedException (typeof (NotImplementedException))]
3178                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Millimeter ()
3179                 {
3180                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Millimeter);
3181                 }
3182
3183                 [Test]
3184                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Pixel ()
3185                 {
3186                         // this unit works
3187                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Pixel);
3188                 }
3189
3190                 [Test]
3191                 [ExpectedException (typeof (NotImplementedException))]
3192                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Point ()
3193                 {
3194                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Point);
3195                 }
3196
3197                 [Test]
3198                 [ExpectedException (typeof (ArgumentException))]
3199                 public void DrawImage_ImageRectangleRectangleGraphicsUnit_World ()
3200                 {
3201                         DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.World);
3202                 }
3203
3204                 [Test]
3205                 [ExpectedException (typeof (ArgumentNullException))]
3206                 public void DrawImage_NullPointRectangleGraphicsUnit ()
3207                 {
3208                         Rectangle r = new Rectangle (1, 2, 3, 4);
3209                         Point[] pts = new Point[3] { new Point (1, 1), new Point (2, 2), new Point (3, 3) };
3210                         using (Bitmap bmp = new Bitmap (40, 40)) {
3211                                 using (Graphics g = Graphics.FromImage (bmp)) {
3212                                         g.DrawImage (null, pts, r, GraphicsUnit.Pixel);
3213                                 }
3214                         }
3215                 }
3216
3217                 private void DrawImage_ImagePointRectangleGraphicsUnit (Point[] pts)
3218                 {
3219                         Rectangle r = new Rectangle (1, 2, 3, 4);
3220                         using (Bitmap bmp = new Bitmap (40, 40)) {
3221                                 using (Graphics g = Graphics.FromImage (bmp)) {
3222                                         g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel);
3223                                 }
3224                         }
3225                 }
3226
3227                 [Test]
3228                 [ExpectedException (typeof (ArgumentNullException))]
3229                 public void DrawImage_ImageNullRectangleGraphicsUnit ()
3230                 {
3231                         DrawImage_ImagePointRectangleGraphicsUnit (null);
3232                 }
3233
3234                 [Test]
3235                 [ExpectedException (typeof (ArgumentException))]
3236                 public void DrawImage_ImagePoint0RectangleGraphicsUnit ()
3237                 {
3238                         DrawImage_ImagePointRectangleGraphicsUnit (new Point[0]);
3239                 }
3240
3241                 [Test]
3242                 [ExpectedException (typeof (ArgumentException))]
3243                 public void DrawImage_ImagePoint1RectangleGraphicsUnit ()
3244                 {
3245                         Point p = new Point (1, 1);
3246                         DrawImage_ImagePointRectangleGraphicsUnit (new Point[1] { p });
3247                 }
3248
3249                 [Test]
3250                 [ExpectedException (typeof (ArgumentException))]
3251                 public void DrawImage_ImagePoint2RectangleGraphicsUnit ()
3252                 {
3253                         Point p = new Point (1, 1);
3254                         DrawImage_ImagePointRectangleGraphicsUnit (new Point[2] { p, p });
3255                 }
3256
3257                 [Test]
3258                 public void DrawImage_ImagePoint3RectangleGraphicsUnit ()
3259                 {
3260                         Point p = new Point (1, 1);
3261                         DrawImage_ImagePointRectangleGraphicsUnit (new Point[3] { p, p, p });
3262                 }
3263
3264                 [Test]
3265                 [ExpectedException (typeof (NotImplementedException))]
3266                 public void DrawImage_ImagePoint4RectangleGraphicsUnit ()
3267                 {
3268                         Point p = new Point (1, 1);
3269                         DrawImage_ImagePointRectangleGraphicsUnit (new Point[4] { p, p, p, p });
3270                 }
3271
3272                 [Test]
3273                 [ExpectedException (typeof (ArgumentNullException))]
3274                 public void DrawImage_NullPointFRectangleGraphicsUnit ()
3275                 {
3276                         Rectangle r = new Rectangle (1, 2, 3, 4);
3277                         PointF[] pts = new PointF[3] { new PointF (1, 1), new PointF (2, 2), new PointF (3, 3) };
3278                         using (Bitmap bmp = new Bitmap (40, 40)) {
3279                                 using (Graphics g = Graphics.FromImage (bmp)) {
3280                                         g.DrawImage (null, pts, r, GraphicsUnit.Pixel);
3281                                 }
3282                         }
3283                 }
3284
3285                 private void DrawImage_ImagePointFRectangleGraphicsUnit (PointF[] pts)
3286                 {
3287                         Rectangle r = new Rectangle (1, 2, 3, 4);
3288                         using (Bitmap bmp = new Bitmap (40, 40)) {
3289                                 using (Graphics g = Graphics.FromImage (bmp)) {
3290                                         g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel);
3291                                 }
3292                         }
3293                 }
3294
3295                 [Test]
3296                 [ExpectedException (typeof (ArgumentNullException))]
3297                 public void DrawImage_ImageNullFRectangleGraphicsUnit ()
3298                 {
3299                         DrawImage_ImagePointFRectangleGraphicsUnit (null);
3300                 }
3301
3302                 [Test]
3303                 [ExpectedException (typeof (ArgumentException))]
3304                 public void DrawImage_ImagePointF0RectangleGraphicsUnit ()
3305                 {
3306                         DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[0]);
3307                 }
3308
3309                 [Test]
3310                 [ExpectedException (typeof (ArgumentException))]
3311                 public void DrawImage_ImagePointF1RectangleGraphicsUnit ()
3312                 {
3313                         PointF p = new PointF (1, 1);
3314                         DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[1] { p });
3315                 }
3316
3317                 [Test]
3318                 [ExpectedException (typeof (ArgumentException))]
3319                 public void DrawImage_ImagePointF2RectangleGraphicsUnit ()
3320                 {
3321                         PointF p = new PointF (1, 1);
3322                         DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[2] { p, p });
3323                 }
3324
3325                 [Test]
3326                 public void DrawImage_ImagePointF3RectangleGraphicsUnit ()
3327                 {
3328                         PointF p = new PointF (1, 1);
3329                         DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[3] { p, p, p });
3330                 }
3331
3332                 [Test]
3333                 [ExpectedException (typeof (NotImplementedException))]
3334                 public void DrawImage_ImagePointF4RectangleGraphicsUnit ()
3335                 {
3336                         PointF p = new PointF (1, 1);
3337                         DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[4] { p, p, p, p });
3338                 }
3339
3340                 [Test]
3341                 public void DrawImage_ImagePointRectangleGraphicsUnitNull ()
3342                 {
3343                         Point p = new Point (1, 1);
3344                         Point[] pts = new Point[3] { p, p, p };
3345                         Rectangle r = new Rectangle (1, 2, 3, 4);
3346                         using (Bitmap bmp = new Bitmap (40, 40)) {
3347                                 using (Graphics g = Graphics.FromImage (bmp)) {
3348                                         g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel, null);
3349                                 }
3350                         }
3351                 }
3352
3353                 [Test]
3354                 public void DrawImage_ImagePointRectangleGraphicsUnitAttributes ()
3355                 {
3356                         Point p = new Point (1, 1);
3357                         Point[] pts = new Point[3] { p, p, p };
3358                         Rectangle r = new Rectangle (1, 2, 3, 4);
3359                         using (Bitmap bmp = new Bitmap (40, 40)) {
3360                                 using (Graphics g = Graphics.FromImage (bmp)) {
3361                                         ImageAttributes ia = new ImageAttributes ();
3362                                         g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel, ia);
3363                                 }
3364                         }
3365                 }
3366
3367                 [Test]
3368                 [ExpectedException (typeof (ArgumentNullException))]
3369                 public void DrawImageUnscaled_NullPoint ()
3370                 {
3371                         using (Bitmap bmp = new Bitmap (40, 40)) {
3372                                 using (Graphics g = Graphics.FromImage (bmp)) {
3373                                         g.DrawImageUnscaled (null, new Point (0, 0));
3374                                 }
3375                         }
3376                 }
3377
3378                 [Test]
3379                 public void DrawImageUnscaled_ImagePoint ()
3380                 {
3381                         using (Bitmap bmp = new Bitmap (40, 40)) {
3382                                 using (Graphics g = Graphics.FromImage (bmp)) {
3383                                         g.DrawImageUnscaled (bmp, new Point (0, 0));
3384                                 }
3385                         }
3386                 }
3387
3388                 [Test]
3389                 [ExpectedException (typeof (ArgumentNullException))]
3390                 public void DrawImageUnscaled_NullRectangle ()
3391                 {
3392                         using (Bitmap bmp = new Bitmap (40, 40)) {
3393                                 using (Graphics g = Graphics.FromImage (bmp)) {
3394                                         g.DrawImageUnscaled (null, new Rectangle (0, 0, -1, -1));
3395                                 }
3396                         }
3397                 }
3398
3399                 [Test]
3400                 public void DrawImageUnscaled_ImageRectangle ()
3401                 {
3402                         using (Bitmap bmp = new Bitmap (40, 40)) {
3403                                 using (Graphics g = Graphics.FromImage (bmp)) {
3404                                         g.DrawImageUnscaled (bmp, new Rectangle (0, 0, -1, -1));
3405                                 }
3406                         }
3407                 }
3408
3409                 [Test]
3410                 [ExpectedException (typeof (ArgumentNullException))]
3411                 public void DrawImageUnscaled_NullIntInt ()
3412                 {
3413                         using (Bitmap bmp = new Bitmap (40, 40)) {
3414                                 using (Graphics g = Graphics.FromImage (bmp)) {
3415                                         g.DrawImageUnscaled (null, 0, 0);
3416                                 }
3417                         }
3418                 }
3419
3420                 [Test]
3421                 public void DrawImageUnscaled_ImageIntInt ()
3422                 {
3423                         using (Bitmap bmp = new Bitmap (40, 40)) {
3424                                 using (Graphics g = Graphics.FromImage (bmp)) {
3425                                         g.DrawImageUnscaled (bmp, 0, 0);
3426                                 }
3427                         }
3428                 }
3429
3430                 [Test]
3431                 [ExpectedException (typeof (ArgumentNullException))]
3432                 public void DrawImageUnscaled_NullIntIntIntInt ()
3433                 {
3434                         using (Bitmap bmp = new Bitmap (40, 40)) {
3435                                 using (Graphics g = Graphics.FromImage (bmp)) {
3436                                         g.DrawImageUnscaled (null, 0, 0, -1, -1);
3437                                 }
3438                         }
3439                 }
3440
3441                 [Test]
3442                 public void DrawImageUnscaled_ImageIntIntIntInt ()
3443                 {
3444                         using (Bitmap bmp = new Bitmap (40, 40)) {
3445                                 using (Graphics g = Graphics.FromImage (bmp)) {
3446                                         g.DrawImageUnscaled (bmp, 0, 0, -1, -1);
3447                                 }
3448                         }
3449                 }
3450 #if NET_2_0
3451                 [Test]
3452                 [ExpectedException (typeof (ArgumentNullException))]
3453                 public void DrawImageUnscaledAndClipped_Null ()
3454                 {
3455                         using (Bitmap bmp = new Bitmap (40, 40)) {
3456                                 using (Graphics g = Graphics.FromImage (bmp)) {
3457                                         g.DrawImageUnscaledAndClipped (null, new Rectangle (0, 0, 0, 0));
3458                                 }
3459                         }
3460                 }
3461
3462                 [Test]
3463                 public void DrawImageUnscaledAndClipped ()
3464                 {
3465                         using (Bitmap bmp = new Bitmap (40, 40)) {
3466                                 using (Graphics g = Graphics.FromImage (bmp)) {
3467                                         // Rectangle is empty when X, Y, Width and Height == 0 
3468                                         // (yep X and Y too, RectangleF only checks for Width and Height)
3469                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 0, 0));
3470                                         // so this one is half-empty ;-)
3471                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (20, 40, 0, 0));
3472                                         // negative width or height isn't empty (for Rectangle)
3473                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (10, 20, -1, 0));
3474                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (20, 10, 0, -1));
3475                                         // smaller
3476                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 10, 20));
3477                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 40, 10));
3478                                         g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 80, 20));
3479                                 }
3480                         }
3481                 }
3482 #endif
3483
3484                 [Test]
3485                 [ExpectedException (typeof (ArgumentNullException))]
3486                 public void DrawPath_Pen_Null ()
3487                 {
3488                         using (Bitmap bmp = new Bitmap (20, 20)) {
3489                                 using (Graphics g = Graphics.FromImage (bmp)) {
3490                                         using (GraphicsPath path = new GraphicsPath ()) {
3491                                                 g.DrawPath (null, path);
3492                                         }
3493                                 }
3494                         }
3495                 }
3496
3497                 [Test]
3498                 [ExpectedException (typeof (ArgumentNullException))]
3499                 public void DrawPath_Path_Null ()
3500                 {
3501                         using (Bitmap bmp = new Bitmap (20, 20)) {
3502                                 using (Graphics g = Graphics.FromImage (bmp)) {
3503                                         g.DrawPath (Pens.Black, null);
3504                                 }
3505                         }
3506                 }
3507
3508                 [Test]
3509                 public void DrawPath_82202 ()
3510                 {
3511                         // based on test case from bug #82202
3512                         using (Bitmap bmp = new Bitmap (20, 20)) {
3513                                 using (Graphics g = Graphics.FromImage (bmp)) {
3514                                         using (GraphicsPath path = new GraphicsPath ()) {
3515                                                 int d = 5;
3516                                                 Rectangle baserect = new Rectangle (0, 0, 19, 19);
3517                                                 Rectangle arcrect = new Rectangle (baserect.Location, new Size (d, d));
3518
3519                                                 path.AddArc (arcrect, 180, 90);
3520                                                 arcrect.X = baserect.Right - d;
3521                                                 path.AddArc (arcrect, 270, 90);
3522                                                 arcrect.Y = baserect.Bottom - d;
3523                                                 path.AddArc (arcrect, 0, 90);
3524                                                 arcrect.X = baserect.Left;
3525                                                 path.AddArc (arcrect, 90, 90);
3526                                                 path.CloseFigure ();
3527                                                 g.Clear (Color.White);
3528                                                 g.DrawPath (Pens.SteelBlue, path);
3529
3530                                                 Assert.AreEqual (-12156236, bmp.GetPixel (0, 9).ToArgb (), "0,9");
3531                                                 Assert.AreEqual (-1, bmp.GetPixel (1, 9).ToArgb (), "1,9");
3532                                         }
3533                                 }
3534                         }
3535                 }
3536
3537                 [Test]
3538                 [ExpectedException (typeof (ArgumentNullException))]
3539                 public void FillPath_Brush_Null ()
3540                 {
3541                         using (Bitmap bmp = new Bitmap (20, 20)) {
3542                                 using (Graphics g = Graphics.FromImage (bmp)) {
3543                                         using (GraphicsPath path = new GraphicsPath ()) {
3544                                                 g.FillPath (null, path);
3545                                         }
3546                                 }
3547                         }
3548                 }
3549
3550                 [Test]
3551                 [ExpectedException (typeof (ArgumentNullException))]
3552                 public void FillPath_Path_Null ()
3553                 {
3554                         using (Bitmap bmp = new Bitmap (20, 20)) {
3555                                 using (Graphics g = Graphics.FromImage (bmp)) {
3556                                         g.FillPath (Brushes.Black, null);
3557                                 }
3558                         }
3559                 }
3560
3561                 [Test]
3562                 public void FillPath_82202 ()
3563                 {
3564                         // based on test case from bug #82202
3565                         using (Bitmap bmp = new Bitmap (20, 20)) {
3566                                 using (Graphics g = Graphics.FromImage (bmp)) {
3567                                         using (GraphicsPath path = new GraphicsPath ()) {
3568                                                 int d = 5;
3569                                                 Rectangle baserect = new Rectangle (0, 0, 19, 19);
3570                                                 Rectangle arcrect = new Rectangle (baserect.Location, new Size (d, d));
3571
3572                                                 path.AddArc (arcrect, 180, 90);
3573                                                 arcrect.X = baserect.Right - d;
3574                                                 path.AddArc (arcrect, 270, 90);
3575                                                 arcrect.Y = baserect.Bottom - d;
3576                                                 path.AddArc (arcrect, 0, 90);
3577                                                 arcrect.X = baserect.Left;
3578                                                 path.AddArc (arcrect, 90, 90);
3579                                                 path.CloseFigure ();
3580                                                 g.Clear (Color.White);
3581                                                 g.FillPath (Brushes.SteelBlue, path);
3582
3583                                                 Assert.AreEqual (-12156236, bmp.GetPixel (0, 9).ToArgb (), "0,9");
3584                                                 Assert.AreEqual (-12156236, bmp.GetPixel (1, 9).ToArgb (), "1,9");
3585                                         }
3586                                 }
3587                         }
3588                 }
3589
3590                 [Test]
3591                 public void TransformPoints_349800 ()
3592                 {
3593                         using (Bitmap bmp = new Bitmap (10, 10)) {
3594                                 using (Graphics g = Graphics.FromImage (bmp)) {
3595                                         Point [] pts = new Point [5];
3596                                         PointF [] ptf = new PointF [5];
3597                                         for (int i = 0; i < 5; i++) {
3598                                                 pts [i] = new Point (i, i);
3599                                                 ptf [i] = new PointF (i, i);
3600                                         }
3601
3602                                         g.TransformPoints (CoordinateSpace.Page, CoordinateSpace.Device, pts);
3603                                         g.TransformPoints (CoordinateSpace.Page, CoordinateSpace.Device, ptf);
3604
3605                                         for (int i = 0; i < 5; i++) {
3606                                                 Assert.AreEqual (i, pts [i].X, "Point.X " + i.ToString ());
3607                                                 Assert.AreEqual (i, pts [i].Y, "Point.Y " + i.ToString ());
3608                                                 Assert.AreEqual (i, ptf [i].X, "PointF.X " + i.ToString ());
3609                                                 Assert.AreEqual (i, ptf [i].Y, "PointF.Y " + i.ToString ());
3610                                         }
3611                                 }
3612                         }
3613                 }
3614         }
3615
3616         [TestFixture]
3617         public class GraphicsFullTrustTest {
3618
3619                 // note: this test would fail, on ReleaseHdc, without fulltrust
3620                 // i.e. it's a demand and not a linkdemand
3621                 [Test]
3622                 public void GetReleaseHdc ()
3623                 {
3624                         using (Bitmap b = new Bitmap (100, 100)) {
3625                                 using (Graphics g = Graphics.FromImage (b)) {
3626                                         IntPtr hdc1 = g.GetHdc ();
3627                                         g.ReleaseHdc (hdc1);
3628                                         IntPtr hdc2 = g.GetHdc ();
3629                                         g.ReleaseHdc (hdc2);
3630                                         Assert.AreEqual (hdc1, hdc2, "hdc");
3631                                 }
3632                         }
3633                 }
3634
3635         }
3636 }