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