2 // Graphics class testing unit
5 // Jordi Mas, jordi@ximian.com
6 // Sebastien Pouliot <sebastien@ximian.com>
8 // Copyright (C) 2005-2008 Novell, Inc (http://www.novell.com)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
30 using NUnit.Framework;
33 using System.Drawing.Drawing2D;
34 using System.Drawing.Imaging;
35 using System.Drawing.Text;
37 using System.Reflection;
38 using System.Security.Permissions;
40 namespace MonoTests.System.Drawing {
43 public class GraphicsTest {
45 private RectangleF[] rects;
49 public void FixtureSetUp ()
52 font = new Font ("Arial", 12);
59 public void FixtureTearDown ()
66 private bool IsEmptyBitmap (Bitmap bitmap, out int x, out int y)
69 int empty = Color.Empty.ToArgb ();
71 for (y = 0; y < bitmap.Height; y++) {
72 for (x = 0; x < bitmap.Width; x++) {
73 if (bitmap.GetPixel (x, y).ToArgb () != empty) {
82 for (y = 0; y < bitmap.Height; y++) {
83 for (x = 0; x < bitmap.Width; x++) {
84 if (bitmap.GetPixel (x, y).ToArgb () != empty)
94 private void CheckForEmptyBitmap (Bitmap bitmap)
97 if (!IsEmptyBitmap (bitmap, out x, out y))
98 Assert.Fail (String.Format ("Position {0},{1}", x, y));
101 private void CheckForNonEmptyBitmap (Bitmap bitmap)
104 if (IsEmptyBitmap (bitmap, out x, out y))
105 Assert.Fail ("Bitmap was empty");
108 private void AssertEquals (string msg, object expected, object actual)
110 Assert.AreEqual (expected, actual, msg);
113 private void AssertEquals (string msg, double expected, double actual, double delta)
115 Assert.AreEqual (expected, actual, delta, msg);
119 public void DefaultProperties ()
121 Bitmap bmp = new Bitmap (200, 200);
122 Graphics g = Graphics.FromImage (bmp);
123 Region r = new Region ();
125 Assert.AreEqual (r.GetBounds (g), g.ClipBounds, "DefaultProperties1");
126 Assert.AreEqual (CompositingMode.SourceOver, g.CompositingMode, "DefaultProperties2");
127 Assert.AreEqual (CompositingQuality.Default, g.CompositingQuality, "DefaultProperties3");
128 Assert.AreEqual (InterpolationMode.Bilinear, g.InterpolationMode, "DefaultProperties4");
129 Assert.AreEqual (1, g.PageScale, "DefaultProperties5");
130 Assert.AreEqual (GraphicsUnit.Display, g.PageUnit, "DefaultProperties6");
131 Assert.AreEqual (PixelOffsetMode.Default, g.PixelOffsetMode, "DefaultProperties7");
132 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "DefaultProperties8");
133 Assert.AreEqual (SmoothingMode.None, g.SmoothingMode, "DefaultProperties9");
134 Assert.AreEqual (TextRenderingHint.SystemDefault, g.TextRenderingHint, "DefaultProperties10");
140 public void SetGetProperties ()
142 Bitmap bmp = new Bitmap (200, 200);
143 Graphics g = Graphics.FromImage (bmp);
145 g.CompositingMode = CompositingMode.SourceCopy;
146 g.CompositingQuality = CompositingQuality.GammaCorrected;
147 g.InterpolationMode = InterpolationMode.HighQualityBilinear;
149 g.PageUnit = GraphicsUnit.Inch;
150 g.PixelOffsetMode = PixelOffsetMode.Half;
151 g.RenderingOrigin = new Point (10, 20);
152 g.SmoothingMode = SmoothingMode.AntiAlias;
153 g.TextRenderingHint = TextRenderingHint.SystemDefault;
155 //Clipping set/get tested in clipping functions
156 Assert.AreEqual (CompositingMode.SourceCopy, g.CompositingMode, "SetGetProperties2");
157 Assert.AreEqual (CompositingQuality.GammaCorrected, g.CompositingQuality, "SetGetProperties3");
158 Assert.AreEqual (InterpolationMode.HighQualityBilinear, g.InterpolationMode, "SetGetProperties4");
159 Assert.AreEqual (2, g.PageScale, "SetGetProperties5");
160 Assert.AreEqual (GraphicsUnit.Inch, g.PageUnit, "SetGetProperties6");
161 Assert.AreEqual (PixelOffsetMode.Half, g.PixelOffsetMode, "SetGetProperties7");
162 Assert.AreEqual (new Point (10, 20), g.RenderingOrigin, "SetGetProperties8");
163 Assert.AreEqual (SmoothingMode.AntiAlias, g.SmoothingMode, "SetGetProperties9");
164 Assert.AreEqual (TextRenderingHint.SystemDefault, g.TextRenderingHint, "SetGetProperties10");
172 Bitmap bmp = new Bitmap (200, 200);
173 Graphics g = Graphics.FromImage (bmp);
174 g.Clip = new Region (new Rectangle (50, 40, 210, 220));
175 rects = g.Clip.GetRegionScans (new Matrix ());
177 Assert.AreEqual (1, rects.Length, "Clip1");
178 Assert.AreEqual (50, rects[0].X, "Clip2");
179 Assert.AreEqual (40, rects[0].Y, "Clip3");
180 Assert.AreEqual (210, rects[0].Width, "Clip4");
181 Assert.AreEqual (220, rects[0].Height, "Clip5");
185 public void Clip_NotAReference ()
187 Bitmap bmp = new Bitmap (200, 200);
188 Graphics g = Graphics.FromImage (bmp);
189 Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite");
191 Assert.IsFalse (g.Clip.IsEmpty (g), "!IsEmpty");
192 Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite-2");
196 public void ExcludeClip ()
198 Bitmap bmp = new Bitmap (200, 200);
199 Graphics g = Graphics.FromImage (bmp);
201 g.Clip = new Region (new RectangleF (10, 10, 100, 100));
202 g.ExcludeClip (new Rectangle (40, 60, 100, 20));
203 rects = g.Clip.GetRegionScans (new Matrix ());
205 Assert.AreEqual (3, rects.Length, "ExcludeClip1");
207 Assert.AreEqual (10, rects[0].X, "ExcludeClip2");
208 Assert.AreEqual (10, rects[0].Y, "ExcludeClip3");
209 Assert.AreEqual (100, rects[0].Width, "ExcludeClip4");
210 Assert.AreEqual (50, rects[0].Height, "ExcludeClip5");
212 Assert.AreEqual (10, rects[1].X, "ExcludeClip6");
213 Assert.AreEqual (60, rects[1].Y, "ExcludeClip7");
214 Assert.AreEqual (30, rects[1].Width, "ExcludeClip8");
215 Assert.AreEqual (20, rects[1].Height, "ExcludeClip9");
217 Assert.AreEqual (10, rects[2].X, "ExcludeClip10");
218 Assert.AreEqual (80, rects[2].Y, "ExcludeClip11");
219 Assert.AreEqual (100, rects[2].Width, "ExcludeClip12");
220 Assert.AreEqual (30, rects[2].Height, "ExcludeClip13");
224 public void IntersectClip ()
226 Bitmap bmp = new Bitmap (200, 200);
227 Graphics g = Graphics.FromImage (bmp);
229 g.Clip = new Region (new RectangleF (260, 30, 60, 80));
230 g.IntersectClip (new Rectangle (290, 40, 60, 80));
231 rects = g.Clip.GetRegionScans (new Matrix ());
233 Assert.AreEqual (1, rects.Length, "IntersectClip");
235 Assert.AreEqual (290, rects[0].X, "IntersectClip");
236 Assert.AreEqual (40, rects[0].Y, "IntersectClip");
237 Assert.AreEqual (30, rects[0].Width, "IntersectClip");
238 Assert.AreEqual (70, rects[0].Height, "IntersectClip");
242 public void ResetClip ()
244 Bitmap bmp = new Bitmap (200, 200);
245 Graphics g = Graphics.FromImage (bmp);
247 g.Clip = new Region (new RectangleF (260, 30, 60, 80));
248 g.IntersectClip (new Rectangle (290, 40, 60, 80));
250 rects = g.Clip.GetRegionScans (new Matrix ());
252 Assert.AreEqual (1, rects.Length, "ResetClip");
254 Assert.AreEqual (-4194304, rects[0].X, "ResetClip");
255 Assert.AreEqual (-4194304, rects[0].Y, "ResetClip");
256 Assert.AreEqual (8388608, rects[0].Width, "ResetClip");
257 Assert.AreEqual (8388608, rects[0].Height, "ResetClip");
261 public void SetClip ()
264 Bitmap bmp = new Bitmap (200, 200);
265 Graphics g = Graphics.FromImage (bmp);
268 g.SetClip (new Region (new Rectangle (50, 40, 210, 220)), CombineMode.Replace);
269 rects = g.Clip.GetRegionScans (new Matrix ());
270 Assert.AreEqual (1, rects.Length, "SetClip1");
271 Assert.AreEqual (50, rects[0].X, "SetClip2");
272 Assert.AreEqual (40, rects[0].Y, "SetClip3");
273 Assert.AreEqual (210, rects[0].Width, "SetClip4");
274 Assert.AreEqual (220, rects[0].Height, "SetClip5");
277 g = Graphics.FromImage (bmp);
278 g.SetClip (new RectangleF (50, 40, 210, 220));
279 rects = g.Clip.GetRegionScans (new Matrix ());
280 Assert.AreEqual (1, rects.Length, "SetClip6");
281 Assert.AreEqual (50, rects[0].X, "SetClip7");
282 Assert.AreEqual (40, rects[0].Y, "SetClip8");
283 Assert.AreEqual (210, rects[0].Width, "SetClip9");
284 Assert.AreEqual (220, rects[0].Height, "SetClip10");
287 g = Graphics.FromImage (bmp);
288 g.SetClip (new Rectangle (50, 40, 210, 220));
289 rects = g.Clip.GetRegionScans (new Matrix ());
290 Assert.AreEqual (1, rects.Length, "SetClip10");
291 Assert.AreEqual (50, rects[0].X, "SetClip11");
292 Assert.AreEqual (40, rects[0].Y, "SetClip12");
293 Assert.AreEqual (210, rects[0].Width, "SetClip13");
294 Assert.AreEqual (220, rects[0].Height, "SetClip14");
298 public void SetSaveReset ()
300 Bitmap bmp = new Bitmap (200, 200);
301 Graphics g = Graphics.FromImage (bmp);
302 GraphicsState state_default, state_modified;
304 state_default = g.Save (); // Default
306 g.CompositingMode = CompositingMode.SourceCopy;
307 g.CompositingQuality = CompositingQuality.GammaCorrected;
308 g.InterpolationMode = InterpolationMode.HighQualityBilinear;
310 g.PageUnit = GraphicsUnit.Inch;
311 g.PixelOffsetMode = PixelOffsetMode.Half;
312 g.Clip = new Region (new Rectangle (0, 0, 100, 100));
313 g.RenderingOrigin = new Point (10, 20);
314 g.SmoothingMode = SmoothingMode.AntiAlias;
315 g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
318 state_modified = g.Save (); // Modified
320 g.CompositingMode = CompositingMode.SourceOver;
321 g.CompositingQuality = CompositingQuality.Default;
322 g.InterpolationMode = InterpolationMode.Bilinear;
324 g.PageUnit = GraphicsUnit.Display;
325 g.PixelOffsetMode = PixelOffsetMode.Default;
326 g.Clip = new Region (new Rectangle (1, 2, 20, 25));
327 g.RenderingOrigin = new Point (5, 6);
328 g.SmoothingMode = SmoothingMode.None;
329 g.TextRenderingHint = TextRenderingHint.SystemDefault;
331 g.Restore (state_modified);
333 Assert.AreEqual (CompositingMode.SourceCopy, g.CompositingMode, "SetSaveReset1");
334 Assert.AreEqual (CompositingQuality.GammaCorrected, g.CompositingQuality, "SetSaveReset2");
335 Assert.AreEqual (InterpolationMode.HighQualityBilinear, g.InterpolationMode, "SetSaveReset3");
336 Assert.AreEqual (2, g.PageScale, "SetSaveReset4");
337 Assert.AreEqual (GraphicsUnit.Inch, g.PageUnit, "SetSaveReset5");
338 Assert.AreEqual (PixelOffsetMode.Half, g.PixelOffsetMode, "SetSaveReset6");
339 Assert.AreEqual (new Point (10, 20), g.RenderingOrigin, "SetSaveReset7");
340 Assert.AreEqual (SmoothingMode.AntiAlias, g.SmoothingMode, "SetSaveReset8");
341 Assert.AreEqual (TextRenderingHint.ClearTypeGridFit, g.TextRenderingHint, "SetSaveReset9");
342 Assert.AreEqual (0, (int) g.ClipBounds.X, "SetSaveReset10");
343 Assert.AreEqual (0, (int) g.ClipBounds.Y, "SetSaveReset10");
345 g.Restore (state_default);
347 Assert.AreEqual (CompositingMode.SourceOver, g.CompositingMode, "SetSaveReset11");
348 Assert.AreEqual (CompositingQuality.Default, g.CompositingQuality, "SetSaveReset12");
349 Assert.AreEqual (InterpolationMode.Bilinear, g.InterpolationMode, "SetSaveReset13");
350 Assert.AreEqual (1, g.PageScale, "SetSaveReset14");
351 Assert.AreEqual (GraphicsUnit.Display, g.PageUnit, "SetSaveReset15");
352 Assert.AreEqual (PixelOffsetMode.Default, g.PixelOffsetMode, "SetSaveReset16");
353 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "SetSaveReset17");
354 Assert.AreEqual (SmoothingMode.None, g.SmoothingMode, "SetSaveReset18");
355 Assert.AreEqual (TextRenderingHint.SystemDefault, g.TextRenderingHint, "SetSaveReset19");
357 Region r = new Region ();
358 Assert.AreEqual (r.GetBounds (g), g.ClipBounds, "SetSaveReset20");
364 [Category ("NotWorking")] // looks like MS PNG codec promote indexed format to 32bpp ARGB
365 public void LoadIndexed_PngStream ()
367 // Tests that we can load an indexed file
368 using (Stream s = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("indexed.png")) {
369 using (Image img = Image.FromStream (s)) {
370 // however it's no more indexed once loaded
371 Assert.AreEqual (PixelFormat.Format32bppArgb, img.PixelFormat, "PixelFormat");
372 using (Graphics g = Graphics.FromImage (img)) {
373 Assert.AreEqual (img.Height, g.VisibleClipBounds.Height, "Height");
374 Assert.AreEqual (img.Width, g.VisibleClipBounds.Width, "Width");
381 public void LoadIndexed_BmpFile ()
383 // Tests that we can load an indexed file, but...
384 string sInFile = TestBitmap.getInFile ("bitmaps/almogaver1bit.bmp");
385 // note: file is misnamed (it's a 4bpp bitmap)
386 using (Image img = Image.FromFile (sInFile)) {
387 Assert.AreEqual (PixelFormat.Format4bppIndexed, img.PixelFormat, "PixelFormat");
388 Assert.Throws<Exception> (() => Graphics.FromImage (img));
393 public void FromImage ()
395 Assert.Throws<ArgumentNullException> (() => Graphics.FromImage (null));
398 private Graphics Get (int w, int h)
400 Bitmap bitmap = new Bitmap (w, h);
401 Graphics g = Graphics.FromImage (bitmap);
402 g.Clip = new Region (new Rectangle (0, 0, w, h));
406 private void Compare (string msg, RectangleF b1, RectangleF b2)
408 AssertEquals (msg + ".compare.X", b1.X, b2.X);
409 AssertEquals (msg + ".compare.Y", b1.Y, b2.Y);
410 AssertEquals (msg + ".compare.Width", b1.Width, b2.Width);
411 AssertEquals (msg + ".compare.Height", b1.Height, b2.Height);
415 public void Clip_GetBounds ()
417 Graphics g = Get (16, 16);
418 RectangleF bounds = g.Clip.GetBounds (g);
419 Assert.AreEqual (0, bounds.X, "X");
420 Assert.AreEqual (0, bounds.Y, "Y");
421 Assert.AreEqual (16, bounds.Width, "Width");
422 Assert.AreEqual (16, bounds.Height, "Height");
423 Assert.IsTrue (g.Transform.IsIdentity, "Identity");
428 public void Clip_TranslateTransform ()
430 Graphics g = Get (16, 16);
431 g.TranslateTransform (12.22f, 10.10f);
432 RectangleF bounds = g.Clip.GetBounds (g);
433 Compare ("translate", bounds, g.ClipBounds);
434 Assert.AreEqual (-12.2200003f, bounds.X, "translate.X");
435 Assert.AreEqual (-10.1000004f, bounds.Y, "translate.Y");
436 Assert.AreEqual (16, bounds.Width, "translate.Width");
437 Assert.AreEqual (16, bounds.Height, "translate.Height");
438 float[] elements = g.Transform.Elements;
439 Assert.AreEqual (1, elements[0], "translate.0");
440 Assert.AreEqual (0, elements[1], "translate.1");
441 Assert.AreEqual (0, elements[2], "translate.2");
442 Assert.AreEqual (1, elements[3], "translate.3");
443 Assert.AreEqual (12.2200003f, elements[4], "translate.4");
444 Assert.AreEqual (10.1000004f, elements[5], "translate.5");
447 bounds = g.Clip.GetBounds (g);
448 Compare ("reset", bounds, g.ClipBounds);
449 Assert.AreEqual (0, bounds.X, "reset.X");
450 Assert.AreEqual (0, bounds.Y, "reset.Y");
451 Assert.AreEqual (16, bounds.Width, "reset.Width");
452 Assert.AreEqual (16, bounds.Height, "reset.Height");
453 Assert.IsTrue (g.Transform.IsIdentity, "Identity");
458 public void Transform_NonInvertibleMatrix ()
460 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
461 Assert.IsFalse (matrix.IsInvertible, "IsInvertible");
462 Graphics g = Get (16, 16);
463 Assert.Throws<ArgumentException> (() => g.Transform = matrix);
468 public void Multiply_NonInvertibleMatrix ()
470 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
471 Assert.IsFalse (matrix.IsInvertible, "IsInvertible");
472 Graphics g = Get (16, 16);
473 Assert.Throws<ArgumentException> (() => g.MultiplyTransform (matrix));
477 public void Multiply_Null ()
479 Graphics g = Get (16, 16);
480 Assert.Throws<ArgumentNullException> (() => g.MultiplyTransform (null));
483 private void CheckBounds (string msg, RectangleF bounds, float x, float y, float w, float h)
485 AssertEquals (msg + ".X", x, bounds.X, 0.1);
486 AssertEquals (msg + ".Y", y, bounds.Y, 0.1);
487 AssertEquals (msg + ".Width", w, bounds.Width, 0.1);
488 AssertEquals (msg + ".Height", h, bounds.Height, 0.1);
492 public void ClipBounds ()
494 Graphics g = Get (16, 16);
495 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
496 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
498 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
499 CheckBounds ("clip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
500 CheckBounds ("clip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
504 public void ClipBounds_Rotate ()
506 Graphics g = Get (16, 16);
507 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
508 g.RotateTransform (90);
509 CheckBounds ("rotate.ClipBounds", g.ClipBounds, 0, -8, 8, 8);
510 CheckBounds ("rotate.Clip.GetBounds", g.Clip.GetBounds (g), 0, -8, 8, 8);
512 g.Transform = new Matrix ();
513 CheckBounds ("identity.ClipBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
514 CheckBounds ("identity.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
518 public void ClipBounds_Scale ()
520 RectangleF clip = new Rectangle (0, 0, 8, 8);
521 Graphics g = Get (16, 16);
522 g.Clip = new Region (clip);
523 g.ScaleTransform (0.25f, 0.5f);
524 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 32, 16);
525 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 32, 16);
528 CheckBounds ("setclip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
529 CheckBounds ("setclip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
533 public void ClipBounds_Translate ()
535 Graphics g = Get (16, 16);
536 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
537 Region clone = g.Clip.Clone ();
538 g.TranslateTransform (8, 8);
539 CheckBounds ("translate.ClipBounds", g.ClipBounds, -8, -8, 8, 8);
540 CheckBounds ("translate.Clip.GetBounds", g.Clip.GetBounds (g), -8, -8, 8, 8);
542 g.SetClip (clone, CombineMode.Replace);
543 CheckBounds ("setclip.ClipBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
544 CheckBounds ("setclip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
548 public void ClipBounds_Transform_Translation ()
550 Graphics g = Get (16, 16);
551 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
552 g.Transform = new Matrix (1, 0, 0, 1, 8, 8);
553 CheckBounds ("transform.ClipBounds", g.ClipBounds, -8, -8, 8, 8);
554 CheckBounds ("transform.Clip.GetBounds", g.Clip.GetBounds (g), -8, -8, 8, 8);
557 CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
558 CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
562 public void ClipBounds_Transform_Scale ()
564 Graphics g = Get (16, 16);
565 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
566 g.Transform = new Matrix (0.5f, 0, 0, 0.25f, 0, 0);
567 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 16, 32);
568 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 32);
571 // see next test for ClipBounds
572 CheckBounds ("resetclip.Clip.GetBounds", g.Clip.GetBounds (g), -4194304, -4194304, 8388608, 8388608);
573 Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite");
577 [Category ("NotWorking")]
578 public void ClipBounds_Transform_Scale_Strange ()
580 Graphics g = Get (16, 16);
581 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
582 g.Transform = new Matrix (0.5f, 0, 0, 0.25f, 0, 0);
583 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 16, 32);
584 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 32);
587 // note: strange case where g.ClipBounds and g.Clip.GetBounds are different
588 CheckBounds ("resetclip.ClipBounds", g.ClipBounds, -8388608, -16777216, 16777216, 33554432);
592 public void ClipBounds_Multiply ()
594 Graphics g = Get (16, 16);
595 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
596 g.Transform = new Matrix (1, 0, 0, 1, 8, 8);
597 g.MultiplyTransform (g.Transform);
598 CheckBounds ("multiply.ClipBounds", g.ClipBounds, -16, -16, 8, 8);
599 CheckBounds ("multiply.Clip.GetBounds", g.Clip.GetBounds (g), -16, -16, 8, 8);
602 CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
603 CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
607 public void ClipBounds_Cumulative_Effects ()
609 Graphics g = Get (16, 16);
610 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
611 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
613 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
614 CheckBounds ("clip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
615 CheckBounds ("clip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
617 g.RotateTransform (90);
618 CheckBounds ("rotate.ClipBounds", g.ClipBounds, 0, -8, 8, 8);
619 CheckBounds ("rotate.Clip.GetBounds", g.Clip.GetBounds (g), 0, -8, 8, 8);
621 g.ScaleTransform (0.25f, 0.5f);
622 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, -16, 32, 16);
623 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, -16, 32, 16);
625 g.TranslateTransform (8, 8);
626 CheckBounds ("translate.ClipBounds", g.ClipBounds, -8, -24, 32, 16);
627 CheckBounds ("translate.Clip.GetBounds", g.Clip.GetBounds (g), -8, -24, 32, 16);
629 g.MultiplyTransform (g.Transform);
630 CheckBounds ("multiply.ClipBounds", g.ClipBounds, -104, -56, 64, 64);
631 CheckBounds ("multiply.Clip.GetBounds", g.Clip.GetBounds (g), -104, -56, 64, 64);
634 CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
635 CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
639 public void Clip_TranslateTransform_BoundsChange ()
641 Graphics g = Get (16, 16);
642 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
643 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
644 g.TranslateTransform (-16, -16);
645 CheckBounds ("translated.ClipBounds", g.ClipBounds, 16, 16, 16, 16);
646 CheckBounds ("translated.Clip.GetBounds", g.Clip.GetBounds (g), 16, 16, 16, 16);
648 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
649 // ClipBounds isn't affected by a previous translation
650 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
651 // Clip.GetBounds isn't affected by a previous translation
652 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
655 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -16, -16, 8, 8);
656 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -16, -16, 8, 8);
660 public void Clip_RotateTransform_BoundsChange ()
662 Graphics g = Get (16, 16);
663 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
664 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
665 // we select a "simple" angle because the region will be converted into
666 // a bitmap (well for libgdiplus) and we would lose precision after that
667 g.RotateTransform (90);
668 CheckBounds ("rotated.ClipBounds", g.ClipBounds, 0, -16, 16, 16);
669 CheckBounds ("rotated.Clip.GetBounds", g.Clip.GetBounds (g), 0, -16, 16, 16);
670 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
671 // ClipBounds isn't affected by a previous rotation (90)
672 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
673 // Clip.GetBounds isn't affected by a previous rotation
674 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
677 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -8, 0, 8, 8);
678 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -8, 0, 8, 8);
681 private void CheckBoundsInt (string msg, RectangleF bounds, int x, int y, int w, int h)
683 // currently bounds are rounded at 8 pixels (FIXME - we can go down to 1 pixel)
684 AssertEquals (msg + ".X", x, bounds.X, 4f);
685 AssertEquals (msg + ".Y", y, bounds.Y, 4f);
686 AssertEquals (msg + ".Width", w, bounds.Width, 4f);
687 AssertEquals (msg + ".Height", h, bounds.Height, 4f);
691 [Category ("NotWorking")]
692 public void Clip_RotateTransform_BoundsChange_45 ()
694 Graphics g = Get (16, 16);
695 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
696 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
697 g.RotateTransform (45);
698 // we can't use the "normal" CheckBound here because of libgdiplus crude rounding
699 CheckBoundsInt ("rotated.ClipBounds", g.ClipBounds, 0, -11, 24, 24);
700 CheckBoundsInt ("rotated.Clip.GetBounds", g.Clip.GetBounds (g), 0, -11, 24, 24);
701 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
702 // ClipBounds IS affected by a previous rotation (45)
703 CheckBoundsInt ("rectangle.ClipBounds", g.ClipBounds, -3, -4, 16, 16);
704 // Clip.GetBounds isn't affected by a previous rotation
705 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
708 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -5, 1, 11, 11);
709 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -5.6f, 0, 11.3f, 11.3f);
713 public void Clip_ScaleTransform_NoBoundsChange ()
715 Graphics g = Get (16, 16);
716 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
717 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
718 g.ScaleTransform (2, 0.5f);
719 CheckBounds ("scaled.ClipBounds", g.ClipBounds, 0, 0, 8, 32);
720 CheckBounds ("scaled.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 32);
721 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
722 // ClipBounds isn't affected by a previous scaling
723 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
724 // Clip.GetBounds isn't affected by a previous scaling
725 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
728 CheckBounds ("reseted.ClipBounds", g.ClipBounds, 0, 0, 16, 4);
729 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 4);
733 [Category ("NotWorking")]
734 public void Clip_MultiplyTransform_NoBoundsChange ()
736 Graphics g = Get (16, 16);
737 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
738 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
739 g.MultiplyTransform (new Matrix (2.5f, 0.5f, -2.5f, 0.5f, 4, -4));
740 CheckBounds ("multiplied.ClipBounds", g.ClipBounds, 3.2f, 1.6f, 19.2f, 19.2f);
741 CheckBounds ("multiplied.Clip.GetBounds", g.Clip.GetBounds (g), 3.2f, 1.6f, 19.2f, 19.2f);
742 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
743 // ClipBounds IS affected by the previous multiplication
744 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, -3, -3, 15, 15);
745 // Clip.GetBounds isn't affected by the previous multiplication
746 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
749 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -16, -3, 40, 7);
750 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -16, -4, 40, 8);
754 public void ScaleTransform_X0 ()
756 Graphics g = Get (16, 16);
757 Assert.Throws<ArgumentException> (() => g.ScaleTransform (0, 1));
761 public void ScaleTransform_Y0 ()
763 Graphics g = Get (16, 16);
764 Assert.Throws<ArgumentException> (() => g.ScaleTransform (1, 0));
768 public void TranslateTransform_Order ()
770 Graphics g = Get (16, 16);
771 g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
772 g.TranslateTransform (3, -3);
773 float[] elements = g.Transform.Elements;
774 Assert.AreEqual (1, elements[0], "default.0");
775 Assert.AreEqual (2, elements[1], "default.1");
776 Assert.AreEqual (3, elements[2], "default.2");
777 Assert.AreEqual (4, elements[3], "default.3");
778 Assert.AreEqual (-1, elements[4], "default.4");
779 Assert.AreEqual (0, elements[5], "default.5");
781 g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
782 g.TranslateTransform (3, -3, MatrixOrder.Prepend);
783 elements = g.Transform.Elements;
784 Assert.AreEqual (1, elements[0], "prepend.0");
785 Assert.AreEqual (2, elements[1], "prepend.1");
786 Assert.AreEqual (3, elements[2], "prepend.2");
787 Assert.AreEqual (4, elements[3], "prepend.3");
788 Assert.AreEqual (-1, elements[4], "prepend.4");
789 Assert.AreEqual (0, elements[5], "prepend.5");
791 g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
792 g.TranslateTransform (3, -3, MatrixOrder.Append);
793 elements = g.Transform.Elements;
794 Assert.AreEqual (1, elements[0], "append.0");
795 Assert.AreEqual (2, elements[1], "append.1");
796 Assert.AreEqual (3, elements[2], "append.2");
797 Assert.AreEqual (4, elements[3], "append.3");
798 Assert.AreEqual (8, elements[4], "append.4");
799 Assert.AreEqual (3, elements[5], "append.5");
802 static Point[] SmallCurve = new Point[3] { new Point (0, 0), new Point (15, 5), new Point (5, 15) };
803 static PointF[] SmallCurveF = new PointF[3] { new PointF (0, 0), new PointF (15, 5), new PointF (5, 15) };
805 static Point[] TooSmallCurve = new Point[2] { new Point (0, 0), new Point (15, 5) };
806 static PointF[] LargeCurveF = new PointF[4] { new PointF (0, 0), new PointF (15, 5), new PointF (5, 15), new PointF (0, 20) };
809 public void DrawCurve_PenNull ()
811 Bitmap bitmap = new Bitmap (20, 20);
812 Graphics g = Graphics.FromImage (bitmap);
813 Assert.Throws<ArgumentNullException> (() => g.DrawCurve (null, SmallCurveF));
817 public void DrawCurve_PointFNull ()
819 Bitmap bitmap = new Bitmap (20, 20);
820 Graphics g = Graphics.FromImage (bitmap);
821 Assert.Throws<ArgumentNullException> (() => g.DrawCurve (Pens.Black, (PointF[]) null));
825 public void DrawCurve_PointNull ()
827 Bitmap bitmap = new Bitmap (20, 20);
828 Graphics g = Graphics.FromImage (bitmap);
829 Assert.Throws<ArgumentNullException> (() => g.DrawCurve (Pens.Black, (Point[]) null));
833 public void DrawCurve_NotEnoughPoints ()
835 Bitmap bitmap = new Bitmap (20, 20);
836 Graphics g = Graphics.FromImage (bitmap);
837 CheckForEmptyBitmap (bitmap);
838 g.DrawCurve (Pens.Black, TooSmallCurve, 0.5f);
839 CheckForNonEmptyBitmap (bitmap);
840 // so a "curve" can be drawn with less than 3 points!
841 // actually I used to call that a line... (and it's not related to tension)
847 public void DrawCurve_SinglePoint ()
849 Bitmap bitmap = new Bitmap (20, 20);
850 Graphics g = Graphics.FromImage (bitmap);
851 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, new Point[1] { new Point (10, 10) }, 0.5f));
852 // a single point isn't enough
856 public void DrawCurve3_NotEnoughPoints ()
858 Bitmap bitmap = new Bitmap (20, 20);
859 Graphics g = Graphics.FromImage (bitmap);
860 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, TooSmallCurve, 0, 2, 0.5f));
861 // aha, this is API dependent
865 public void DrawCurve_NegativeTension ()
867 Bitmap bitmap = new Bitmap (20, 20);
868 Graphics g = Graphics.FromImage (bitmap);
869 // documented as bigger (or equals) to 0
870 g.DrawCurve (Pens.Black, SmallCurveF, -0.9f);
871 CheckForNonEmptyBitmap (bitmap);
877 public void DrawCurve_PositiveTension ()
879 Bitmap bitmap = new Bitmap (20, 20);
880 Graphics g = Graphics.FromImage (bitmap);
881 g.DrawCurve (Pens.Black, SmallCurveF, 0.9f);
882 // this is not the same as -1
883 CheckForNonEmptyBitmap (bitmap);
889 [Category ("NotWorking")] // libgdiplus is drawing something
890 public void DrawCurve_LargeTension ()
892 Bitmap bitmap = new Bitmap (20, 20);
893 Graphics g = Graphics.FromImage (bitmap);
894 g.DrawCurve (Pens.Black, SmallCurve, Single.MaxValue);
895 CheckForEmptyBitmap (bitmap);
896 // too much tension ;)
902 public void DrawCurve_ZeroSegments ()
904 Bitmap bitmap = new Bitmap (20, 20);
905 Graphics g = Graphics.FromImage (bitmap);
906 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, SmallCurveF, 0, 0));
910 public void DrawCurve_NegativeSegments ()
912 Bitmap bitmap = new Bitmap (20, 20);
913 Graphics g = Graphics.FromImage (bitmap);
914 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, SmallCurveF, 0, -1));
918 public void DrawCurve_OffsetTooLarge ()
920 Bitmap bitmap = new Bitmap (20, 20);
921 Graphics g = Graphics.FromImage (bitmap);
922 // starting offset 1 doesn't give 3 points to make a curve
923 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, SmallCurveF, 1, 2));
924 // and in this case 2 points aren't enough to draw something
928 public void DrawCurve_Offset_0 ()
930 Bitmap bitmap = new Bitmap (20, 20);
931 Graphics g = Graphics.FromImage (bitmap);
932 g.DrawCurve (Pens.Black, LargeCurveF, 0, 2, 0.5f);
933 CheckForNonEmptyBitmap (bitmap);
939 public void DrawCurve_Offset_1 ()
941 Bitmap bitmap = new Bitmap (20, 20);
942 Graphics g = Graphics.FromImage (bitmap);
943 g.DrawCurve (Pens.Black, LargeCurveF, 1, 2, 0.5f);
944 CheckForNonEmptyBitmap (bitmap);
950 public void DrawCurve_Offset_2 ()
952 Bitmap bitmap = new Bitmap (20, 20);
953 Graphics g = Graphics.FromImage (bitmap);
954 // it works even with two points because we know the previous ones
955 g.DrawCurve (Pens.Black, LargeCurveF, 2, 1, 0.5f);
956 CheckForNonEmptyBitmap (bitmap);
962 public void DrawRectangle_Negative ()
964 Bitmap bitmap = new Bitmap (20, 20);
965 Graphics g = Graphics.FromImage (bitmap);
966 Pen pen = new Pen (Color.Red);
967 g.DrawRectangle (pen, 5, 5, -10, -10);
968 g.DrawRectangle (pen, 0.0f, 0.0f, 5.0f, -10.0f);
969 g.DrawRectangle (pen, new Rectangle (15, 0, -10, 5));
970 CheckForEmptyBitmap (bitmap);
977 public void DrawRectangles_Negative ()
979 Bitmap bitmap = new Bitmap (20, 20);
980 Graphics g = Graphics.FromImage (bitmap);
981 Pen pen = new Pen (Color.Red);
982 Rectangle[] rects = new Rectangle[2] {
983 new Rectangle (5, 5, -10, -10), new Rectangle (0, 0, 5, -10)
985 RectangleF[] rectf = new RectangleF[2] {
986 new RectangleF (0.0f, 5.0f, -10.0f, -10.0f), new RectangleF (15.0f, 0.0f, -10.0f, 5.0f)
988 g.DrawRectangles (pen, rects);
989 g.DrawRectangles (pen, rectf);
990 CheckForEmptyBitmap (bitmap);
997 public void FillRectangle_Negative ()
999 Bitmap bitmap = new Bitmap (20, 20);
1000 Graphics g = Graphics.FromImage (bitmap);
1001 SolidBrush brush = new SolidBrush (Color.Red);
1002 g.FillRectangle (brush, 5, 5, -10, -10);
1003 g.FillRectangle (brush, 0.0f, 0.0f, 5.0f, -10.0f);
1004 g.FillRectangle (brush, new Rectangle (15, 0, -10, 5));
1005 CheckForEmptyBitmap (bitmap);
1012 public void FillRectangles_Negative ()
1014 Bitmap bitmap = new Bitmap (20, 20);
1015 Graphics g = Graphics.FromImage (bitmap);
1016 SolidBrush brush = new SolidBrush (Color.Red);
1017 Rectangle[] rects = new Rectangle[2] {
1018 new Rectangle (5, 5, -10, -10), new Rectangle (0, 0, 5, -10)
1020 RectangleF[] rectf = new RectangleF[2] {
1021 new RectangleF (0.0f, 5.0f, -10.0f, -10.0f), new RectangleF (15.0f, 0.0f, -10.0f, 5.0f)
1023 g.FillRectangles (brush, rects);
1024 g.FillRectangles (brush, rectf);
1025 CheckForEmptyBitmap (bitmap);
1031 [Test] // bug #355141
1033 public void FromHwnd_Zero ()
1035 Graphics g = Graphics.FromHwnd (IntPtr.Zero);
1036 Assert.IsNotNull (g);
1039 private void CheckDefaultProperties (string message, Graphics g)
1041 Assert.IsTrue (g.Clip.IsInfinite (g), message + ".Clip.IsInfinite");
1042 AssertEquals (message + ".CompositingMode", CompositingMode.SourceOver, g.CompositingMode);
1043 AssertEquals (message + ".CompositingQuality", CompositingQuality.Default, g.CompositingQuality);
1044 AssertEquals (message + ".InterpolationMode", InterpolationMode.Bilinear, g.InterpolationMode);
1045 AssertEquals (message + ".PageScale", 1.0f, g.PageScale);
1046 AssertEquals (message + ".PageUnit", GraphicsUnit.Display, g.PageUnit);
1047 AssertEquals (message + ".PixelOffsetMode", PixelOffsetMode.Default, g.PixelOffsetMode);
1048 AssertEquals (message + ".SmoothingMode", SmoothingMode.None, g.SmoothingMode);
1049 AssertEquals (message + ".TextContrast", 4, g.TextContrast);
1050 AssertEquals (message + ".TextRenderingHint", TextRenderingHint.SystemDefault, g.TextRenderingHint);
1051 Assert.IsTrue (g.Transform.IsIdentity, message + ".Transform.IsIdentity");
1054 private void CheckCustomProperties (string message, Graphics g)
1056 Assert.IsFalse (g.Clip.IsInfinite (g), message + ".Clip.IsInfinite");
1057 AssertEquals (message + ".CompositingMode", CompositingMode.SourceCopy, g.CompositingMode);
1058 AssertEquals (message + ".CompositingQuality", CompositingQuality.HighQuality, g.CompositingQuality);
1059 AssertEquals (message + ".InterpolationMode", InterpolationMode.HighQualityBicubic, g.InterpolationMode);
1060 AssertEquals (message + ".PageScale", 0.5f, g.PageScale);
1061 AssertEquals (message + ".PageUnit", GraphicsUnit.Inch, g.PageUnit);
1062 AssertEquals (message + ".PixelOffsetMode", PixelOffsetMode.Half, g.PixelOffsetMode);
1063 AssertEquals (message + ".RenderingOrigin", new Point (-1, -1), g.RenderingOrigin);
1064 AssertEquals (message + ".SmoothingMode", SmoothingMode.AntiAlias, g.SmoothingMode);
1065 AssertEquals (message + ".TextContrast", 0, g.TextContrast);
1066 AssertEquals (message + ".TextRenderingHint", TextRenderingHint.AntiAlias, g.TextRenderingHint);
1067 Assert.IsFalse (g.Transform.IsIdentity, message + ".Transform.IsIdentity");
1070 private void CheckMatrix (string message, Matrix m, float xx, float yx, float xy, float yy, float x0, float y0)
1072 float[] elements = m.Elements;
1073 AssertEquals (message + ".Matrix.xx", xx, elements[0], 0.01);
1074 AssertEquals (message + ".Matrix.yx", yx, elements[1], 0.01);
1075 AssertEquals (message + ".Matrix.xy", xy, elements[2], 0.01);
1076 AssertEquals (message + ".Matrix.yy", yy, elements[3], 0.01);
1077 AssertEquals (message + ".Matrix.x0", x0, elements[4], 0.01);
1078 AssertEquals (message + ".Matrix.y0", y0, elements[5], 0.01);
1082 public void BeginContainer ()
1084 Bitmap bitmap = new Bitmap (20, 20);
1085 Graphics g = Graphics.FromImage (bitmap);
1087 CheckDefaultProperties ("default", g);
1088 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1090 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1091 g.CompositingMode = CompositingMode.SourceCopy;
1092 g.CompositingQuality = CompositingQuality.HighQuality;
1093 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1095 g.PageUnit = GraphicsUnit.Inch;
1096 g.PixelOffsetMode = PixelOffsetMode.Half;
1097 g.RenderingOrigin = new Point (-1, -1);
1098 g.RotateTransform (45);
1099 g.SmoothingMode = SmoothingMode.AntiAlias;
1101 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1102 CheckCustomProperties ("modified", g);
1103 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1105 GraphicsContainer gc = g.BeginContainer ();
1106 // things gets reseted after calling BeginContainer
1107 CheckDefaultProperties ("BeginContainer", g);
1108 // but not everything
1109 Assert.AreEqual (new Point (-1, -1), g.RenderingOrigin, "BeginContainer.RenderingOrigin");
1111 g.EndContainer (gc);
1112 CheckCustomProperties ("EndContainer", g);
1116 public void BeginContainer_Rect ()
1118 Bitmap bitmap = new Bitmap (20, 20);
1119 Graphics g = Graphics.FromImage (bitmap);
1121 CheckDefaultProperties ("default", g);
1122 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1124 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1125 g.CompositingMode = CompositingMode.SourceCopy;
1126 g.CompositingQuality = CompositingQuality.HighQuality;
1127 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1129 g.PageUnit = GraphicsUnit.Inch;
1130 g.PixelOffsetMode = PixelOffsetMode.Half;
1131 g.RenderingOrigin = new Point (-1, -1);
1132 g.RotateTransform (45);
1133 g.SmoothingMode = SmoothingMode.AntiAlias;
1135 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1136 CheckCustomProperties ("modified", g);
1137 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1139 GraphicsContainer gc = g.BeginContainer (new Rectangle (10, 20, 30, 40), new Rectangle (10, 20, 300, 400), GraphicsUnit.Millimeter);
1140 // things gets reseted after calling BeginContainer
1141 CheckDefaultProperties ("BeginContainer", g);
1142 // but not everything
1143 Assert.AreEqual (new Point (-1, -1), g.RenderingOrigin, "BeginContainer.RenderingOrigin");
1145 g.EndContainer (gc);
1146 CheckCustomProperties ("EndContainer", g);
1147 CheckMatrix ("EndContainer.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1151 public void BeginContainer_RectF ()
1153 Bitmap bitmap = new Bitmap (20, 20);
1154 Graphics g = Graphics.FromImage (bitmap);
1156 CheckDefaultProperties ("default", g);
1157 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1159 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1160 g.CompositingMode = CompositingMode.SourceCopy;
1161 g.CompositingQuality = CompositingQuality.HighQuality;
1162 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1164 g.PageUnit = GraphicsUnit.Inch;
1165 g.PixelOffsetMode = PixelOffsetMode.Half;
1166 g.RenderingOrigin = new Point (-1, -1);
1167 g.RotateTransform (45);
1168 g.SmoothingMode = SmoothingMode.AntiAlias;
1170 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1171 CheckCustomProperties ("modified", g);
1172 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1174 GraphicsContainer gc = g.BeginContainer (new RectangleF (40, 30, 20, 10), new RectangleF (10, 20, 30, 40), GraphicsUnit.Inch);
1175 // things gets reseted after calling BeginContainer
1176 CheckDefaultProperties ("BeginContainer", g);
1177 // but not everything
1178 Assert.AreEqual (new Point (-1, -1), g.RenderingOrigin, "BeginContainer.RenderingOrigin");
1180 g.EndContainer (gc);
1181 CheckCustomProperties ("EndContainer", g);
1184 private void BeginContainer_GraphicsUnit (GraphicsUnit unit)
1186 Bitmap bitmap = new Bitmap (20, 20);
1187 Graphics g = Graphics.FromImage (bitmap);
1188 g.BeginContainer (new RectangleF (40, 30, 20, 10), new RectangleF (10, 20, 30, 40), unit);
1192 public void BeginContainer_GraphicsUnit_Display ()
1194 Assert.Throws<ArgumentException> (() => BeginContainer_GraphicsUnit(GraphicsUnit.Display));
1198 public void BeginContainer_GraphicsUnit_Valid ()
1200 BeginContainer_GraphicsUnit (GraphicsUnit.Document);
1201 BeginContainer_GraphicsUnit (GraphicsUnit.Inch);
1202 BeginContainer_GraphicsUnit (GraphicsUnit.Millimeter);
1203 BeginContainer_GraphicsUnit (GraphicsUnit.Pixel);
1204 BeginContainer_GraphicsUnit (GraphicsUnit.Point);
1208 public void BeginContainer_GraphicsUnit_World ()
1210 Assert.Throws<ArgumentException> (() => BeginContainer_GraphicsUnit(GraphicsUnit.World));
1214 public void BeginContainer_GraphicsUnit_Bad ()
1216 Assert.Throws<ArgumentException> (() => BeginContainer_GraphicsUnit((GraphicsUnit) Int32.MinValue));
1220 public void EndContainer_Null ()
1222 Bitmap bitmap = new Bitmap (20, 20);
1223 Graphics g = Graphics.FromImage (bitmap);
1224 Assert.Throws<ArgumentNullException> (() => g.EndContainer (null));
1230 Bitmap bitmap = new Bitmap (20, 20);
1231 Graphics g = Graphics.FromImage (bitmap);
1233 CheckDefaultProperties ("default", g);
1234 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1236 GraphicsState gs1 = g.Save ();
1237 // nothing is changed after a save
1238 CheckDefaultProperties ("save1", g);
1239 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "save1.RenderingOrigin");
1241 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1242 g.CompositingMode = CompositingMode.SourceCopy;
1243 g.CompositingQuality = CompositingQuality.HighQuality;
1244 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1246 g.PageUnit = GraphicsUnit.Inch;
1247 g.PixelOffsetMode = PixelOffsetMode.Half;
1248 g.RenderingOrigin = new Point (-1, -1);
1249 g.RotateTransform (45);
1250 g.SmoothingMode = SmoothingMode.AntiAlias;
1252 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1253 CheckCustomProperties ("modified", g);
1254 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1256 GraphicsState gs2 = g.Save ();
1257 CheckCustomProperties ("save2", g);
1260 CheckCustomProperties ("restored1", g);
1261 CheckMatrix ("restored1.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1264 CheckDefaultProperties ("restored2", g);
1265 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "restored2.RenderingOrigin");
1269 public void Restore_Null ()
1271 Bitmap bitmap = new Bitmap (20, 20);
1272 Graphics g = Graphics.FromImage (bitmap);
1273 Assert.Throws<NullReferenceException> (() => g.Restore (null));
1277 public void FillRectangles_BrushNull_Rectangle ()
1279 using (Bitmap bitmap = new Bitmap (20, 20)) {
1280 using (Graphics g = Graphics.FromImage (bitmap)) {
1281 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (null, new Rectangle[1]));
1287 public void FillRectangles_Rectangle_Null ()
1289 using (Bitmap bitmap = new Bitmap (20, 20)) {
1290 using (Graphics g = Graphics.FromImage (bitmap)) {
1291 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (Brushes.Red, (Rectangle[]) null));
1296 [Test] // see bug #78408
1297 public void FillRectanglesZeroRectangle ()
1299 using (Bitmap bitmap = new Bitmap (20, 20)) {
1300 using (Graphics g = Graphics.FromImage (bitmap)) {
1301 Assert.Throws<ArgumentException> (() => g.FillRectangles (Brushes.Red, new Rectangle[0]));
1307 public void FillRectangles_BrushNull_RectangleF ()
1309 using (Bitmap bitmap = new Bitmap (20, 20)) {
1310 using (Graphics g = Graphics.FromImage (bitmap)) {
1311 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (null, new RectangleF[1]));
1317 public void FillRectangles_RectangleF_Null ()
1319 using (Bitmap bitmap = new Bitmap (20, 20)) {
1320 using (Graphics g = Graphics.FromImage (bitmap)) {
1321 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (Brushes.Red, (RectangleF[]) null));
1326 [Test] // see bug #78408
1327 public void FillRectanglesZeroRectangleF ()
1329 using (Bitmap bitmap = new Bitmap (20, 20)) {
1330 using (Graphics g = Graphics.FromImage (bitmap)) {
1331 Assert.Throws<ArgumentException> (() => g.FillRectangles (Brushes.Red, new RectangleF[0]));
1337 public void FillRectangles_NormalBehavior ()
1339 using (Bitmap bitmap = new Bitmap (20, 20)) {
1340 using (Graphics g = Graphics.FromImage (bitmap)) {
1341 g.Clear (Color.Fuchsia);
1342 Rectangle rect = new Rectangle (5, 5, 10, 10);
1343 g.Clip = new Region (rect);
1344 g.FillRectangle (Brushes.Red, rect);
1346 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1347 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1348 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (5, 14).ToArgb (), "5,14");
1349 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1351 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1352 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1353 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1357 // see bug #81737 for details
1358 private Bitmap FillDrawRectangle (float width)
1360 Bitmap bitmap = new Bitmap (20, 20);
1361 using (Graphics g = Graphics.FromImage (bitmap)) {
1362 g.Clear (Color.Red);
1363 Rectangle rect = new Rectangle (5, 5, 10, 10);
1364 g.FillRectangle (Brushes.Green, rect);
1366 using (Pen pen = new Pen (Color.Blue, width)) {
1367 g.DrawRectangle (pen, rect);
1370 g.DrawRectangle (Pens.Blue, rect);
1377 public void FillDrawRectangle_Width_Default ()
1380 using (Bitmap bitmap = FillDrawRectangle (Single.MinValue)) {
1382 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1383 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1384 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1386 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1387 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1388 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1390 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1391 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1392 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1394 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1395 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1396 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1398 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1399 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1400 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1402 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1403 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1404 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1406 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1407 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1408 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1410 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1411 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1412 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1417 [Category ("NotOnMac")]
1418 public void FillDrawRectangle_Width_2 ()
1421 using (Bitmap bitmap = FillDrawRectangle (2.0f)) {
1423 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1424 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1425 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1426 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1428 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1429 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1430 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1431 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1433 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1434 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1435 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1436 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1438 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1439 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1440 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1441 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 9).ToArgb (), "13,9");
1443 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1444 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1445 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1446 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 13).ToArgb (), "13,13");
1448 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1449 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1450 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1451 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 13).ToArgb (), "9,13");
1453 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 16).ToArgb (), "3,16");
1454 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 15).ToArgb (), "4,15");
1455 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 14).ToArgb (), "5,14");
1456 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 13).ToArgb (), "6,13");
1458 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1459 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1460 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1461 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1466 public void FillDrawRectangle_Width_3 ()
1469 using (Bitmap bitmap = FillDrawRectangle (3.0f)) {
1471 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1472 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1473 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1474 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1475 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 7).ToArgb (), "7,7");
1477 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1478 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1479 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1480 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1481 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 7).ToArgb (), "9,7");
1483 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1484 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1485 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1486 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1487 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 7).ToArgb (), "13,7");
1489 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 9).ToArgb (), "17,9");
1490 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1491 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1492 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1493 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 9).ToArgb (), "13,9");
1495 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 17).ToArgb (), "17,17");
1496 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1497 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1498 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1499 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 13).ToArgb (), "13,13");
1501 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 17).ToArgb (), "9,17");
1502 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1503 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1504 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1505 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 13).ToArgb (), "9,13");
1507 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 17).ToArgb (), "3,17");
1508 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1509 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1510 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1511 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 13).ToArgb (), "7,13");
1513 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1514 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1515 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1516 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1517 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 9).ToArgb (), "7,9");
1521 // reverse, draw the fill over
1522 private Bitmap DrawFillRectangle (float width)
1524 Bitmap bitmap = new Bitmap (20, 20);
1525 using (Graphics g = Graphics.FromImage (bitmap)) {
1526 g.Clear (Color.Red);
1527 Rectangle rect = new Rectangle (5, 5, 10, 10);
1529 using (Pen pen = new Pen (Color.Blue, width)) {
1530 g.DrawRectangle (pen, rect);
1533 g.DrawRectangle (Pens.Blue, rect);
1535 g.FillRectangle (Brushes.Green, rect);
1541 public void DrawFillRectangle_Width_Default ()
1544 using (Bitmap bitmap = DrawFillRectangle (Single.MinValue)) {
1545 // NW - no blue border
1546 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1547 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1548 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1549 // N - no blue border
1550 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1551 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1552 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1554 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1555 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1556 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1558 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1559 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1560 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1562 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1563 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1564 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1566 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1567 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1568 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1570 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1571 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1572 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1573 // W - no blue border
1574 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1575 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1576 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1581 [Category ("NotOnMac")]
1582 public void DrawFillRectangle_Width_2 ()
1585 using (Bitmap bitmap = DrawFillRectangle (2.0f)) {
1586 // looks like a one pixel border - but enlarged
1588 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1589 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1590 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1592 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1593 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1594 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1596 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1597 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1598 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1600 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1601 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1602 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1604 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1605 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1606 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1608 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1609 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1610 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1612 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 16).ToArgb (), "4,16");
1613 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 15).ToArgb (), "5,15");
1614 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 14).ToArgb (), "6,14");
1616 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1617 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1618 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1623 public void DrawFillRectangle_Width_3 ()
1626 using (Bitmap bitmap = DrawFillRectangle (3.0f)) {
1628 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1629 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1630 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1632 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1633 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1634 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1636 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1637 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1638 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1639 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1641 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 9).ToArgb (), "17,9");
1642 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1643 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1644 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1646 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 17).ToArgb (), "17,17");
1647 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1648 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1649 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1651 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 17).ToArgb (), "9,17");
1652 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1653 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1654 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1656 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 17).ToArgb (), "3,17");
1657 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1658 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1659 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1661 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1662 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1663 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1667 private Bitmap DrawLines (float width)
1669 Bitmap bitmap = new Bitmap (20, 20);
1670 using (Graphics g = Graphics.FromImage (bitmap)) {
1671 g.Clear (Color.Red);
1672 Point[] pts = new Point[3] { new Point (5, 5), new Point (15, 5), new Point (15, 15) };
1674 using (Pen pen = new Pen (Color.Blue, width)) {
1675 g.DrawLines (pen, pts);
1678 g.DrawLines (Pens.Blue, pts);
1685 public void DrawLines_Width_Default ()
1688 using (Bitmap bitmap = DrawLines (Single.MinValue)) {
1690 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1691 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1692 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1693 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1694 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1695 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1697 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1698 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1699 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1700 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1701 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1702 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1703 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1704 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1705 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1707 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1708 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1709 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1710 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 16).ToArgb (), "14,16");
1711 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 16).ToArgb (), "15,16");
1712 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1717 [Category ("NotWorking")]
1718 public void DrawLines_Width_2 ()
1721 using (Bitmap bitmap = DrawLines (2.0f)) {
1723 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 3).ToArgb (), "4,3");
1724 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1725 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1726 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1727 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 3).ToArgb (), "5,3");
1728 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1729 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1730 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1732 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 3).ToArgb (), "13,3");
1733 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 4).ToArgb (), "13,4");
1734 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 5).ToArgb (), "13,5");
1735 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1736 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 3).ToArgb (), "14,3");
1737 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1738 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1739 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1740 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 3).ToArgb (), "15,3");
1741 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1742 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1743 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1744 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1745 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1746 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1747 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1749 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 14).ToArgb (), "13,14");
1750 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1751 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 14).ToArgb (), "15,14");
1752 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 14).ToArgb (), "16,14");
1753 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 15).ToArgb (), "13,15");
1754 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1755 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1756 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1761 [Category ("NotWorking")]
1762 public void DrawLines_Width_3 ()
1765 using (Bitmap bitmap = DrawLines (3.0f)) {
1767 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 3).ToArgb (), "4,3");
1768 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1769 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1770 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1771 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 7).ToArgb (), "4,7");
1772 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 3).ToArgb (), "5,3");
1773 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1774 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1775 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1776 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 7).ToArgb (), "5,7");
1778 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 3).ToArgb (), "13,3");
1779 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 4).ToArgb (), "13,4");
1780 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 5).ToArgb (), "13,5");
1781 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1782 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 7).ToArgb (), "13,7");
1783 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 3).ToArgb (), "14,3");
1784 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1785 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1786 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1787 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 7).ToArgb (), "14,7");
1788 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 3).ToArgb (), "15,3");
1789 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1790 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1791 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1792 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 7).ToArgb (), "15,7");
1793 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1794 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1795 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1796 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1797 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 7).ToArgb (), "16,7");
1798 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1799 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 4).ToArgb (), "17,4");
1800 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 5).ToArgb (), "17,5");
1801 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 6).ToArgb (), "17,6");
1802 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 7).ToArgb (), "17,7");
1804 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 14).ToArgb (), "13,14");
1805 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1806 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 14).ToArgb (), "15,14");
1807 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 14).ToArgb (), "16,14");
1808 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 14).ToArgb (), "17,14");
1809 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 15).ToArgb (), "13,15");
1810 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1811 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1812 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1813 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 15).ToArgb (), "17,15");
1818 public void MeasureString_StringFont ()
1820 using (Bitmap bitmap = new Bitmap (20, 20)) {
1821 using (Graphics g = Graphics.FromImage (bitmap)) {
1822 SizeF size = g.MeasureString (null, font);
1823 Assert.IsTrue (size.IsEmpty, "MeasureString(null,font)");
1824 size = g.MeasureString (String.Empty, font);
1825 Assert.IsTrue (size.IsEmpty, "MeasureString(empty,font)");
1827 size = g.MeasureString (null, null);
1828 Assert.IsTrue (size.IsEmpty, "MeasureString(null,null)");
1829 size = g.MeasureString (String.Empty, null);
1830 Assert.IsTrue (size.IsEmpty, "MeasureString(empty,null)");
1836 public void MeasureString_StringFont_Null ()
1838 using (Bitmap bitmap = new Bitmap (20, 20)) {
1839 using (Graphics g = Graphics.FromImage (bitmap)) {
1840 Assert.Throws<ArgumentNullException> (() => g.MeasureString ("a", null));
1846 public void MeasureString_StringFontSizeF ()
1849 Assert.Ignore ("Couldn't create required font");
1851 using (Bitmap bitmap = new Bitmap (20, 20)) {
1852 using (Graphics g = Graphics.FromImage (bitmap)) {
1853 SizeF size = g.MeasureString ("a", font, SizeF.Empty);
1854 Assert.IsFalse (size.IsEmpty, "MeasureString(a,font,empty)");
1856 size = g.MeasureString (String.Empty, font, SizeF.Empty);
1857 Assert.IsTrue (size.IsEmpty, "MeasureString(empty,font,empty)");
1862 private void MeasureString_StringFontInt (string s)
1865 Assert.Ignore ("Couldn't create required font");
1867 using (Bitmap bitmap = new Bitmap (20, 20)) {
1868 using (Graphics g = Graphics.FromImage (bitmap)) {
1869 SizeF size0 = g.MeasureString (s, font, 0);
1870 SizeF sizeN = g.MeasureString (s, font, Int32.MinValue);
1871 SizeF sizeP = g.MeasureString (s, font, Int32.MaxValue);
1872 Assert.AreEqual (size0, sizeN, "0-Min");
1873 Assert.AreEqual (size0, sizeP, "0-Max");
1879 public void MeasureString_StringFontInt_ShortString ()
1881 MeasureString_StringFontInt ("a");
1885 public void MeasureString_StringFontInt_LongString ()
1887 HostIgnoreList.CheckTest ("MonoTests.System.Drawing.GraphicsTest.MeasureString_StringFontInt_LongString");
1888 MeasureString_StringFontInt ("A very long string..."); // see bug #79643
1892 public void MeasureString_StringFormat_Alignment ()
1895 Assert.Ignore ("Couldn't create required font");
1897 string text = "Hello Mono::";
1898 StringFormat string_format = new StringFormat ();
1900 using (Bitmap bitmap = new Bitmap (20, 20)) {
1901 using (Graphics g = Graphics.FromImage (bitmap)) {
1902 string_format.Alignment = StringAlignment.Near;
1903 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1905 string_format.Alignment = StringAlignment.Center;
1906 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1908 string_format.Alignment = StringAlignment.Far;
1909 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1911 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1912 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1914 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1915 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1921 public void MeasureString_StringFormat_Alignment_DirectionVertical ()
1924 Assert.Ignore ("Couldn't create required font");
1926 string text = "Hello Mono::";
1927 StringFormat string_format = new StringFormat ();
1928 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
1930 using (Bitmap bitmap = new Bitmap (20, 20)) {
1931 using (Graphics g = Graphics.FromImage (bitmap)) {
1932 string_format.Alignment = StringAlignment.Near;
1933 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1935 string_format.Alignment = StringAlignment.Center;
1936 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1938 string_format.Alignment = StringAlignment.Far;
1939 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1941 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1942 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1944 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1945 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1951 public void MeasureString_StringFormat_LineAlignment ()
1954 Assert.Ignore ("Couldn't create required font");
1956 string text = "Hello Mono::";
1957 StringFormat string_format = new StringFormat ();
1959 using (Bitmap bitmap = new Bitmap (20, 20)) {
1960 using (Graphics g = Graphics.FromImage (bitmap)) {
1961 string_format.LineAlignment = StringAlignment.Near;
1962 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1964 string_format.LineAlignment = StringAlignment.Center;
1965 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1967 string_format.LineAlignment = StringAlignment.Far;
1968 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1970 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1971 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1973 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1974 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1980 public void MeasureString_StringFormat_LineAlignment_DirectionVertical ()
1983 Assert.Ignore ("Couldn't create required font");
1985 string text = "Hello Mono::";
1986 StringFormat string_format = new StringFormat ();
1987 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
1989 using (Bitmap bitmap = new Bitmap (20, 20)) {
1990 using (Graphics g = Graphics.FromImage (bitmap)) {
1991 string_format.LineAlignment = StringAlignment.Near;
1992 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1994 string_format.LineAlignment = StringAlignment.Center;
1995 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1997 string_format.LineAlignment = StringAlignment.Far;
1998 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
2000 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2001 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2003 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2004 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2010 public void MeasureString_MultlineString_Width ()
2012 using (Bitmap bitmap = new Bitmap (20, 20)) {
2013 using (Graphics g = Graphics.FromImage (bitmap)) {
2014 StringFormat string_format = new StringFormat ();
2016 string text1 = "Test\nTest123\nTest 456\nTest 1,2,3,4,5...";
2017 string text2 = "Test 1,2,3,4,5...";
2019 SizeF size1 = g.MeasureString (text1, font, SizeF.Empty, string_format);
2020 SizeF size2 = g.MeasureString (text2, font, SizeF.Empty, string_format);
2022 Assert.AreEqual ((int) size1.Width, (int) size2.Width, "Multiline Text Width");
2028 public void MeasureString_Bug76664 ()
2031 Assert.Ignore ("Couldn't create required font");
2033 using (Bitmap bitmap = new Bitmap (20, 20)) {
2034 using (Graphics g = Graphics.FromImage (bitmap)) {
2035 string s = "aaa aa aaaa a aaa";
2036 SizeF size = g.MeasureString (s, font);
2039 SizeF size2 = g.MeasureString (s, font, new SizeF (80, size.Height), null, out chars, out lines);
2042 Assert.IsTrue (size2.Width < size.Width, "Width/pixel");
2043 Assert.AreEqual (size2.Height, size.Height, "Height/pixel");
2045 Assert.AreEqual (1, lines, "lines fitted");
2046 // LAMESPEC: documentation seems to suggest chars is total length
2047 Assert.IsTrue (chars < s.Length, "characters fitted");
2053 public void MeasureString_Bug80680 ()
2056 Assert.Ignore ("Couldn't create required font");
2058 using (Bitmap bitmap = new Bitmap (20, 20)) {
2059 using (Graphics g = Graphics.FromImage (bitmap)) {
2060 string s = String.Empty;
2061 SizeF size = g.MeasureString (s, font);
2062 Assert.AreEqual (0, size.Height, "Empty.Height");
2063 Assert.AreEqual (0, size.Width, "Empty.Width");
2066 SizeF expected = g.MeasureString (s, font);
2067 for (int i = 1; i < 10; i++) {
2069 size = g.MeasureString (s, font);
2070 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2071 Assert.AreEqual (expected.Width, size.Width, 0.1, ">" + s + "< Width");
2075 expected = g.MeasureString (s, font);
2077 size = g.MeasureString (s, font);
2078 float space_width = size.Width - expected.Width;
2079 for (int i = 1; i < 10; i++) {
2080 size = g.MeasureString (s, font);
2081 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2082 Assert.AreEqual (expected.Width + i * space_width, size.Width, 0.1, ">" + s + "< Width");
2087 expected = g.MeasureString (s, font);
2088 for (int i = 1; i < 10; i++) {
2090 size = g.MeasureString (s, font);
2091 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2092 Assert.AreEqual (expected.Width, size.Width, 0.1, ">" + s + "< Width");
2099 public void MeasureCharacterRanges_NullOrEmptyText ()
2101 using (Bitmap bitmap = new Bitmap (20, 20)) {
2102 using (Graphics g = Graphics.FromImage (bitmap)) {
2103 Region[] regions = g.MeasureCharacterRanges (null, font, new RectangleF (), null);
2104 Assert.AreEqual (0, regions.Length, "text null");
2105 regions = g.MeasureCharacterRanges (String.Empty, font, new RectangleF (), null);
2106 Assert.AreEqual (0, regions.Length, "text empty");
2107 // null font is ok with null or empty string
2108 regions = g.MeasureCharacterRanges (null, null, new RectangleF (), null);
2109 Assert.AreEqual (0, regions.Length, "text null/null font");
2110 regions = g.MeasureCharacterRanges (String.Empty, null, new RectangleF (), null);
2111 Assert.AreEqual (0, regions.Length, "text empty/null font");
2117 public void MeasureCharacterRanges_EmptyStringFormat ()
2120 Assert.Ignore ("Couldn't create required font");
2122 using (Bitmap bitmap = new Bitmap (20, 20)) {
2123 using (Graphics g = Graphics.FromImage (bitmap)) {
2124 // string format without character ranges
2125 Region[] regions = g.MeasureCharacterRanges ("Mono", font, new RectangleF (), new StringFormat ());
2126 Assert.AreEqual (0, regions.Length, "empty stringformat");
2132 public void MeasureCharacterRanges_FontNull ()
2134 using (Bitmap bitmap = new Bitmap (20, 20)) {
2135 using (Graphics g = Graphics.FromImage (bitmap)) {
2136 Assert.Throws<ArgumentNullException> (() => g.MeasureCharacterRanges ("a", null, new RectangleF (), null));
2141 [Test] // adapted from bug #78777
2142 public void MeasureCharacterRanges_TwoLines ()
2145 Assert.Ignore ("Couldn't create required font");
2147 string text = "this\nis a test";
2148 CharacterRange[] ranges = new CharacterRange[2];
2149 ranges[0] = new CharacterRange (0, 5);
2150 ranges[1] = new CharacterRange (5, 9);
2152 StringFormat string_format = new StringFormat ();
2153 string_format.FormatFlags = StringFormatFlags.NoClip;
2154 string_format.SetMeasurableCharacterRanges (ranges);
2156 using (Bitmap bitmap = new Bitmap (20, 20)) {
2157 using (Graphics g = Graphics.FromImage (bitmap)) {
2158 SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2159 RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2160 Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2162 Assert.AreEqual (2, regions.Length, "Length");
2163 Assert.AreEqual (regions[0].GetBounds (g).Height, regions[1].GetBounds (g).Height, "Height");
2168 private void MeasureCharacterRanges (string text, int first, int length)
2171 Assert.Ignore ("Couldn't create required font");
2173 CharacterRange[] ranges = new CharacterRange[1];
2174 ranges[0] = new CharacterRange (first, length);
2176 StringFormat string_format = new StringFormat ();
2177 string_format.FormatFlags = StringFormatFlags.NoClip;
2178 string_format.SetMeasurableCharacterRanges (ranges);
2180 using (Bitmap bitmap = new Bitmap (20, 20)) {
2181 using (Graphics g = Graphics.FromImage (bitmap)) {
2182 SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2183 RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2184 g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2190 public void MeasureCharacterRanges_FirstTooFar ()
2192 string text = "this\nis a test";
2193 Assert.Throws<ArgumentException> (() => MeasureCharacterRanges(text, text.Length, 1));
2197 public void MeasureCharacterRanges_LengthTooLong ()
2199 string text = "this\nis a test";
2200 Assert.Throws<ArgumentException> (() => MeasureCharacterRanges(text, 0, text.Length + 1));
2204 public void MeasureCharacterRanges_Prefix ()
2207 Assert.Ignore ("Couldn't create required font");
2209 string text = "Hello &Mono::";
2210 CharacterRange[] ranges = new CharacterRange[1];
2211 ranges[0] = new CharacterRange (5, 4);
2213 StringFormat string_format = new StringFormat ();
2214 string_format.SetMeasurableCharacterRanges (ranges);
2216 using (Bitmap bitmap = new Bitmap (20, 20)) {
2217 using (Graphics g = Graphics.FromImage (bitmap)) {
2218 SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2219 RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2221 // here & is part of the measure and visible
2222 string_format.HotkeyPrefix = HotkeyPrefix.None;
2223 Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2224 RectangleF bounds_none = regions[0].GetBounds (g);
2226 // here & is part of the measure (range) but visible as an underline
2227 string_format.HotkeyPrefix = HotkeyPrefix.Show;
2228 regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2229 RectangleF bounds_show = regions[0].GetBounds (g);
2230 Assert.IsTrue (bounds_show.Width < bounds_none.Width, "Show<None");
2232 // here & is part of the measure (range) but invisible
2233 string_format.HotkeyPrefix = HotkeyPrefix.Hide;
2234 regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2235 RectangleF bounds_hide = regions[0].GetBounds (g);
2236 Assert.AreEqual (bounds_hide.Width, bounds_show.Width, "Hide==None");
2242 public void MeasureCharacterRanges_NullStringFormat ()
2245 Assert.Ignore ("Couldn't create required font");
2247 using (Bitmap bitmap = new Bitmap (20, 20)) {
2248 using (Graphics g = Graphics.FromImage (bitmap)) {
2249 Assert.Throws<ArgumentException> (() => g.MeasureCharacterRanges ("Mono", font, new RectangleF (), null));
2255 [Category ("NotWorking")]
2256 public void MeasureCharacterRanges_StringFormat_Alignment ()
2259 Assert.Ignore ("Couldn't create required font");
2261 string text = "Hello Mono::";
2262 CharacterRange[] ranges = new CharacterRange[1];
2263 ranges[0] = new CharacterRange (5, 4);
2264 StringFormat string_format = new StringFormat ();
2265 string_format.SetMeasurableCharacterRanges (ranges);
2267 using (Bitmap bitmap = new Bitmap (20, 20)) {
2268 using (Graphics g = Graphics.FromImage (bitmap)) {
2269 string_format.Alignment = StringAlignment.Near;
2270 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2271 Assert.AreEqual (1, regions.Length, "Near.Region");
2272 RectangleF near = regions[0].GetBounds (g);
2274 string_format.Alignment = StringAlignment.Center;
2275 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2276 Assert.AreEqual (1, regions.Length, "Center.Region");
2277 RectangleF center = regions[0].GetBounds (g);
2279 string_format.Alignment = StringAlignment.Far;
2280 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2281 Assert.AreEqual (1, regions.Length, "Far.Region");
2282 RectangleF far = regions[0].GetBounds (g);
2284 Assert.IsTrue (near.X < center.X, "near-center/X");
2285 Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y");
2286 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2287 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2289 Assert.IsTrue (center.X < far.X, "center-far/X");
2290 Assert.AreEqual (center.Y, far.Y, "center-far/Y");
2291 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2292 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2298 [Category ("NotWorking")]
2299 public void MeasureCharacterRanges_StringFormat_LineAlignment ()
2302 Assert.Ignore ("Couldn't create required font");
2304 string text = "Hello Mono::";
2305 CharacterRange[] ranges = new CharacterRange[1];
2306 ranges[0] = new CharacterRange (5, 4);
2307 StringFormat string_format = new StringFormat ();
2308 string_format.SetMeasurableCharacterRanges (ranges);
2310 using (Bitmap bitmap = new Bitmap (20, 20)) {
2311 using (Graphics g = Graphics.FromImage (bitmap)) {
2312 string_format.LineAlignment = StringAlignment.Near;
2313 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2314 Assert.AreEqual (1, regions.Length, "Near.Region");
2315 RectangleF near = regions[0].GetBounds (g);
2317 string_format.LineAlignment = StringAlignment.Center;
2318 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2319 Assert.AreEqual (1, regions.Length, "Center.Region");
2320 RectangleF center = regions[0].GetBounds (g);
2322 string_format.LineAlignment = StringAlignment.Far;
2323 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2324 Assert.AreEqual (1, regions.Length, "Far.Region");
2325 RectangleF far = regions[0].GetBounds (g);
2327 Assert.AreEqual (near.X, center.X, 0.1, "near-center/X");
2328 Assert.IsTrue (near.Y < center.Y, "near-center/Y");
2329 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2330 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2332 Assert.AreEqual (center.X, far.X, 0.1, "center-far/X");
2333 Assert.IsTrue (center.Y < far.Y, "center-far/Y");
2334 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2335 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2341 [Category ("NotWorking")]
2342 public void MeasureCharacterRanges_StringFormat_Alignment_DirectionVertical ()
2345 Assert.Ignore ("Couldn't create required font");
2347 string text = "Hello Mono::";
2348 CharacterRange[] ranges = new CharacterRange[1];
2349 ranges[0] = new CharacterRange (5, 4);
2350 StringFormat string_format = new StringFormat ();
2351 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2352 string_format.SetMeasurableCharacterRanges (ranges);
2354 using (Bitmap bitmap = new Bitmap (20, 20)) {
2355 using (Graphics g = Graphics.FromImage (bitmap)) {
2356 string_format.Alignment = StringAlignment.Near;
2357 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2358 Assert.AreEqual (1, regions.Length, "Near.Region");
2359 RectangleF near = regions[0].GetBounds (g);
2361 string_format.Alignment = StringAlignment.Center;
2362 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2363 Assert.AreEqual (1, regions.Length, "Center.Region");
2364 RectangleF center = regions[0].GetBounds (g);
2366 string_format.Alignment = StringAlignment.Far;
2367 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2368 Assert.AreEqual (1, regions.Length, "Far.Region");
2369 RectangleF far = regions[0].GetBounds (g);
2371 Assert.IsTrue (near.X == center.X, "near-center/X"); // ???
2372 Assert.IsTrue (near.Y < center.Y, "near-center/Y");
2373 Assert.IsTrue (near.Width == center.Width, "near-center/Width"); // ???
2374 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2376 Assert.AreEqual (center.X, far.X, 0.1, "center-far/X");
2377 Assert.IsTrue (center.Y < far.Y, "center-far/Y");
2378 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2379 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2385 [Category ("NotWorking")]
2386 public void MeasureCharacterRanges_StringFormat_LineAlignment_DirectionVertical ()
2389 Assert.Ignore ("Couldn't create required font");
2391 string text = "Hello Mono::";
2392 CharacterRange[] ranges = new CharacterRange[1];
2393 ranges[0] = new CharacterRange (5, 4);
2394 StringFormat string_format = new StringFormat ();
2395 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2396 string_format.SetMeasurableCharacterRanges (ranges);
2398 using (Bitmap bitmap = new Bitmap (20, 20)) {
2399 using (Graphics g = Graphics.FromImage (bitmap)) {
2400 string_format.LineAlignment = StringAlignment.Near;
2401 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2402 Assert.AreEqual (1, regions.Length, "Near.Region");
2403 RectangleF near = regions[0].GetBounds (g);
2405 string_format.LineAlignment = StringAlignment.Center;
2406 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2407 Assert.AreEqual (1, regions.Length, "Center.Region");
2408 RectangleF center = regions[0].GetBounds (g);
2410 string_format.LineAlignment = StringAlignment.Far;
2411 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2412 Assert.AreEqual (1, regions.Length, "Far.Region");
2413 RectangleF far = regions[0].GetBounds (g);
2415 Assert.IsTrue (near.X < center.X, "near-center/X");
2416 Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y");
2417 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2418 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2420 Assert.IsTrue (center.X < far.X, "center-far/X");
2421 Assert.AreEqual (center.Y, far.Y, 0.1, "center-far/Y");
2422 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2423 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2428 static CharacterRange [] ranges = new CharacterRange [] {
2429 new CharacterRange (0, 1),
2430 new CharacterRange (1, 1),
2431 new CharacterRange (2, 1)
2434 Region [] Measure (Graphics gfx, RectangleF rect)
2436 using (StringFormat format = StringFormat.GenericTypographic) {
2437 format.SetMeasurableCharacterRanges (ranges);
2439 using (Font font = new Font (FontFamily.GenericSerif, 11.0f)) {
2440 return gfx.MeasureCharacterRanges ("abc", font, rect, format);
2446 public void Measure ()
2448 using (Graphics gfx = Graphics.FromImage (new Bitmap (1, 1))) {
2449 Region [] zero = Measure (gfx, new RectangleF (0, 0, 0, 0));
2450 Assert.AreEqual (3, zero.Length, "zero.Length");
2452 Region [] small = Measure (gfx, new RectangleF (0, 0, 100, 100));
2453 Assert.AreEqual (3, small.Length, "small.Length");
2454 for (int i = 0; i < 3; i++ ) {
2455 RectangleF zb = zero [i].GetBounds (gfx);
2456 RectangleF sb = small [i].GetBounds (gfx);
2457 Assert.AreEqual (sb.X, zb.X, "sx" + i.ToString ());
2458 Assert.AreEqual (sb.Y, zb.Y, "sy" + i.ToString ());
2459 Assert.AreEqual (sb.Width, zb.Width, "sw" + i.ToString ());
2460 Assert.AreEqual (sb.Height, zb.Height, "sh" + i.ToString ());
2463 Region [] max = Measure (gfx, new RectangleF (0, 0, Single.MaxValue, Single.MaxValue));
2464 Assert.AreEqual (3, max.Length, "empty.Length");
2465 for (int i = 0; i < 3; i++) {
2466 RectangleF zb = zero [i].GetBounds (gfx);
2467 RectangleF mb = max [i].GetBounds (gfx);
2468 Assert.AreEqual (mb.X, zb.X, "mx" + i.ToString ());
2469 Assert.AreEqual (mb.Y, zb.Y, "my" + i.ToString ());
2470 Assert.AreEqual (mb.Width, zb.Width, "mw" + i.ToString ());
2471 Assert.AreEqual (mb.Height, zb.Height, "mh" + i.ToString ());
2477 public void MeasureLimits ()
2479 using (Graphics gfx = Graphics.FromImage (new Bitmap (1, 1))) {
2480 Region [] min = Measure (gfx, new RectangleF (0, 0, Single.MinValue, Single.MinValue));
2481 Assert.AreEqual (3, min.Length, "origin.Length");
2482 for (int i = 0; i < 3; i++) {
2483 RectangleF mb = min [i].GetBounds (gfx);
2484 Assert.AreEqual (-4194304.0f, mb.X, "minx" + i.ToString ());
2485 Assert.AreEqual (-4194304.0f, mb.Y, "miny" + i.ToString ());
2486 Assert.AreEqual (8388608.0f, mb.Width, "minw" + i.ToString ());
2487 Assert.AreEqual (8388608.0f, mb.Height, "minh" + i.ToString ());
2490 Region [] neg = Measure (gfx, new RectangleF (0, 0, -20, -20));
2491 Assert.AreEqual (3, neg.Length, "neg.Length");
2492 for (int i = 0; i < 3; i++) {
2493 RectangleF mb = neg [i].GetBounds (gfx);
2494 Assert.AreEqual (-4194304.0f, mb.X, "minx" + i.ToString ());
2495 Assert.AreEqual (-4194304.0f, mb.Y, "miny" + i.ToString ());
2496 Assert.AreEqual (8388608.0f, mb.Width, "minw" + i.ToString ());
2497 Assert.AreEqual (8388608.0f, mb.Height, "minh" + i.ToString ());
2503 public void DrawString_EndlessLoop_Bug77699 ()
2506 Assert.Ignore ("Couldn't create required font");
2508 using (Bitmap bitmap = new Bitmap (20, 20)) {
2509 using (Graphics g = Graphics.FromImage (bitmap)) {
2510 Rectangle rect = Rectangle.Empty;
2511 rect.Location = new Point (10, 10);
2512 rect.Size = new Size (1, 20);
2513 StringFormat fmt = new StringFormat ();
2514 fmt.Alignment = StringAlignment.Center;
2515 fmt.LineAlignment = StringAlignment.Center;
2516 fmt.FormatFlags = StringFormatFlags.NoWrap;
2517 fmt.Trimming = StringTrimming.EllipsisWord;
2518 g.DrawString ("Test String", font, Brushes.Black, rect, fmt);
2524 public void DrawString_EndlessLoop_Wrapping ()
2527 Assert.Ignore ("Couldn't create required font");
2529 using (Bitmap bitmap = new Bitmap (20, 20)) {
2530 using (Graphics g = Graphics.FromImage (bitmap)) {
2531 Rectangle rect = Rectangle.Empty;
2532 rect.Location = new Point (10, 10);
2533 rect.Size = new Size (1, 20);
2534 StringFormat fmt = new StringFormat ();
2535 fmt.Alignment = StringAlignment.Center;
2536 fmt.LineAlignment = StringAlignment.Center;
2537 fmt.Trimming = StringTrimming.EllipsisWord;
2538 g.DrawString ("Test String", font, Brushes.Black, rect, fmt);
2544 public void MeasureString_Wrapping_Dots ()
2546 HostIgnoreList.CheckTest ("MonoTests.System.Drawing.GraphicsTest.MeasureString_Wrapping_Dots");
2549 Assert.Ignore ("Couldn't create required font");
2551 string text = "this is really long text........................................... with a lot o periods.";
2552 using (Bitmap bitmap = new Bitmap (20, 20)) {
2553 using (Graphics g = Graphics.FromImage (bitmap)) {
2554 using (StringFormat format = new StringFormat ()) {
2555 format.Alignment = StringAlignment.Center;
2556 SizeF sz = g.MeasureString (text, font, 80, format);
2557 Assert.IsTrue (sz.Width < 80, "Width");
2558 Assert.IsTrue (sz.Height > font.Height * 2, "Height");
2565 public void GetReleaseHdcInternal ()
2567 using (Bitmap b = new Bitmap (10, 10)) {
2568 using (Graphics g = Graphics.FromImage (b)) {
2569 IntPtr hdc1 = g.GetHdc ();
2570 g.ReleaseHdcInternal (hdc1);
2571 IntPtr hdc2 = g.GetHdc ();
2572 g.ReleaseHdcInternal (hdc2);
2573 Assert.AreEqual (hdc1, hdc2, "hdc");
2579 public void ReleaseHdcInternal_IntPtrZero ()
2581 using (Bitmap b = new Bitmap (10, 10)) {
2582 using (Graphics g = Graphics.FromImage (b)) {
2583 Assert.Throws<ArgumentException> (() => g.ReleaseHdcInternal (IntPtr.Zero));
2589 public void ReleaseHdcInternal_TwoTimes ()
2591 using (Bitmap b = new Bitmap (10, 10)) {
2592 using (Graphics g = Graphics.FromImage (b)) {
2593 IntPtr hdc = g.GetHdc ();
2594 g.ReleaseHdcInternal (hdc);
2595 Assert.Throws<ArgumentException> (() => g.ReleaseHdcInternal (hdc));
2600 public void TestReleaseHdc ()
2602 using (Bitmap b = new Bitmap (10, 10)) {
2603 using (Graphics g = Graphics.FromImage (b)) {
2604 IntPtr hdc1 = g.GetHdc ();
2606 IntPtr hdc2 = g.GetHdc ();
2608 Assert.AreEqual (hdc1, hdc2, "hdc");
2614 public void TestReleaseHdcException ()
2616 using (Bitmap b = new Bitmap (10, 10)) {
2617 using (Graphics g = Graphics.FromImage (b)) {
2618 Assert.Throws<ArgumentException> (() => g.ReleaseHdc ());
2624 public void TestReleaseHdcException2 ()
2626 using (Bitmap b = new Bitmap (10, 10)) {
2627 using (Graphics g = Graphics.FromImage (b)) {
2630 Assert.Throws<ArgumentException> (() => g.ReleaseHdc ());
2635 public void VisibleClipBound ()
2638 using (Bitmap bmp = new Bitmap (100, 100)) {
2639 using (Graphics g = Graphics.FromImage (bmp)) {
2640 RectangleF noclip = g.VisibleClipBounds;
2641 Assert.AreEqual (0, noclip.X, "noclip.X");
2642 Assert.AreEqual (0, noclip.Y, "noclip.Y");
2643 Assert.AreEqual (100, noclip.Width, "noclip.Width");
2644 Assert.AreEqual (100, noclip.Height, "noclip.Height");
2646 // note: libgdiplus regions are precise to multiple of multiple of 8
2647 g.Clip = new Region (new RectangleF (0, 0, 32, 32));
2648 RectangleF clip = g.VisibleClipBounds;
2649 Assert.AreEqual (0, clip.X, "clip.X");
2650 Assert.AreEqual (0, clip.Y, "clip.Y");
2651 Assert.AreEqual (32, clip.Width, "clip.Width");
2652 Assert.AreEqual (32, clip.Height, "clip.Height");
2654 g.RotateTransform (90);
2655 RectangleF rotclip = g.VisibleClipBounds;
2656 Assert.AreEqual (0, rotclip.X, "rotclip.X");
2657 Assert.AreEqual (-32, rotclip.Y, "rotclip.Y");
2658 Assert.AreEqual (32, rotclip.Width, "rotclip.Width");
2659 Assert.AreEqual (32, rotclip.Height, "rotclip.Height");
2665 public void VisibleClipBound_BigClip ()
2667 using (Bitmap bmp = new Bitmap (100, 100)) {
2668 using (Graphics g = Graphics.FromImage (bmp)) {
2669 RectangleF noclip = g.VisibleClipBounds;
2670 Assert.AreEqual (0, noclip.X, "noclip.X");
2671 Assert.AreEqual (0, noclip.Y, "noclip.Y");
2672 Assert.AreEqual (100, noclip.Width, "noclip.Width");
2673 Assert.AreEqual (100, noclip.Height, "noclip.Height");
2675 // clip is larger than bitmap
2676 g.Clip = new Region (new RectangleF (0, 0, 200, 200));
2677 RectangleF clipbound = g.ClipBounds;
2678 Assert.AreEqual (0, clipbound.X, "clipbound.X");
2679 Assert.AreEqual (0, clipbound.Y, "clipbound.Y");
2680 Assert.AreEqual (200, clipbound.Width, "clipbound.Width");
2681 Assert.AreEqual (200, clipbound.Height, "clipbound.Height");
2683 RectangleF clip = g.VisibleClipBounds;
2684 Assert.AreEqual (0, clip.X, "clip.X");
2685 Assert.AreEqual (0, clip.Y, "clip.Y");
2686 Assert.AreEqual (100, clip.Width, "clip.Width");
2687 Assert.AreEqual (100, clip.Height, "clip.Height");
2689 g.RotateTransform (90);
2690 RectangleF rotclipbound = g.ClipBounds;
2691 Assert.AreEqual (0, rotclipbound.X, "rotclipbound.X");
2692 Assert.AreEqual (-200, rotclipbound.Y, "rotclipbound.Y");
2693 Assert.AreEqual (200, rotclipbound.Width, "rotclipbound.Width");
2694 Assert.AreEqual (200, rotclipbound.Height, "rotclipbound.Height");
2696 RectangleF rotclip = g.VisibleClipBounds;
2697 Assert.AreEqual (0, rotclip.X, "rotclip.X");
2698 Assert.AreEqual (-100, rotclip.Y, "rotclip.Y");
2699 Assert.AreEqual (100, rotclip.Width, "rotclip.Width");
2700 Assert.AreEqual (100, rotclip.Height, "rotclip.Height");
2706 public void Rotate ()
2708 using (Bitmap bmp = new Bitmap (100, 50)) {
2709 using (Graphics g = Graphics.FromImage (bmp)) {
2710 RectangleF vcb = g.VisibleClipBounds;
2711 Assert.AreEqual (0, vcb.X, "vcb.X");
2712 Assert.AreEqual (0, vcb.Y, "vcb.Y");
2713 Assert.AreEqual (100, vcb.Width, "vcb.Width");
2714 Assert.AreEqual (50, vcb.Height, "vcb.Height");
2716 g.RotateTransform (90);
2717 RectangleF rvcb = g.VisibleClipBounds;
2718 Assert.AreEqual (0, rvcb.X, "rvcb.X");
2719 Assert.AreEqual (-100, rvcb.Y, "rvcb.Y");
2720 Assert.AreEqual (50.0f, rvcb.Width, 0.0001, "rvcb.Width");
2721 Assert.AreEqual (100, rvcb.Height, "rvcb.Height");
2727 public void Scale ()
2729 using (Bitmap bmp = new Bitmap (100, 50)) {
2730 using (Graphics g = Graphics.FromImage (bmp)) {
2731 RectangleF vcb = g.VisibleClipBounds;
2732 Assert.AreEqual (0, vcb.X, "vcb.X");
2733 Assert.AreEqual (0, vcb.Y, "vcb.Y");
2734 Assert.AreEqual (100, vcb.Width, "vcb.Width");
2735 Assert.AreEqual (50, vcb.Height, "vcb.Height");
2737 g.ScaleTransform (2, 0.5f);
2738 RectangleF svcb = g.VisibleClipBounds;
2739 Assert.AreEqual (0, svcb.X, "svcb.X");
2740 Assert.AreEqual (0, svcb.Y, "svcb.Y");
2741 Assert.AreEqual (50, svcb.Width, "svcb.Width");
2742 Assert.AreEqual (100, svcb.Height, "svcb.Height");
2748 public void Translate ()
2750 using (Bitmap bmp = new Bitmap (100, 50)) {
2751 using (Graphics g = Graphics.FromImage (bmp)) {
2752 RectangleF vcb = g.VisibleClipBounds;
2753 Assert.AreEqual (0, vcb.X, "vcb.X");
2754 Assert.AreEqual (0, vcb.Y, "vcb.Y");
2755 Assert.AreEqual (100, vcb.Width, "vcb.Width");
2756 Assert.AreEqual (50, vcb.Height, "vcb.Height");
2758 g.TranslateTransform (-25, 25);
2759 RectangleF tvcb = g.VisibleClipBounds;
2760 Assert.AreEqual (25, tvcb.X, "tvcb.X");
2761 Assert.AreEqual (-25, tvcb.Y, "tvcb.Y");
2762 Assert.AreEqual (100, tvcb.Width, "tvcb.Width");
2763 Assert.AreEqual (50, tvcb.Height, "tvcb.Height");
2769 public void DrawIcon_NullRectangle ()
2771 using (Bitmap bmp = new Bitmap (40, 40)) {
2772 using (Graphics g = Graphics.FromImage (bmp)) {
2773 Assert.Throws<ArgumentNullException> (() => g.DrawIcon (null, new Rectangle (0, 0, 32, 32)));
2779 public void DrawIcon_IconRectangle ()
2781 using (Bitmap bmp = new Bitmap (40, 40)) {
2782 using (Graphics g = Graphics.FromImage (bmp)) {
2783 g.DrawIcon (SystemIcons.Application, new Rectangle (0, 0, 40, 20));
2784 // Rectangle is empty when X, Y, Width and Height == 0
2785 // (yep X and Y too, RectangleF only checks for Width and Height)
2786 g.DrawIcon (SystemIcons.Asterisk, new Rectangle (0, 0, 0, 0));
2787 // so this one is half-empty ;-)
2788 g.DrawIcon (SystemIcons.Error, new Rectangle (20, 40, 0, 0));
2789 // negative width or height isn't empty (for Rectangle)
2790 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (10, 20, -1, 0));
2791 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (20, 10, 0, -1));
2797 public void DrawIcon_NullIntInt ()
2799 using (Bitmap bmp = new Bitmap (40, 40)) {
2800 using (Graphics g = Graphics.FromImage (bmp)) {
2801 Assert.Throws<ArgumentNullException> (() => g.DrawIcon (null, 4, 2));
2807 public void DrawIcon_IconIntInt ()
2809 using (Bitmap bmp = new Bitmap (40, 40)) {
2810 using (Graphics g = Graphics.FromImage (bmp)) {
2811 g.DrawIcon (SystemIcons.Exclamation, 4, 2);
2812 g.DrawIcon (SystemIcons.Hand, 0, 0);
2818 public void DrawIconUnstretched_NullRectangle ()
2820 using (Bitmap bmp = new Bitmap (40, 40)) {
2821 using (Graphics g = Graphics.FromImage (bmp)) {
2822 Assert.Throws<ArgumentNullException> (() => g.DrawIconUnstretched (null, new Rectangle (0, 0, 40, 20)));
2828 public void DrawIconUnstretched_IconRectangle ()
2830 using (Bitmap bmp = new Bitmap (40, 40)) {
2831 using (Graphics g = Graphics.FromImage (bmp)) {
2832 g.DrawIconUnstretched (SystemIcons.Information, new Rectangle (0, 0, 40, 20));
2833 // Rectangle is empty when X, Y, Width and Height == 0
2834 // (yep X and Y too, RectangleF only checks for Width and Height)
2835 g.DrawIconUnstretched (SystemIcons.Question, new Rectangle (0, 0, 0, 0));
2836 // so this one is half-empty ;-)
2837 g.DrawIconUnstretched (SystemIcons.Warning, new Rectangle (20, 40, 0, 0));
2838 // negative width or height isn't empty (for Rectangle)
2839 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (10, 20, -1, 0));
2840 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (20, 10, 0, -1));
2846 public void DrawImage_NullRectangleF ()
2848 using (Bitmap bmp = new Bitmap (40, 40)) {
2849 using (Graphics g = Graphics.FromImage (bmp)) {
2850 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new RectangleF (0, 0, 0, 0)));
2856 public void DrawImage_ImageRectangleF ()
2858 using (Bitmap bmp = new Bitmap (40, 40)) {
2859 using (Graphics g = Graphics.FromImage (bmp)) {
2860 g.DrawImage (bmp, new RectangleF (0, 0, 0, 0));
2861 g.DrawImage (bmp, new RectangleF (20, 40, 0, 0));
2862 g.DrawImage (bmp, new RectangleF (10, 20, -1, 0));
2863 g.DrawImage (bmp, new RectangleF (20, 10, 0, -1));
2869 public void DrawImage_NullPointF ()
2871 using (Bitmap bmp = new Bitmap (40, 40)) {
2872 using (Graphics g = Graphics.FromImage (bmp)) {
2873 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new PointF (0, 0)));
2879 public void DrawImage_ImagePointF ()
2881 using (Bitmap bmp = new Bitmap (40, 40)) {
2882 using (Graphics g = Graphics.FromImage (bmp)) {
2883 g.DrawImage (bmp, new PointF (0, 0));
2889 public void DrawImage_NullPointFArray ()
2891 using (Bitmap bmp = new Bitmap (40, 40)) {
2892 using (Graphics g = Graphics.FromImage (bmp)) {
2893 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new PointF[0]));
2899 public void DrawImage_ImagePointFArrayNull ()
2901 using (Bitmap bmp = new Bitmap (40, 40)) {
2902 using (Graphics g = Graphics.FromImage (bmp)) {
2903 Assert.Throws<ArgumentNullException> (() => g.DrawImage (bmp, (PointF[]) null));
2909 public void DrawImage_ImagePointFArrayEmpty ()
2911 using (Bitmap bmp = new Bitmap (40, 40)) {
2912 using (Graphics g = Graphics.FromImage (bmp)) {
2913 Assert.Throws<ArgumentException> (() => g.DrawImage (bmp, new PointF[0]));
2919 public void DrawImage_ImagePointFArray ()
2921 using (Bitmap bmp = new Bitmap (40, 40)) {
2922 using (Graphics g = Graphics.FromImage (bmp)) {
2923 g.DrawImage (bmp, new PointF[] {
2924 new PointF (0, 0), new PointF (1, 1), new PointF (2, 2) });
2930 public void DrawImage_NullRectangle ()
2932 using (Bitmap bmp = new Bitmap (40, 40)) {
2933 using (Graphics g = Graphics.FromImage (bmp)) {
2934 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Rectangle (0, 0, 0, 0)));
2940 public void DrawImage_ImageRectangle ()
2942 using (Bitmap bmp = new Bitmap (40, 40)) {
2943 using (Graphics g = Graphics.FromImage (bmp)) {
2944 // Rectangle is empty when X, Y, Width and Height == 0
2945 // (yep X and Y too, RectangleF only checks for Width and Height)
2946 g.DrawImage (bmp, new Rectangle (0, 0, 0, 0));
2947 // so this one is half-empty ;-)
2948 g.DrawImage (bmp, new Rectangle (20, 40, 0, 0));
2949 // negative width or height isn't empty (for Rectangle)
2950 g.DrawImage (bmp, new Rectangle (10, 20, -1, 0));
2951 g.DrawImage (bmp, new Rectangle (20, 10, 0, -1));
2957 public void DrawImage_NullPoint ()
2959 using (Bitmap bmp = new Bitmap (40, 40)) {
2960 using (Graphics g = Graphics.FromImage (bmp)) {
2961 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Point (0, 0)));
2967 public void DrawImage_ImagePoint ()
2969 using (Bitmap bmp = new Bitmap (40, 40)) {
2970 using (Graphics g = Graphics.FromImage (bmp)) {
2971 g.DrawImage (bmp, new Point (0, 0));
2977 public void DrawImage_NullPointArray ()
2979 using (Bitmap bmp = new Bitmap (40, 40)) {
2980 using (Graphics g = Graphics.FromImage (bmp)) {
2981 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Point[0]));
2987 public void DrawImage_ImagePointArrayNull ()
2989 using (Bitmap bmp = new Bitmap (40, 40)) {
2990 using (Graphics g = Graphics.FromImage (bmp)) {
2991 Assert.Throws<ArgumentNullException> (() => g.DrawImage (bmp, (Point[]) null));
2997 public void DrawImage_ImagePointArrayEmpty ()
2999 using (Bitmap bmp = new Bitmap (40, 40)) {
3000 using (Graphics g = Graphics.FromImage (bmp)) {
3001 Assert.Throws<ArgumentException> (() => g.DrawImage (bmp, new Point[0]));
3007 public void DrawImage_ImagePointArray ()
3009 using (Bitmap bmp = new Bitmap (40, 40)) {
3010 using (Graphics g = Graphics.FromImage (bmp)) {
3011 g.DrawImage (bmp, new Point[] {
3012 new Point (0, 0), new Point (1, 1), new Point (2, 2) });
3018 public void DrawImage_NullIntInt ()
3020 using (Bitmap bmp = new Bitmap (40, 40)) {
3021 using (Graphics g = Graphics.FromImage (bmp)) {
3022 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, Int32.MaxValue, Int32.MinValue));
3028 public void DrawImage_ImageIntInt_Overflow ()
3030 using (Bitmap bmp = new Bitmap (40, 40)) {
3031 using (Graphics g = Graphics.FromImage (bmp)) {
3032 Assert.Throws<OverflowException> (() => g.DrawImage (bmp, Int32.MaxValue, Int32.MinValue));
3038 public void DrawImage_ImageIntInt ()
3040 using (Bitmap bmp = new Bitmap (40, 40)) {
3041 using (Graphics g = Graphics.FromImage (bmp)) {
3042 g.DrawImage (bmp, -40, -40);
3048 public void DrawImage_NullFloat ()
3050 using (Bitmap bmp = new Bitmap (40, 40)) {
3051 using (Graphics g = Graphics.FromImage (bmp)) {
3052 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, Single.MaxValue, Single.MinValue));
3058 public void DrawImage_ImageFloatFloat_Overflow ()
3060 using (Bitmap bmp = new Bitmap (40, 40)) {
3061 using (Graphics g = Graphics.FromImage (bmp)) {
3062 Assert.Throws<OverflowException> (() => g.DrawImage (bmp, Single.MaxValue, Single.MinValue));
3068 public void DrawImage_ImageFloatFloat ()
3070 using (Bitmap bmp = new Bitmap (40, 40)) {
3071 using (Graphics g = Graphics.FromImage (bmp)) {
3072 g.DrawImage (bmp, -40.0f, -40.0f);
3078 public void DrawImage_NullRectangleRectangleGraphicsUnit ()
3080 using (Bitmap bmp = new Bitmap (40, 40)) {
3081 using (Graphics g = Graphics.FromImage (bmp)) {
3082 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Rectangle (), new Rectangle (), GraphicsUnit.Display));
3087 private void DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit unit)
3089 using (Bitmap bmp = new Bitmap (40, 40)) {
3090 using (Graphics g = Graphics.FromImage (bmp)) {
3091 Rectangle r = new Rectangle (0, 0, 40, 40);
3092 g.DrawImage (bmp, r, r, unit);
3098 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Display ()
3100 Assert.Throws<ArgumentException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Display));
3104 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Document ()
3106 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Document));
3110 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Inch ()
3112 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit(GraphicsUnit.Inch));
3116 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Millimeter ()
3118 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Millimeter));
3122 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Pixel ()
3125 DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Pixel);
3129 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Point ()
3131 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Point));
3135 public void DrawImage_ImageRectangleRectangleGraphicsUnit_World ()
3137 Assert.Throws<ArgumentException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.World));
3141 public void DrawImage_NullPointRectangleGraphicsUnit ()
3143 Rectangle r = new Rectangle (1, 2, 3, 4);
3144 Point[] pts = new Point[3] { new Point (1, 1), new Point (2, 2), new Point (3, 3) };
3145 using (Bitmap bmp = new Bitmap (40, 40)) {
3146 using (Graphics g = Graphics.FromImage (bmp)) {
3147 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, pts, r, GraphicsUnit.Pixel));
3152 private void DrawImage_ImagePointRectangleGraphicsUnit (Point[] pts)
3154 Rectangle r = new Rectangle (1, 2, 3, 4);
3155 using (Bitmap bmp = new Bitmap (40, 40)) {
3156 using (Graphics g = Graphics.FromImage (bmp)) {
3157 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel);
3163 public void DrawImage_ImageNullRectangleGraphicsUnit ()
3165 Assert.Throws<ArgumentNullException> (() => DrawImage_ImagePointRectangleGraphicsUnit (null));
3169 public void DrawImage_ImagePoint0RectangleGraphicsUnit ()
3171 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[0]));
3175 public void DrawImage_ImagePoint1RectangleGraphicsUnit ()
3177 Point p = new Point (1, 1);
3178 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[1] { p }));
3182 public void DrawImage_ImagePoint2RectangleGraphicsUnit ()
3184 Point p = new Point (1, 1);
3185 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[2] { p, p }));
3189 public void DrawImage_ImagePoint3RectangleGraphicsUnit ()
3191 Point p = new Point (1, 1);
3192 DrawImage_ImagePointRectangleGraphicsUnit (new Point[3] { p, p, p });
3196 public void DrawImage_ImagePoint4RectangleGraphicsUnit ()
3198 Point p = new Point (1, 1);
3199 Assert.Throws<NotImplementedException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[4] { p, p, p, p }));
3203 public void DrawImage_NullPointFRectangleGraphicsUnit ()
3205 Rectangle r = new Rectangle (1, 2, 3, 4);
3206 PointF[] pts = new PointF[3] { new PointF (1, 1), new PointF (2, 2), new PointF (3, 3) };
3207 using (Bitmap bmp = new Bitmap (40, 40)) {
3208 using (Graphics g = Graphics.FromImage (bmp)) {
3209 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, pts, r, GraphicsUnit.Pixel));
3214 private void DrawImage_ImagePointFRectangleGraphicsUnit (PointF[] pts)
3216 Rectangle r = new Rectangle (1, 2, 3, 4);
3217 using (Bitmap bmp = new Bitmap (40, 40)) {
3218 using (Graphics g = Graphics.FromImage (bmp)) {
3219 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel);
3225 public void DrawImage_ImageNullFRectangleGraphicsUnit ()
3227 Assert.Throws<ArgumentNullException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (null));
3231 public void DrawImage_ImagePointF0RectangleGraphicsUnit ()
3233 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[0]));
3237 public void DrawImage_ImagePointF1RectangleGraphicsUnit ()
3239 PointF p = new PointF (1, 1);
3240 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[1] { p }));
3244 public void DrawImage_ImagePointF2RectangleGraphicsUnit ()
3246 PointF p = new PointF (1, 1);
3247 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[2] { p, p }));
3251 public void DrawImage_ImagePointF3RectangleGraphicsUnit ()
3253 PointF p = new PointF (1, 1);
3254 DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[3] { p, p, p });
3258 public void DrawImage_ImagePointF4RectangleGraphicsUnit ()
3260 PointF p = new PointF (1, 1);
3261 Assert.Throws<NotImplementedException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[4] { p, p, p, p }));
3265 public void DrawImage_ImagePointRectangleGraphicsUnitNull ()
3267 Point p = new Point (1, 1);
3268 Point[] pts = new Point[3] { p, p, p };
3269 Rectangle r = new Rectangle (1, 2, 3, 4);
3270 using (Bitmap bmp = new Bitmap (40, 40)) {
3271 using (Graphics g = Graphics.FromImage (bmp)) {
3272 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel, null);
3278 public void DrawImage_ImagePointRectangleGraphicsUnitAttributes ()
3280 Point p = new Point (1, 1);
3281 Point[] pts = new Point[3] { p, p, p };
3282 Rectangle r = new Rectangle (1, 2, 3, 4);
3283 using (Bitmap bmp = new Bitmap (40, 40)) {
3284 using (Graphics g = Graphics.FromImage (bmp)) {
3285 ImageAttributes ia = new ImageAttributes ();
3286 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel, ia);
3292 public void DrawImageUnscaled_NullPoint ()
3294 using (Bitmap bmp = new Bitmap (40, 40)) {
3295 using (Graphics g = Graphics.FromImage (bmp)) {
3296 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, new Point (0, 0)));
3302 public void DrawImageUnscaled_ImagePoint ()
3304 using (Bitmap bmp = new Bitmap (40, 40)) {
3305 using (Graphics g = Graphics.FromImage (bmp)) {
3306 g.DrawImageUnscaled (bmp, new Point (0, 0));
3312 public void DrawImageUnscaled_NullRectangle ()
3314 using (Bitmap bmp = new Bitmap (40, 40)) {
3315 using (Graphics g = Graphics.FromImage (bmp)) {
3316 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, new Rectangle (0, 0, -1, -1)));
3322 public void DrawImageUnscaled_ImageRectangle ()
3324 using (Bitmap bmp = new Bitmap (40, 40)) {
3325 using (Graphics g = Graphics.FromImage (bmp)) {
3326 g.DrawImageUnscaled (bmp, new Rectangle (0, 0, -1, -1));
3332 public void DrawImageUnscaled_NullIntInt ()
3334 using (Bitmap bmp = new Bitmap (40, 40)) {
3335 using (Graphics g = Graphics.FromImage (bmp)) {
3336 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, 0, 0));
3342 public void DrawImageUnscaled_ImageIntInt ()
3344 using (Bitmap bmp = new Bitmap (40, 40)) {
3345 using (Graphics g = Graphics.FromImage (bmp)) {
3346 g.DrawImageUnscaled (bmp, 0, 0);
3352 public void DrawImageUnscaled_NullIntIntIntInt ()
3354 using (Bitmap bmp = new Bitmap (40, 40)) {
3355 using (Graphics g = Graphics.FromImage (bmp)) {
3356 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, 0, 0, -1, -1));
3362 public void DrawImageUnscaled_ImageIntIntIntInt ()
3364 using (Bitmap bmp = new Bitmap (40, 40)) {
3365 using (Graphics g = Graphics.FromImage (bmp)) {
3366 g.DrawImageUnscaled (bmp, 0, 0, -1, -1);
3371 public void DrawImageUnscaledAndClipped_Null ()
3373 using (Bitmap bmp = new Bitmap (40, 40)) {
3374 using (Graphics g = Graphics.FromImage (bmp)) {
3375 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaledAndClipped (null, new Rectangle (0, 0, 0, 0)));
3381 public void DrawImageUnscaledAndClipped ()
3383 using (Bitmap bmp = new Bitmap (40, 40)) {
3384 using (Graphics g = Graphics.FromImage (bmp)) {
3385 // Rectangle is empty when X, Y, Width and Height == 0
3386 // (yep X and Y too, RectangleF only checks for Width and Height)
3387 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 0, 0));
3388 // so this one is half-empty ;-)
3389 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (20, 40, 0, 0));
3390 // negative width or height isn't empty (for Rectangle)
3391 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (10, 20, -1, 0));
3392 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (20, 10, 0, -1));
3394 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 10, 20));
3395 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 40, 10));
3396 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 80, 20));
3402 public void DrawPath_Pen_Null ()
3404 using (Bitmap bmp = new Bitmap (20, 20)) {
3405 using (Graphics g = Graphics.FromImage (bmp)) {
3406 using (GraphicsPath path = new GraphicsPath ()) {
3407 Assert.Throws<ArgumentNullException> (() => g.DrawPath (null, path));
3414 public void DrawPath_Path_Null ()
3416 using (Bitmap bmp = new Bitmap (20, 20)) {
3417 using (Graphics g = Graphics.FromImage (bmp)) {
3418 Assert.Throws<ArgumentNullException> (() => g.DrawPath (Pens.Black, null));
3424 public void DrawPath_82202 ()
3426 // based on test case from bug #82202
3427 using (Bitmap bmp = new Bitmap (20, 20)) {
3428 using (Graphics g = Graphics.FromImage (bmp)) {
3429 using (GraphicsPath path = new GraphicsPath ()) {
3431 Rectangle baserect = new Rectangle (0, 0, 19, 19);
3432 Rectangle arcrect = new Rectangle (baserect.Location, new Size (d, d));
3434 path.AddArc (arcrect, 180, 90);
3435 arcrect.X = baserect.Right - d;
3436 path.AddArc (arcrect, 270, 90);
3437 arcrect.Y = baserect.Bottom - d;
3438 path.AddArc (arcrect, 0, 90);
3439 arcrect.X = baserect.Left;
3440 path.AddArc (arcrect, 90, 90);
3441 path.CloseFigure ();
3442 g.Clear (Color.White);
3443 g.DrawPath (Pens.SteelBlue, path);
3445 Assert.AreEqual (-12156236, bmp.GetPixel (0, 9).ToArgb (), "0,9");
3446 Assert.AreEqual (-1, bmp.GetPixel (1, 9).ToArgb (), "1,9");
3453 public void FillPath_Brush_Null ()
3455 using (Bitmap bmp = new Bitmap (20, 20)) {
3456 using (Graphics g = Graphics.FromImage (bmp)) {
3457 using (GraphicsPath path = new GraphicsPath ()) {
3458 Assert.Throws<ArgumentNullException> (() => g.FillPath (null, path));
3465 public void FillPath_Path_Null ()
3467 using (Bitmap bmp = new Bitmap (20, 20)) {
3468 using (Graphics g = Graphics.FromImage (bmp)) {
3469 Assert.Throws<ArgumentNullException> (() => g.FillPath (Brushes.Black, null));
3475 public void FillPath_82202 ()
3477 // based on test case from bug #82202
3478 using (Bitmap bmp = new Bitmap (20, 20)) {
3479 using (Graphics g = Graphics.FromImage (bmp)) {
3480 using (GraphicsPath path = new GraphicsPath ()) {
3482 Rectangle baserect = new Rectangle (0, 0, 19, 19);
3483 Rectangle arcrect = new Rectangle (baserect.Location, new Size (d, d));
3485 path.AddArc (arcrect, 180, 90);
3486 arcrect.X = baserect.Right - d;
3487 path.AddArc (arcrect, 270, 90);
3488 arcrect.Y = baserect.Bottom - d;
3489 path.AddArc (arcrect, 0, 90);
3490 arcrect.X = baserect.Left;
3491 path.AddArc (arcrect, 90, 90);
3492 path.CloseFigure ();
3493 g.Clear (Color.White);
3494 g.FillPath (Brushes.SteelBlue, path);
3496 Assert.AreEqual (-12156236, bmp.GetPixel (0, 9).ToArgb (), "0,9");
3497 Assert.AreEqual (-12156236, bmp.GetPixel (1, 9).ToArgb (), "1,9");
3504 public void TransformPoints_349800 ()
3506 using (Bitmap bmp = new Bitmap (10, 10)) {
3507 using (Graphics g = Graphics.FromImage (bmp)) {
3508 Point [] pts = new Point [5];
3509 PointF [] ptf = new PointF [5];
3510 for (int i = 0; i < 5; i++) {
3511 pts [i] = new Point (i, i);
3512 ptf [i] = new PointF (i, i);
3515 g.TransformPoints (CoordinateSpace.Page, CoordinateSpace.Device, pts);
3516 g.TransformPoints (CoordinateSpace.Page, CoordinateSpace.Device, ptf);
3518 for (int i = 0; i < 5; i++) {
3519 Assert.AreEqual (i, pts [i].X, "Point.X " + i.ToString ());
3520 Assert.AreEqual (i, pts [i].Y, "Point.Y " + i.ToString ());
3521 Assert.AreEqual (i, ptf [i].X, "PointF.X " + i.ToString ());
3522 Assert.AreEqual (i, ptf [i].Y, "PointF.Y " + i.ToString ());
3529 public void Dpi_556181 ()
3532 using (Bitmap bmp = new Bitmap (10, 10)) {
3533 using (Graphics g = Graphics.FromImage (bmp)) {
3537 bmp.SetResolution (x, y);
3538 using (Graphics g = Graphics.FromImage (bmp)) {
3539 Assert.AreEqual (x, g.DpiX, "DpiX");
3540 Assert.AreEqual (y, g.DpiY, "DpiY");
3547 public class GraphicsFullTrustTest {
3549 // note: this test would fail, on ReleaseHdc, without fulltrust
3550 // i.e. it's a demand and not a linkdemand
3552 public void GetReleaseHdc ()
3554 using (Bitmap b = new Bitmap (100, 100)) {
3555 using (Graphics g = Graphics.FromImage (b)) {
3556 IntPtr hdc1 = g.GetHdc ();
3557 g.ReleaseHdc (hdc1);
3558 IntPtr hdc2 = g.GetHdc ();
3559 g.ReleaseHdc (hdc2);
3560 Assert.AreEqual (hdc1, hdc2, "hdc");