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