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 [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
44 public class GraphicsTest {
46 private RectangleF[] rects;
50 public void FixtureSetUp ()
53 font = new Font ("Arial", 12);
60 public void FixtureTearDown ()
67 private bool IsEmptyBitmap (Bitmap bitmap, out int x, out int y)
70 int empty = Color.Empty.ToArgb ();
72 for (y = 0; y < bitmap.Height; y++) {
73 for (x = 0; x < bitmap.Width; x++) {
74 if (bitmap.GetPixel (x, y).ToArgb () != empty) {
83 for (y = 0; y < bitmap.Height; y++) {
84 for (x = 0; x < bitmap.Width; x++) {
85 if (bitmap.GetPixel (x, y).ToArgb () != empty)
95 private void CheckForEmptyBitmap (Bitmap bitmap)
98 if (!IsEmptyBitmap (bitmap, out x, out y))
99 Assert.Fail (String.Format ("Position {0},{1}", x, y));
102 private void CheckForNonEmptyBitmap (Bitmap bitmap)
105 if (IsEmptyBitmap (bitmap, out x, out y))
106 Assert.Fail ("Bitmap was empty");
109 private void AssertEquals (string msg, object expected, object actual)
111 Assert.AreEqual (expected, actual, msg);
114 private void AssertEquals (string msg, double expected, double actual, double delta)
116 Assert.AreEqual (expected, actual, delta, msg);
120 public void DefaultProperties ()
122 Bitmap bmp = new Bitmap (200, 200);
123 Graphics g = Graphics.FromImage (bmp);
124 Region r = new Region ();
126 Assert.AreEqual (r.GetBounds (g), g.ClipBounds, "DefaultProperties1");
127 Assert.AreEqual (CompositingMode.SourceOver, g.CompositingMode, "DefaultProperties2");
128 Assert.AreEqual (CompositingQuality.Default, g.CompositingQuality, "DefaultProperties3");
129 Assert.AreEqual (InterpolationMode.Bilinear, g.InterpolationMode, "DefaultProperties4");
130 Assert.AreEqual (1, g.PageScale, "DefaultProperties5");
131 Assert.AreEqual (GraphicsUnit.Display, g.PageUnit, "DefaultProperties6");
132 Assert.AreEqual (PixelOffsetMode.Default, g.PixelOffsetMode, "DefaultProperties7");
133 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "DefaultProperties8");
134 Assert.AreEqual (SmoothingMode.None, g.SmoothingMode, "DefaultProperties9");
135 Assert.AreEqual (TextRenderingHint.SystemDefault, g.TextRenderingHint, "DefaultProperties10");
141 public void SetGetProperties ()
143 Bitmap bmp = new Bitmap (200, 200);
144 Graphics g = Graphics.FromImage (bmp);
146 g.CompositingMode = CompositingMode.SourceCopy;
147 g.CompositingQuality = CompositingQuality.GammaCorrected;
148 g.InterpolationMode = InterpolationMode.HighQualityBilinear;
150 g.PageUnit = GraphicsUnit.Inch;
151 g.PixelOffsetMode = PixelOffsetMode.Half;
152 g.RenderingOrigin = new Point (10, 20);
153 g.SmoothingMode = SmoothingMode.AntiAlias;
154 g.TextRenderingHint = TextRenderingHint.SystemDefault;
156 //Clipping set/get tested in clipping functions
157 Assert.AreEqual (CompositingMode.SourceCopy, g.CompositingMode, "SetGetProperties2");
158 Assert.AreEqual (CompositingQuality.GammaCorrected, g.CompositingQuality, "SetGetProperties3");
159 Assert.AreEqual (InterpolationMode.HighQualityBilinear, g.InterpolationMode, "SetGetProperties4");
160 Assert.AreEqual (2, g.PageScale, "SetGetProperties5");
161 Assert.AreEqual (GraphicsUnit.Inch, g.PageUnit, "SetGetProperties6");
162 Assert.AreEqual (PixelOffsetMode.Half, g.PixelOffsetMode, "SetGetProperties7");
163 Assert.AreEqual (new Point (10, 20), g.RenderingOrigin, "SetGetProperties8");
164 Assert.AreEqual (SmoothingMode.AntiAlias, g.SmoothingMode, "SetGetProperties9");
165 Assert.AreEqual (TextRenderingHint.SystemDefault, g.TextRenderingHint, "SetGetProperties10");
173 Bitmap bmp = new Bitmap (200, 200);
174 Graphics g = Graphics.FromImage (bmp);
175 g.Clip = new Region (new Rectangle (50, 40, 210, 220));
176 rects = g.Clip.GetRegionScans (new Matrix ());
178 Assert.AreEqual (1, rects.Length, "Clip1");
179 Assert.AreEqual (50, rects[0].X, "Clip2");
180 Assert.AreEqual (40, rects[0].Y, "Clip3");
181 Assert.AreEqual (210, rects[0].Width, "Clip4");
182 Assert.AreEqual (220, rects[0].Height, "Clip5");
186 public void Clip_NotAReference ()
188 Bitmap bmp = new Bitmap (200, 200);
189 Graphics g = Graphics.FromImage (bmp);
190 Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite");
192 Assert.IsFalse (g.Clip.IsEmpty (g), "!IsEmpty");
193 Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite-2");
197 public void ExcludeClip ()
199 Bitmap bmp = new Bitmap (200, 200);
200 Graphics g = Graphics.FromImage (bmp);
202 g.Clip = new Region (new RectangleF (10, 10, 100, 100));
203 g.ExcludeClip (new Rectangle (40, 60, 100, 20));
204 rects = g.Clip.GetRegionScans (new Matrix ());
206 Assert.AreEqual (3, rects.Length, "ExcludeClip1");
208 Assert.AreEqual (10, rects[0].X, "ExcludeClip2");
209 Assert.AreEqual (10, rects[0].Y, "ExcludeClip3");
210 Assert.AreEqual (100, rects[0].Width, "ExcludeClip4");
211 Assert.AreEqual (50, rects[0].Height, "ExcludeClip5");
213 Assert.AreEqual (10, rects[1].X, "ExcludeClip6");
214 Assert.AreEqual (60, rects[1].Y, "ExcludeClip7");
215 Assert.AreEqual (30, rects[1].Width, "ExcludeClip8");
216 Assert.AreEqual (20, rects[1].Height, "ExcludeClip9");
218 Assert.AreEqual (10, rects[2].X, "ExcludeClip10");
219 Assert.AreEqual (80, rects[2].Y, "ExcludeClip11");
220 Assert.AreEqual (100, rects[2].Width, "ExcludeClip12");
221 Assert.AreEqual (30, rects[2].Height, "ExcludeClip13");
225 public void IntersectClip ()
227 Bitmap bmp = new Bitmap (200, 200);
228 Graphics g = Graphics.FromImage (bmp);
230 g.Clip = new Region (new RectangleF (260, 30, 60, 80));
231 g.IntersectClip (new Rectangle (290, 40, 60, 80));
232 rects = g.Clip.GetRegionScans (new Matrix ());
234 Assert.AreEqual (1, rects.Length, "IntersectClip");
236 Assert.AreEqual (290, rects[0].X, "IntersectClip");
237 Assert.AreEqual (40, rects[0].Y, "IntersectClip");
238 Assert.AreEqual (30, rects[0].Width, "IntersectClip");
239 Assert.AreEqual (70, rects[0].Height, "IntersectClip");
243 public void ResetClip ()
245 Bitmap bmp = new Bitmap (200, 200);
246 Graphics g = Graphics.FromImage (bmp);
248 g.Clip = new Region (new RectangleF (260, 30, 60, 80));
249 g.IntersectClip (new Rectangle (290, 40, 60, 80));
251 rects = g.Clip.GetRegionScans (new Matrix ());
253 Assert.AreEqual (1, rects.Length, "ResetClip");
255 Assert.AreEqual (-4194304, rects[0].X, "ResetClip");
256 Assert.AreEqual (-4194304, rects[0].Y, "ResetClip");
257 Assert.AreEqual (8388608, rects[0].Width, "ResetClip");
258 Assert.AreEqual (8388608, rects[0].Height, "ResetClip");
262 public void SetClip ()
265 Bitmap bmp = new Bitmap (200, 200);
266 Graphics g = Graphics.FromImage (bmp);
269 g.SetClip (new Region (new Rectangle (50, 40, 210, 220)), CombineMode.Replace);
270 rects = g.Clip.GetRegionScans (new Matrix ());
271 Assert.AreEqual (1, rects.Length, "SetClip1");
272 Assert.AreEqual (50, rects[0].X, "SetClip2");
273 Assert.AreEqual (40, rects[0].Y, "SetClip3");
274 Assert.AreEqual (210, rects[0].Width, "SetClip4");
275 Assert.AreEqual (220, rects[0].Height, "SetClip5");
278 g = Graphics.FromImage (bmp);
279 g.SetClip (new RectangleF (50, 40, 210, 220));
280 rects = g.Clip.GetRegionScans (new Matrix ());
281 Assert.AreEqual (1, rects.Length, "SetClip6");
282 Assert.AreEqual (50, rects[0].X, "SetClip7");
283 Assert.AreEqual (40, rects[0].Y, "SetClip8");
284 Assert.AreEqual (210, rects[0].Width, "SetClip9");
285 Assert.AreEqual (220, rects[0].Height, "SetClip10");
288 g = Graphics.FromImage (bmp);
289 g.SetClip (new Rectangle (50, 40, 210, 220));
290 rects = g.Clip.GetRegionScans (new Matrix ());
291 Assert.AreEqual (1, rects.Length, "SetClip10");
292 Assert.AreEqual (50, rects[0].X, "SetClip11");
293 Assert.AreEqual (40, rects[0].Y, "SetClip12");
294 Assert.AreEqual (210, rects[0].Width, "SetClip13");
295 Assert.AreEqual (220, rects[0].Height, "SetClip14");
299 public void SetSaveReset ()
301 Bitmap bmp = new Bitmap (200, 200);
302 Graphics g = Graphics.FromImage (bmp);
303 GraphicsState state_default, state_modified;
305 state_default = g.Save (); // Default
307 g.CompositingMode = CompositingMode.SourceCopy;
308 g.CompositingQuality = CompositingQuality.GammaCorrected;
309 g.InterpolationMode = InterpolationMode.HighQualityBilinear;
311 g.PageUnit = GraphicsUnit.Inch;
312 g.PixelOffsetMode = PixelOffsetMode.Half;
313 g.Clip = new Region (new Rectangle (0, 0, 100, 100));
314 g.RenderingOrigin = new Point (10, 20);
315 g.SmoothingMode = SmoothingMode.AntiAlias;
316 g.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
319 state_modified = g.Save (); // Modified
321 g.CompositingMode = CompositingMode.SourceOver;
322 g.CompositingQuality = CompositingQuality.Default;
323 g.InterpolationMode = InterpolationMode.Bilinear;
325 g.PageUnit = GraphicsUnit.Display;
326 g.PixelOffsetMode = PixelOffsetMode.Default;
327 g.Clip = new Region (new Rectangle (1, 2, 20, 25));
328 g.RenderingOrigin = new Point (5, 6);
329 g.SmoothingMode = SmoothingMode.None;
330 g.TextRenderingHint = TextRenderingHint.SystemDefault;
332 g.Restore (state_modified);
334 Assert.AreEqual (CompositingMode.SourceCopy, g.CompositingMode, "SetSaveReset1");
335 Assert.AreEqual (CompositingQuality.GammaCorrected, g.CompositingQuality, "SetSaveReset2");
336 Assert.AreEqual (InterpolationMode.HighQualityBilinear, g.InterpolationMode, "SetSaveReset3");
337 Assert.AreEqual (2, g.PageScale, "SetSaveReset4");
338 Assert.AreEqual (GraphicsUnit.Inch, g.PageUnit, "SetSaveReset5");
339 Assert.AreEqual (PixelOffsetMode.Half, g.PixelOffsetMode, "SetSaveReset6");
340 Assert.AreEqual (new Point (10, 20), g.RenderingOrigin, "SetSaveReset7");
341 Assert.AreEqual (SmoothingMode.AntiAlias, g.SmoothingMode, "SetSaveReset8");
342 Assert.AreEqual (TextRenderingHint.ClearTypeGridFit, g.TextRenderingHint, "SetSaveReset9");
343 Assert.AreEqual (0, (int) g.ClipBounds.X, "SetSaveReset10");
344 Assert.AreEqual (0, (int) g.ClipBounds.Y, "SetSaveReset10");
346 g.Restore (state_default);
348 Assert.AreEqual (CompositingMode.SourceOver, g.CompositingMode, "SetSaveReset11");
349 Assert.AreEqual (CompositingQuality.Default, g.CompositingQuality, "SetSaveReset12");
350 Assert.AreEqual (InterpolationMode.Bilinear, g.InterpolationMode, "SetSaveReset13");
351 Assert.AreEqual (1, g.PageScale, "SetSaveReset14");
352 Assert.AreEqual (GraphicsUnit.Display, g.PageUnit, "SetSaveReset15");
353 Assert.AreEqual (PixelOffsetMode.Default, g.PixelOffsetMode, "SetSaveReset16");
354 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "SetSaveReset17");
355 Assert.AreEqual (SmoothingMode.None, g.SmoothingMode, "SetSaveReset18");
356 Assert.AreEqual (TextRenderingHint.SystemDefault, g.TextRenderingHint, "SetSaveReset19");
358 Region r = new Region ();
359 Assert.AreEqual (r.GetBounds (g), g.ClipBounds, "SetSaveReset20");
365 [Category ("NotWorking")] // looks like MS PNG codec promote indexed format to 32bpp ARGB
366 public void LoadIndexed_PngStream ()
368 // Tests that we can load an indexed file
369 using (Stream s = Assembly.GetExecutingAssembly ().GetManifestResourceStream ("indexed.png")) {
370 using (Image img = Image.FromStream (s)) {
371 // however it's no more indexed once loaded
372 Assert.AreEqual (PixelFormat.Format32bppArgb, img.PixelFormat, "PixelFormat");
373 using (Graphics g = Graphics.FromImage (img)) {
374 Assert.AreEqual (img.Height, g.VisibleClipBounds.Height, "Height");
375 Assert.AreEqual (img.Width, g.VisibleClipBounds.Width, "Width");
382 public void LoadIndexed_BmpFile ()
384 // Tests that we can load an indexed file, but...
385 string sInFile = TestBitmap.getInFile ("bitmaps/almogaver1bit.bmp");
386 // note: file is misnamed (it's a 4bpp bitmap)
387 using (Image img = Image.FromFile (sInFile)) {
388 Assert.AreEqual (PixelFormat.Format4bppIndexed, img.PixelFormat, "PixelFormat");
389 Assert.Throws<Exception> (() => Graphics.FromImage (img));
394 public void FromImage ()
396 Assert.Throws<ArgumentNullException> (() => Graphics.FromImage (null));
399 private Graphics Get (int w, int h)
401 Bitmap bitmap = new Bitmap (w, h);
402 Graphics g = Graphics.FromImage (bitmap);
403 g.Clip = new Region (new Rectangle (0, 0, w, h));
407 private void Compare (string msg, RectangleF b1, RectangleF b2)
409 AssertEquals (msg + ".compare.X", b1.X, b2.X);
410 AssertEquals (msg + ".compare.Y", b1.Y, b2.Y);
411 AssertEquals (msg + ".compare.Width", b1.Width, b2.Width);
412 AssertEquals (msg + ".compare.Height", b1.Height, b2.Height);
416 public void Clip_GetBounds ()
418 Graphics g = Get (16, 16);
419 RectangleF bounds = g.Clip.GetBounds (g);
420 Assert.AreEqual (0, bounds.X, "X");
421 Assert.AreEqual (0, bounds.Y, "Y");
422 Assert.AreEqual (16, bounds.Width, "Width");
423 Assert.AreEqual (16, bounds.Height, "Height");
424 Assert.IsTrue (g.Transform.IsIdentity, "Identity");
429 public void Clip_TranslateTransform ()
431 Graphics g = Get (16, 16);
432 g.TranslateTransform (12.22f, 10.10f);
433 RectangleF bounds = g.Clip.GetBounds (g);
434 Compare ("translate", bounds, g.ClipBounds);
435 Assert.AreEqual (-12.2200003f, bounds.X, "translate.X");
436 Assert.AreEqual (-10.1000004f, bounds.Y, "translate.Y");
437 Assert.AreEqual (16, bounds.Width, "translate.Width");
438 Assert.AreEqual (16, bounds.Height, "translate.Height");
439 float[] elements = g.Transform.Elements;
440 Assert.AreEqual (1, elements[0], "translate.0");
441 Assert.AreEqual (0, elements[1], "translate.1");
442 Assert.AreEqual (0, elements[2], "translate.2");
443 Assert.AreEqual (1, elements[3], "translate.3");
444 Assert.AreEqual (12.2200003f, elements[4], "translate.4");
445 Assert.AreEqual (10.1000004f, elements[5], "translate.5");
448 bounds = g.Clip.GetBounds (g);
449 Compare ("reset", bounds, g.ClipBounds);
450 Assert.AreEqual (0, bounds.X, "reset.X");
451 Assert.AreEqual (0, bounds.Y, "reset.Y");
452 Assert.AreEqual (16, bounds.Width, "reset.Width");
453 Assert.AreEqual (16, bounds.Height, "reset.Height");
454 Assert.IsTrue (g.Transform.IsIdentity, "Identity");
459 public void Transform_NonInvertibleMatrix ()
461 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
462 Assert.IsFalse (matrix.IsInvertible, "IsInvertible");
463 Graphics g = Get (16, 16);
464 Assert.Throws<ArgumentException> (() => g.Transform = matrix);
469 public void Multiply_NonInvertibleMatrix ()
471 Matrix matrix = new Matrix (123, 24, 82, 16, 47, 30);
472 Assert.IsFalse (matrix.IsInvertible, "IsInvertible");
473 Graphics g = Get (16, 16);
474 Assert.Throws<ArgumentException> (() => g.MultiplyTransform (matrix));
478 public void Multiply_Null ()
480 Graphics g = Get (16, 16);
481 Assert.Throws<ArgumentNullException> (() => g.MultiplyTransform (null));
484 private void CheckBounds (string msg, RectangleF bounds, float x, float y, float w, float h)
486 AssertEquals (msg + ".X", x, bounds.X, 0.1);
487 AssertEquals (msg + ".Y", y, bounds.Y, 0.1);
488 AssertEquals (msg + ".Width", w, bounds.Width, 0.1);
489 AssertEquals (msg + ".Height", h, bounds.Height, 0.1);
493 public void ClipBounds ()
495 Graphics g = Get (16, 16);
496 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
497 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
499 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
500 CheckBounds ("clip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
501 CheckBounds ("clip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
505 public void ClipBounds_Rotate ()
507 Graphics g = Get (16, 16);
508 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
509 g.RotateTransform (90);
510 CheckBounds ("rotate.ClipBounds", g.ClipBounds, 0, -8, 8, 8);
511 CheckBounds ("rotate.Clip.GetBounds", g.Clip.GetBounds (g), 0, -8, 8, 8);
513 g.Transform = new Matrix ();
514 CheckBounds ("identity.ClipBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
515 CheckBounds ("identity.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
519 public void ClipBounds_Scale ()
521 RectangleF clip = new Rectangle (0, 0, 8, 8);
522 Graphics g = Get (16, 16);
523 g.Clip = new Region (clip);
524 g.ScaleTransform (0.25f, 0.5f);
525 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 32, 16);
526 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 32, 16);
529 CheckBounds ("setclip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
530 CheckBounds ("setclip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
534 public void ClipBounds_Translate ()
536 Graphics g = Get (16, 16);
537 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
538 Region clone = g.Clip.Clone ();
539 g.TranslateTransform (8, 8);
540 CheckBounds ("translate.ClipBounds", g.ClipBounds, -8, -8, 8, 8);
541 CheckBounds ("translate.Clip.GetBounds", g.Clip.GetBounds (g), -8, -8, 8, 8);
543 g.SetClip (clone, CombineMode.Replace);
544 CheckBounds ("setclip.ClipBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
545 CheckBounds ("setclip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
549 public void ClipBounds_Transform_Translation ()
551 Graphics g = Get (16, 16);
552 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
553 g.Transform = new Matrix (1, 0, 0, 1, 8, 8);
554 CheckBounds ("transform.ClipBounds", g.ClipBounds, -8, -8, 8, 8);
555 CheckBounds ("transform.Clip.GetBounds", g.Clip.GetBounds (g), -8, -8, 8, 8);
558 CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
559 CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
563 public void ClipBounds_Transform_Scale ()
565 Graphics g = Get (16, 16);
566 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
567 g.Transform = new Matrix (0.5f, 0, 0, 0.25f, 0, 0);
568 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 16, 32);
569 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 32);
572 // see next test for ClipBounds
573 CheckBounds ("resetclip.Clip.GetBounds", g.Clip.GetBounds (g), -4194304, -4194304, 8388608, 8388608);
574 Assert.IsTrue (g.Clip.IsInfinite (g), "IsInfinite");
578 [Category ("NotWorking")]
579 public void ClipBounds_Transform_Scale_Strange ()
581 Graphics g = Get (16, 16);
582 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
583 g.Transform = new Matrix (0.5f, 0, 0, 0.25f, 0, 0);
584 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, 0, 16, 32);
585 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 32);
588 // note: strange case where g.ClipBounds and g.Clip.GetBounds are different
589 CheckBounds ("resetclip.ClipBounds", g.ClipBounds, -8388608, -16777216, 16777216, 33554432);
593 public void ClipBounds_Multiply ()
595 Graphics g = Get (16, 16);
596 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
597 g.Transform = new Matrix (1, 0, 0, 1, 8, 8);
598 g.MultiplyTransform (g.Transform);
599 CheckBounds ("multiply.ClipBounds", g.ClipBounds, -16, -16, 8, 8);
600 CheckBounds ("multiply.Clip.GetBounds", g.Clip.GetBounds (g), -16, -16, 8, 8);
603 CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
604 CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
608 public void ClipBounds_Cumulative_Effects ()
610 Graphics g = Get (16, 16);
611 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
612 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
614 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
615 CheckBounds ("clip.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
616 CheckBounds ("clip.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
618 g.RotateTransform (90);
619 CheckBounds ("rotate.ClipBounds", g.ClipBounds, 0, -8, 8, 8);
620 CheckBounds ("rotate.Clip.GetBounds", g.Clip.GetBounds (g), 0, -8, 8, 8);
622 g.ScaleTransform (0.25f, 0.5f);
623 CheckBounds ("scale.ClipBounds", g.ClipBounds, 0, -16, 32, 16);
624 CheckBounds ("scale.Clip.GetBounds", g.Clip.GetBounds (g), 0, -16, 32, 16);
626 g.TranslateTransform (8, 8);
627 CheckBounds ("translate.ClipBounds", g.ClipBounds, -8, -24, 32, 16);
628 CheckBounds ("translate.Clip.GetBounds", g.Clip.GetBounds (g), -8, -24, 32, 16);
630 g.MultiplyTransform (g.Transform);
631 CheckBounds ("multiply.ClipBounds", g.ClipBounds, -104, -56, 64, 64);
632 CheckBounds ("multiply.Clip.GetBounds", g.Clip.GetBounds (g), -104, -56, 64, 64);
635 CheckBounds ("reset.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
636 CheckBounds ("reset.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
640 public void Clip_TranslateTransform_BoundsChange ()
642 Graphics g = Get (16, 16);
643 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
644 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
645 g.TranslateTransform (-16, -16);
646 CheckBounds ("translated.ClipBounds", g.ClipBounds, 16, 16, 16, 16);
647 CheckBounds ("translated.Clip.GetBounds", g.Clip.GetBounds (g), 16, 16, 16, 16);
649 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
650 // ClipBounds isn't affected by a previous translation
651 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
652 // Clip.GetBounds isn't affected by a previous translation
653 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
656 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -16, -16, 8, 8);
657 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -16, -16, 8, 8);
661 public void Clip_RotateTransform_BoundsChange ()
663 Graphics g = Get (16, 16);
664 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
665 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
666 // we select a "simple" angle because the region will be converted into
667 // a bitmap (well for libgdiplus) and we would lose precision after that
668 g.RotateTransform (90);
669 CheckBounds ("rotated.ClipBounds", g.ClipBounds, 0, -16, 16, 16);
670 CheckBounds ("rotated.Clip.GetBounds", g.Clip.GetBounds (g), 0, -16, 16, 16);
671 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
672 // ClipBounds isn't affected by a previous rotation (90)
673 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
674 // Clip.GetBounds isn't affected by a previous rotation
675 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
678 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -8, 0, 8, 8);
679 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -8, 0, 8, 8);
682 private void CheckBoundsInt (string msg, RectangleF bounds, int x, int y, int w, int h)
684 // currently bounds are rounded at 8 pixels (FIXME - we can go down to 1 pixel)
685 AssertEquals (msg + ".X", x, bounds.X, 4f);
686 AssertEquals (msg + ".Y", y, bounds.Y, 4f);
687 AssertEquals (msg + ".Width", w, bounds.Width, 4f);
688 AssertEquals (msg + ".Height", h, bounds.Height, 4f);
692 [Category ("NotWorking")]
693 public void Clip_RotateTransform_BoundsChange_45 ()
695 Graphics g = Get (16, 16);
696 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
697 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
698 g.RotateTransform (45);
699 // we can't use the "normal" CheckBound here because of libgdiplus crude rounding
700 CheckBoundsInt ("rotated.ClipBounds", g.ClipBounds, 0, -11, 24, 24);
701 CheckBoundsInt ("rotated.Clip.GetBounds", g.Clip.GetBounds (g), 0, -11, 24, 24);
702 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
703 // ClipBounds IS affected by a previous rotation (45)
704 CheckBoundsInt ("rectangle.ClipBounds", g.ClipBounds, -3, -4, 16, 16);
705 // Clip.GetBounds isn't affected by a previous rotation
706 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
709 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -5, 1, 11, 11);
710 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -5.6f, 0, 11.3f, 11.3f);
714 public void Clip_ScaleTransform_NoBoundsChange ()
716 Graphics g = Get (16, 16);
717 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
718 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
719 g.ScaleTransform (2, 0.5f);
720 CheckBounds ("scaled.ClipBounds", g.ClipBounds, 0, 0, 8, 32);
721 CheckBounds ("scaled.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 32);
722 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
723 // ClipBounds isn't affected by a previous scaling
724 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, 0, 0, 8, 8);
725 // Clip.GetBounds isn't affected by a previous scaling
726 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
729 CheckBounds ("reseted.ClipBounds", g.ClipBounds, 0, 0, 16, 4);
730 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 4);
734 [Category ("NotWorking")]
735 public void Clip_MultiplyTransform_NoBoundsChange ()
737 Graphics g = Get (16, 16);
738 CheckBounds ("graphics.ClipBounds", g.ClipBounds, 0, 0, 16, 16);
739 CheckBounds ("graphics.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 16, 16);
740 g.MultiplyTransform (new Matrix (2.5f, 0.5f, -2.5f, 0.5f, 4, -4));
741 CheckBounds ("multiplied.ClipBounds", g.ClipBounds, 3.2f, 1.6f, 19.2f, 19.2f);
742 CheckBounds ("multiplied.Clip.GetBounds", g.Clip.GetBounds (g), 3.2f, 1.6f, 19.2f, 19.2f);
743 g.Clip = new Region (new Rectangle (0, 0, 8, 8));
744 // ClipBounds IS affected by the previous multiplication
745 CheckBounds ("rectangle.ClipBounds", g.ClipBounds, -3, -3, 15, 15);
746 // Clip.GetBounds isn't affected by the previous multiplication
747 CheckBounds ("rectangle.Clip.GetBounds", g.Clip.GetBounds (g), 0, 0, 8, 8);
750 CheckBounds ("reseted.ClipBounds", g.ClipBounds, -16, -3, 40, 7);
751 CheckBounds ("reseted.Clip.GetBounds", g.Clip.GetBounds (g), -16, -4, 40, 8);
755 public void ScaleTransform_X0 ()
757 Graphics g = Get (16, 16);
758 Assert.Throws<ArgumentException> (() => g.ScaleTransform (0, 1));
762 public void ScaleTransform_Y0 ()
764 Graphics g = Get (16, 16);
765 Assert.Throws<ArgumentException> (() => g.ScaleTransform (1, 0));
769 public void TranslateTransform_Order ()
771 Graphics g = Get (16, 16);
772 g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
773 g.TranslateTransform (3, -3);
774 float[] elements = g.Transform.Elements;
775 Assert.AreEqual (1, elements[0], "default.0");
776 Assert.AreEqual (2, elements[1], "default.1");
777 Assert.AreEqual (3, elements[2], "default.2");
778 Assert.AreEqual (4, elements[3], "default.3");
779 Assert.AreEqual (-1, elements[4], "default.4");
780 Assert.AreEqual (0, elements[5], "default.5");
782 g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
783 g.TranslateTransform (3, -3, MatrixOrder.Prepend);
784 elements = g.Transform.Elements;
785 Assert.AreEqual (1, elements[0], "prepend.0");
786 Assert.AreEqual (2, elements[1], "prepend.1");
787 Assert.AreEqual (3, elements[2], "prepend.2");
788 Assert.AreEqual (4, elements[3], "prepend.3");
789 Assert.AreEqual (-1, elements[4], "prepend.4");
790 Assert.AreEqual (0, elements[5], "prepend.5");
792 g.Transform = new Matrix (1, 2, 3, 4, 5, 6);
793 g.TranslateTransform (3, -3, MatrixOrder.Append);
794 elements = g.Transform.Elements;
795 Assert.AreEqual (1, elements[0], "append.0");
796 Assert.AreEqual (2, elements[1], "append.1");
797 Assert.AreEqual (3, elements[2], "append.2");
798 Assert.AreEqual (4, elements[3], "append.3");
799 Assert.AreEqual (8, elements[4], "append.4");
800 Assert.AreEqual (3, elements[5], "append.5");
803 static Point[] SmallCurve = new Point[3] { new Point (0, 0), new Point (15, 5), new Point (5, 15) };
804 static PointF[] SmallCurveF = new PointF[3] { new PointF (0, 0), new PointF (15, 5), new PointF (5, 15) };
806 static Point[] TooSmallCurve = new Point[2] { new Point (0, 0), new Point (15, 5) };
807 static PointF[] LargeCurveF = new PointF[4] { new PointF (0, 0), new PointF (15, 5), new PointF (5, 15), new PointF (0, 20) };
810 public void DrawCurve_PenNull ()
812 Bitmap bitmap = new Bitmap (20, 20);
813 Graphics g = Graphics.FromImage (bitmap);
814 Assert.Throws<ArgumentNullException> (() => g.DrawCurve (null, SmallCurveF));
818 public void DrawCurve_PointFNull ()
820 Bitmap bitmap = new Bitmap (20, 20);
821 Graphics g = Graphics.FromImage (bitmap);
822 Assert.Throws<ArgumentNullException> (() => g.DrawCurve (Pens.Black, (PointF[]) null));
826 public void DrawCurve_PointNull ()
828 Bitmap bitmap = new Bitmap (20, 20);
829 Graphics g = Graphics.FromImage (bitmap);
830 Assert.Throws<ArgumentNullException> (() => g.DrawCurve (Pens.Black, (Point[]) null));
834 public void DrawCurve_NotEnoughPoints ()
836 Bitmap bitmap = new Bitmap (20, 20);
837 Graphics g = Graphics.FromImage (bitmap);
838 CheckForEmptyBitmap (bitmap);
839 g.DrawCurve (Pens.Black, TooSmallCurve, 0.5f);
840 CheckForNonEmptyBitmap (bitmap);
841 // so a "curve" can be drawn with less than 3 points!
842 // actually I used to call that a line... (and it's not related to tension)
848 public void DrawCurve_SinglePoint ()
850 Bitmap bitmap = new Bitmap (20, 20);
851 Graphics g = Graphics.FromImage (bitmap);
852 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, new Point[1] { new Point (10, 10) }, 0.5f));
853 // a single point isn't enough
857 public void DrawCurve3_NotEnoughPoints ()
859 Bitmap bitmap = new Bitmap (20, 20);
860 Graphics g = Graphics.FromImage (bitmap);
861 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, TooSmallCurve, 0, 2, 0.5f));
862 // aha, this is API dependent
866 public void DrawCurve_NegativeTension ()
868 Bitmap bitmap = new Bitmap (20, 20);
869 Graphics g = Graphics.FromImage (bitmap);
870 // documented as bigger (or equals) to 0
871 g.DrawCurve (Pens.Black, SmallCurveF, -0.9f);
872 CheckForNonEmptyBitmap (bitmap);
878 public void DrawCurve_PositiveTension ()
880 Bitmap bitmap = new Bitmap (20, 20);
881 Graphics g = Graphics.FromImage (bitmap);
882 g.DrawCurve (Pens.Black, SmallCurveF, 0.9f);
883 // this is not the same as -1
884 CheckForNonEmptyBitmap (bitmap);
890 [Category ("NotWorking")] // libgdiplus is drawing something
891 public void DrawCurve_LargeTension ()
893 Bitmap bitmap = new Bitmap (20, 20);
894 Graphics g = Graphics.FromImage (bitmap);
895 g.DrawCurve (Pens.Black, SmallCurve, Single.MaxValue);
896 CheckForEmptyBitmap (bitmap);
897 // too much tension ;)
903 public void DrawCurve_ZeroSegments ()
905 Bitmap bitmap = new Bitmap (20, 20);
906 Graphics g = Graphics.FromImage (bitmap);
907 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, SmallCurveF, 0, 0));
911 public void DrawCurve_NegativeSegments ()
913 Bitmap bitmap = new Bitmap (20, 20);
914 Graphics g = Graphics.FromImage (bitmap);
915 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, SmallCurveF, 0, -1));
919 public void DrawCurve_OffsetTooLarge ()
921 Bitmap bitmap = new Bitmap (20, 20);
922 Graphics g = Graphics.FromImage (bitmap);
923 // starting offset 1 doesn't give 3 points to make a curve
924 Assert.Throws<ArgumentException> (() => g.DrawCurve (Pens.Black, SmallCurveF, 1, 2));
925 // and in this case 2 points aren't enough to draw something
929 public void DrawCurve_Offset_0 ()
931 Bitmap bitmap = new Bitmap (20, 20);
932 Graphics g = Graphics.FromImage (bitmap);
933 g.DrawCurve (Pens.Black, LargeCurveF, 0, 2, 0.5f);
934 CheckForNonEmptyBitmap (bitmap);
940 public void DrawCurve_Offset_1 ()
942 Bitmap bitmap = new Bitmap (20, 20);
943 Graphics g = Graphics.FromImage (bitmap);
944 g.DrawCurve (Pens.Black, LargeCurveF, 1, 2, 0.5f);
945 CheckForNonEmptyBitmap (bitmap);
951 public void DrawCurve_Offset_2 ()
953 Bitmap bitmap = new Bitmap (20, 20);
954 Graphics g = Graphics.FromImage (bitmap);
955 // it works even with two points because we know the previous ones
956 g.DrawCurve (Pens.Black, LargeCurveF, 2, 1, 0.5f);
957 CheckForNonEmptyBitmap (bitmap);
963 public void DrawRectangle_Negative ()
965 Bitmap bitmap = new Bitmap (20, 20);
966 Graphics g = Graphics.FromImage (bitmap);
967 Pen pen = new Pen (Color.Red);
968 g.DrawRectangle (pen, 5, 5, -10, -10);
969 g.DrawRectangle (pen, 0.0f, 0.0f, 5.0f, -10.0f);
970 g.DrawRectangle (pen, new Rectangle (15, 0, -10, 5));
971 CheckForEmptyBitmap (bitmap);
978 public void DrawRectangles_Negative ()
980 Bitmap bitmap = new Bitmap (20, 20);
981 Graphics g = Graphics.FromImage (bitmap);
982 Pen pen = new Pen (Color.Red);
983 Rectangle[] rects = new Rectangle[2] {
984 new Rectangle (5, 5, -10, -10), new Rectangle (0, 0, 5, -10)
986 RectangleF[] rectf = new RectangleF[2] {
987 new RectangleF (0.0f, 5.0f, -10.0f, -10.0f), new RectangleF (15.0f, 0.0f, -10.0f, 5.0f)
989 g.DrawRectangles (pen, rects);
990 g.DrawRectangles (pen, rectf);
991 CheckForEmptyBitmap (bitmap);
998 public void FillRectangle_Negative ()
1000 Bitmap bitmap = new Bitmap (20, 20);
1001 Graphics g = Graphics.FromImage (bitmap);
1002 SolidBrush brush = new SolidBrush (Color.Red);
1003 g.FillRectangle (brush, 5, 5, -10, -10);
1004 g.FillRectangle (brush, 0.0f, 0.0f, 5.0f, -10.0f);
1005 g.FillRectangle (brush, new Rectangle (15, 0, -10, 5));
1006 CheckForEmptyBitmap (bitmap);
1013 public void FillRectangles_Negative ()
1015 Bitmap bitmap = new Bitmap (20, 20);
1016 Graphics g = Graphics.FromImage (bitmap);
1017 SolidBrush brush = new SolidBrush (Color.Red);
1018 Rectangle[] rects = new Rectangle[2] {
1019 new Rectangle (5, 5, -10, -10), new Rectangle (0, 0, 5, -10)
1021 RectangleF[] rectf = new RectangleF[2] {
1022 new RectangleF (0.0f, 5.0f, -10.0f, -10.0f), new RectangleF (15.0f, 0.0f, -10.0f, 5.0f)
1024 g.FillRectangles (brush, rects);
1025 g.FillRectangles (brush, rectf);
1026 CheckForEmptyBitmap (bitmap);
1032 [Test] // bug #355141
1034 public void FromHwnd_Zero ()
1036 Graphics g = Graphics.FromHwnd (IntPtr.Zero);
1037 Assert.IsNotNull (g);
1040 private void CheckDefaultProperties (string message, Graphics g)
1042 Assert.IsTrue (g.Clip.IsInfinite (g), message + ".Clip.IsInfinite");
1043 AssertEquals (message + ".CompositingMode", CompositingMode.SourceOver, g.CompositingMode);
1044 AssertEquals (message + ".CompositingQuality", CompositingQuality.Default, g.CompositingQuality);
1045 AssertEquals (message + ".InterpolationMode", InterpolationMode.Bilinear, g.InterpolationMode);
1046 AssertEquals (message + ".PageScale", 1.0f, g.PageScale);
1047 AssertEquals (message + ".PageUnit", GraphicsUnit.Display, g.PageUnit);
1048 AssertEquals (message + ".PixelOffsetMode", PixelOffsetMode.Default, g.PixelOffsetMode);
1049 AssertEquals (message + ".SmoothingMode", SmoothingMode.None, g.SmoothingMode);
1050 AssertEquals (message + ".TextContrast", 4, g.TextContrast);
1051 AssertEquals (message + ".TextRenderingHint", TextRenderingHint.SystemDefault, g.TextRenderingHint);
1052 Assert.IsTrue (g.Transform.IsIdentity, message + ".Transform.IsIdentity");
1055 private void CheckCustomProperties (string message, Graphics g)
1057 Assert.IsFalse (g.Clip.IsInfinite (g), message + ".Clip.IsInfinite");
1058 AssertEquals (message + ".CompositingMode", CompositingMode.SourceCopy, g.CompositingMode);
1059 AssertEquals (message + ".CompositingQuality", CompositingQuality.HighQuality, g.CompositingQuality);
1060 AssertEquals (message + ".InterpolationMode", InterpolationMode.HighQualityBicubic, g.InterpolationMode);
1061 AssertEquals (message + ".PageScale", 0.5f, g.PageScale);
1062 AssertEquals (message + ".PageUnit", GraphicsUnit.Inch, g.PageUnit);
1063 AssertEquals (message + ".PixelOffsetMode", PixelOffsetMode.Half, g.PixelOffsetMode);
1064 AssertEquals (message + ".RenderingOrigin", new Point (-1, -1), g.RenderingOrigin);
1065 AssertEquals (message + ".SmoothingMode", SmoothingMode.AntiAlias, g.SmoothingMode);
1066 AssertEquals (message + ".TextContrast", 0, g.TextContrast);
1067 AssertEquals (message + ".TextRenderingHint", TextRenderingHint.AntiAlias, g.TextRenderingHint);
1068 Assert.IsFalse (g.Transform.IsIdentity, message + ".Transform.IsIdentity");
1071 private void CheckMatrix (string message, Matrix m, float xx, float yx, float xy, float yy, float x0, float y0)
1073 float[] elements = m.Elements;
1074 AssertEquals (message + ".Matrix.xx", xx, elements[0], 0.01);
1075 AssertEquals (message + ".Matrix.yx", yx, elements[1], 0.01);
1076 AssertEquals (message + ".Matrix.xy", xy, elements[2], 0.01);
1077 AssertEquals (message + ".Matrix.yy", yy, elements[3], 0.01);
1078 AssertEquals (message + ".Matrix.x0", x0, elements[4], 0.01);
1079 AssertEquals (message + ".Matrix.y0", y0, elements[5], 0.01);
1083 public void BeginContainer ()
1085 Bitmap bitmap = new Bitmap (20, 20);
1086 Graphics g = Graphics.FromImage (bitmap);
1088 CheckDefaultProperties ("default", g);
1089 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1091 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1092 g.CompositingMode = CompositingMode.SourceCopy;
1093 g.CompositingQuality = CompositingQuality.HighQuality;
1094 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1096 g.PageUnit = GraphicsUnit.Inch;
1097 g.PixelOffsetMode = PixelOffsetMode.Half;
1098 g.RenderingOrigin = new Point (-1, -1);
1099 g.RotateTransform (45);
1100 g.SmoothingMode = SmoothingMode.AntiAlias;
1102 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1103 CheckCustomProperties ("modified", g);
1104 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1106 GraphicsContainer gc = g.BeginContainer ();
1107 // things gets reseted after calling BeginContainer
1108 CheckDefaultProperties ("BeginContainer", g);
1109 // but not everything
1110 Assert.AreEqual (new Point (-1, -1), g.RenderingOrigin, "BeginContainer.RenderingOrigin");
1112 g.EndContainer (gc);
1113 CheckCustomProperties ("EndContainer", g);
1117 public void BeginContainer_Rect ()
1119 Bitmap bitmap = new Bitmap (20, 20);
1120 Graphics g = Graphics.FromImage (bitmap);
1122 CheckDefaultProperties ("default", g);
1123 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1125 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1126 g.CompositingMode = CompositingMode.SourceCopy;
1127 g.CompositingQuality = CompositingQuality.HighQuality;
1128 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1130 g.PageUnit = GraphicsUnit.Inch;
1131 g.PixelOffsetMode = PixelOffsetMode.Half;
1132 g.RenderingOrigin = new Point (-1, -1);
1133 g.RotateTransform (45);
1134 g.SmoothingMode = SmoothingMode.AntiAlias;
1136 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1137 CheckCustomProperties ("modified", g);
1138 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1140 GraphicsContainer gc = g.BeginContainer (new Rectangle (10, 20, 30, 40), new Rectangle (10, 20, 300, 400), GraphicsUnit.Millimeter);
1141 // things gets reseted after calling BeginContainer
1142 CheckDefaultProperties ("BeginContainer", g);
1143 // but not everything
1144 Assert.AreEqual (new Point (-1, -1), g.RenderingOrigin, "BeginContainer.RenderingOrigin");
1146 g.EndContainer (gc);
1147 CheckCustomProperties ("EndContainer", g);
1148 CheckMatrix ("EndContainer.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1152 public void BeginContainer_RectF ()
1154 Bitmap bitmap = new Bitmap (20, 20);
1155 Graphics g = Graphics.FromImage (bitmap);
1157 CheckDefaultProperties ("default", g);
1158 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1160 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1161 g.CompositingMode = CompositingMode.SourceCopy;
1162 g.CompositingQuality = CompositingQuality.HighQuality;
1163 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1165 g.PageUnit = GraphicsUnit.Inch;
1166 g.PixelOffsetMode = PixelOffsetMode.Half;
1167 g.RenderingOrigin = new Point (-1, -1);
1168 g.RotateTransform (45);
1169 g.SmoothingMode = SmoothingMode.AntiAlias;
1171 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1172 CheckCustomProperties ("modified", g);
1173 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1175 GraphicsContainer gc = g.BeginContainer (new RectangleF (40, 30, 20, 10), new RectangleF (10, 20, 30, 40), GraphicsUnit.Inch);
1176 // things gets reseted after calling BeginContainer
1177 CheckDefaultProperties ("BeginContainer", g);
1178 // but not everything
1179 Assert.AreEqual (new Point (-1, -1), g.RenderingOrigin, "BeginContainer.RenderingOrigin");
1181 g.EndContainer (gc);
1182 CheckCustomProperties ("EndContainer", g);
1185 private void BeginContainer_GraphicsUnit (GraphicsUnit unit)
1187 Bitmap bitmap = new Bitmap (20, 20);
1188 Graphics g = Graphics.FromImage (bitmap);
1189 g.BeginContainer (new RectangleF (40, 30, 20, 10), new RectangleF (10, 20, 30, 40), unit);
1193 public void BeginContainer_GraphicsUnit_Display ()
1195 Assert.Throws<ArgumentException> (() => BeginContainer_GraphicsUnit(GraphicsUnit.Display));
1199 public void BeginContainer_GraphicsUnit_Valid ()
1201 BeginContainer_GraphicsUnit (GraphicsUnit.Document);
1202 BeginContainer_GraphicsUnit (GraphicsUnit.Inch);
1203 BeginContainer_GraphicsUnit (GraphicsUnit.Millimeter);
1204 BeginContainer_GraphicsUnit (GraphicsUnit.Pixel);
1205 BeginContainer_GraphicsUnit (GraphicsUnit.Point);
1209 public void BeginContainer_GraphicsUnit_World ()
1211 Assert.Throws<ArgumentException> (() => BeginContainer_GraphicsUnit(GraphicsUnit.World));
1215 public void BeginContainer_GraphicsUnit_Bad ()
1217 Assert.Throws<ArgumentException> (() => BeginContainer_GraphicsUnit((GraphicsUnit) Int32.MinValue));
1221 public void EndContainer_Null ()
1223 Bitmap bitmap = new Bitmap (20, 20);
1224 Graphics g = Graphics.FromImage (bitmap);
1225 Assert.Throws<ArgumentNullException> (() => g.EndContainer (null));
1231 Bitmap bitmap = new Bitmap (20, 20);
1232 Graphics g = Graphics.FromImage (bitmap);
1234 CheckDefaultProperties ("default", g);
1235 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "default.RenderingOrigin");
1237 GraphicsState gs1 = g.Save ();
1238 // nothing is changed after a save
1239 CheckDefaultProperties ("save1", g);
1240 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "save1.RenderingOrigin");
1242 g.Clip = new Region (new Rectangle (10, 10, 10, 10));
1243 g.CompositingMode = CompositingMode.SourceCopy;
1244 g.CompositingQuality = CompositingQuality.HighQuality;
1245 g.InterpolationMode = InterpolationMode.HighQualityBicubic;
1247 g.PageUnit = GraphicsUnit.Inch;
1248 g.PixelOffsetMode = PixelOffsetMode.Half;
1249 g.RenderingOrigin = new Point (-1, -1);
1250 g.RotateTransform (45);
1251 g.SmoothingMode = SmoothingMode.AntiAlias;
1253 g.TextRenderingHint = TextRenderingHint.AntiAlias;
1254 CheckCustomProperties ("modified", g);
1255 CheckMatrix ("modified.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1257 GraphicsState gs2 = g.Save ();
1258 CheckCustomProperties ("save2", g);
1261 CheckCustomProperties ("restored1", g);
1262 CheckMatrix ("restored1.Transform", g.Transform, 0.707f, 0.707f, -0.707f, 0.707f, 0, 0);
1265 CheckDefaultProperties ("restored2", g);
1266 Assert.AreEqual (new Point (0, 0), g.RenderingOrigin, "restored2.RenderingOrigin");
1270 public void Restore_Null ()
1272 Bitmap bitmap = new Bitmap (20, 20);
1273 Graphics g = Graphics.FromImage (bitmap);
1274 Assert.Throws<NullReferenceException> (() => g.Restore (null));
1278 public void FillRectangles_BrushNull_Rectangle ()
1280 using (Bitmap bitmap = new Bitmap (20, 20)) {
1281 using (Graphics g = Graphics.FromImage (bitmap)) {
1282 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (null, new Rectangle[1]));
1288 public void FillRectangles_Rectangle_Null ()
1290 using (Bitmap bitmap = new Bitmap (20, 20)) {
1291 using (Graphics g = Graphics.FromImage (bitmap)) {
1292 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (Brushes.Red, (Rectangle[]) null));
1297 [Test] // see bug #78408
1298 public void FillRectanglesZeroRectangle ()
1300 using (Bitmap bitmap = new Bitmap (20, 20)) {
1301 using (Graphics g = Graphics.FromImage (bitmap)) {
1302 Assert.Throws<ArgumentException> (() => g.FillRectangles (Brushes.Red, new Rectangle[0]));
1308 public void FillRectangles_BrushNull_RectangleF ()
1310 using (Bitmap bitmap = new Bitmap (20, 20)) {
1311 using (Graphics g = Graphics.FromImage (bitmap)) {
1312 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (null, new RectangleF[1]));
1318 public void FillRectangles_RectangleF_Null ()
1320 using (Bitmap bitmap = new Bitmap (20, 20)) {
1321 using (Graphics g = Graphics.FromImage (bitmap)) {
1322 Assert.Throws<ArgumentNullException> (() => g.FillRectangles (Brushes.Red, (RectangleF[]) null));
1327 [Test] // see bug #78408
1328 public void FillRectanglesZeroRectangleF ()
1330 using (Bitmap bitmap = new Bitmap (20, 20)) {
1331 using (Graphics g = Graphics.FromImage (bitmap)) {
1332 Assert.Throws<ArgumentException> (() => g.FillRectangles (Brushes.Red, new RectangleF[0]));
1338 public void FillRectangles_NormalBehavior ()
1340 using (Bitmap bitmap = new Bitmap (20, 20)) {
1341 using (Graphics g = Graphics.FromImage (bitmap)) {
1342 g.Clear (Color.Fuchsia);
1343 Rectangle rect = new Rectangle (5, 5, 10, 10);
1344 g.Clip = new Region (rect);
1345 g.FillRectangle (Brushes.Red, rect);
1347 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1348 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1349 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (5, 14).ToArgb (), "5,14");
1350 Assert.AreEqual (Color.Red.ToArgb (), bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1352 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1353 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1354 Assert.AreEqual (Color.Fuchsia.ToArgb (), bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1358 // see bug #81737 for details
1359 private Bitmap FillDrawRectangle (float width)
1361 Bitmap bitmap = new Bitmap (20, 20);
1362 using (Graphics g = Graphics.FromImage (bitmap)) {
1363 g.Clear (Color.Red);
1364 Rectangle rect = new Rectangle (5, 5, 10, 10);
1365 g.FillRectangle (Brushes.Green, rect);
1367 using (Pen pen = new Pen (Color.Blue, width)) {
1368 g.DrawRectangle (pen, rect);
1371 g.DrawRectangle (Pens.Blue, rect);
1378 public void FillDrawRectangle_Width_Default ()
1381 using (Bitmap bitmap = FillDrawRectangle (Single.MinValue)) {
1383 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1384 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1385 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1387 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1388 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1389 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1391 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1392 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1393 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1395 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1396 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1397 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1399 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1400 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1401 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1403 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1404 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1405 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1407 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1408 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1409 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1411 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1412 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1413 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1418 [Category ("NotOnMac")]
1419 public void FillDrawRectangle_Width_2 ()
1422 using (Bitmap bitmap = FillDrawRectangle (2.0f)) {
1424 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1425 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1426 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1427 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1429 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1430 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1431 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1432 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1434 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1435 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1436 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1437 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1439 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1440 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1441 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1442 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 9).ToArgb (), "13,9");
1444 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1445 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1446 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1447 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 13).ToArgb (), "13,13");
1449 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1450 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1451 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1452 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 13).ToArgb (), "9,13");
1454 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 16).ToArgb (), "3,16");
1455 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 15).ToArgb (), "4,15");
1456 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 14).ToArgb (), "5,14");
1457 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 13).ToArgb (), "6,13");
1459 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1460 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1461 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1462 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1467 public void FillDrawRectangle_Width_3 ()
1470 using (Bitmap bitmap = FillDrawRectangle (3.0f)) {
1472 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1473 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1474 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1475 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1476 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 7).ToArgb (), "7,7");
1478 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1479 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1480 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1481 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1482 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 7).ToArgb (), "9,7");
1484 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1485 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1486 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1487 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1488 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 7).ToArgb (), "13,7");
1490 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 9).ToArgb (), "17,9");
1491 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1492 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1493 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1494 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 9).ToArgb (), "13,9");
1496 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 17).ToArgb (), "17,17");
1497 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1498 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1499 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1500 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (13, 13).ToArgb (), "13,13");
1502 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 17).ToArgb (), "9,17");
1503 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1504 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1505 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1506 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 13).ToArgb (), "9,13");
1508 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 17).ToArgb (), "3,17");
1509 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1510 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1511 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1512 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 13).ToArgb (), "7,13");
1514 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1515 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1516 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1517 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1518 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (7, 9).ToArgb (), "7,9");
1522 // reverse, draw the fill over
1523 private Bitmap DrawFillRectangle (float width)
1525 Bitmap bitmap = new Bitmap (20, 20);
1526 using (Graphics g = Graphics.FromImage (bitmap)) {
1527 g.Clear (Color.Red);
1528 Rectangle rect = new Rectangle (5, 5, 10, 10);
1530 using (Pen pen = new Pen (Color.Blue, width)) {
1531 g.DrawRectangle (pen, rect);
1534 g.DrawRectangle (Pens.Blue, rect);
1536 g.FillRectangle (Brushes.Green, rect);
1542 public void DrawFillRectangle_Width_Default ()
1545 using (Bitmap bitmap = DrawFillRectangle (Single.MinValue)) {
1546 // NW - no blue border
1547 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1548 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1549 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 6).ToArgb (), "6,6");
1550 // N - no blue border
1551 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1552 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1553 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 6).ToArgb (), "9,6");
1555 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1556 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1557 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1559 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1560 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1561 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1563 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1564 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1565 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1567 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1568 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1569 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1571 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1572 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1573 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1574 // W - no blue border
1575 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1576 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1577 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 9).ToArgb (), "6,9");
1582 [Category ("NotOnMac")]
1583 public void DrawFillRectangle_Width_2 ()
1586 using (Bitmap bitmap = DrawFillRectangle (2.0f)) {
1587 // looks like a one pixel border - but enlarged
1589 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1590 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1591 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1593 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1594 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1595 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1597 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1598 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1599 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1601 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1602 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1603 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1605 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1606 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1607 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1609 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1610 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1611 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1613 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 16).ToArgb (), "4,16");
1614 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 15).ToArgb (), "5,15");
1615 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 14).ToArgb (), "6,14");
1617 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1618 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1619 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1624 public void DrawFillRectangle_Width_3 ()
1627 using (Bitmap bitmap = DrawFillRectangle (3.0f)) {
1629 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 3).ToArgb (), "3,3");
1630 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1631 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1633 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 3).ToArgb (), "9,3");
1634 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 4).ToArgb (), "9,4");
1635 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 5).ToArgb (), "9,5");
1637 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1638 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1639 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1640 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1642 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 9).ToArgb (), "17,9");
1643 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 9).ToArgb (), "16,9");
1644 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 9).ToArgb (), "15,9");
1645 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 9).ToArgb (), "14,9");
1647 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 17).ToArgb (), "17,17");
1648 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1649 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1650 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1652 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (9, 17).ToArgb (), "9,17");
1653 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 16).ToArgb (), "9,16");
1654 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (9, 15).ToArgb (), "9,15");
1655 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (9, 14).ToArgb (), "9,14");
1657 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 17).ToArgb (), "3,17");
1658 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 16).ToArgb (), "4,16");
1659 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 15).ToArgb (), "5,15");
1660 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (6, 14).ToArgb (), "6,14");
1662 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (3, 9).ToArgb (), "3,9");
1663 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (4, 9).ToArgb (), "4,9");
1664 Assert.AreEqual (0xFF008000, (uint) bitmap.GetPixel (5, 9).ToArgb (), "5,9");
1668 private Bitmap DrawLines (float width)
1670 Bitmap bitmap = new Bitmap (20, 20);
1671 using (Graphics g = Graphics.FromImage (bitmap)) {
1672 g.Clear (Color.Red);
1673 Point[] pts = new Point[3] { new Point (5, 5), new Point (15, 5), new Point (15, 15) };
1675 using (Pen pen = new Pen (Color.Blue, width)) {
1676 g.DrawLines (pen, pts);
1679 g.DrawLines (Pens.Blue, pts);
1686 public void DrawLines_Width_Default ()
1689 using (Bitmap bitmap = DrawLines (Single.MinValue)) {
1691 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1692 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1693 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1694 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1695 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1696 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1698 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1699 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1700 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1701 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1702 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1703 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1704 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1705 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1706 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1708 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1709 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1710 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1711 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 16).ToArgb (), "14,16");
1712 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 16).ToArgb (), "15,16");
1713 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 16).ToArgb (), "16,16");
1718 [Category ("NotWorking")]
1719 public void DrawLines_Width_2 ()
1722 using (Bitmap bitmap = DrawLines (2.0f)) {
1724 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 3).ToArgb (), "4,3");
1725 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1726 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1727 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1728 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 3).ToArgb (), "5,3");
1729 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1730 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1731 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1733 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 3).ToArgb (), "13,3");
1734 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 4).ToArgb (), "13,4");
1735 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 5).ToArgb (), "13,5");
1736 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1737 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 3).ToArgb (), "14,3");
1738 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1739 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1740 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1741 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 3).ToArgb (), "15,3");
1742 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1743 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1744 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1745 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1746 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1747 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1748 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1750 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 14).ToArgb (), "13,14");
1751 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1752 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 14).ToArgb (), "15,14");
1753 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 14).ToArgb (), "16,14");
1754 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 15).ToArgb (), "13,15");
1755 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1756 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1757 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1762 [Category ("NotWorking")]
1763 public void DrawLines_Width_3 ()
1766 using (Bitmap bitmap = DrawLines (3.0f)) {
1768 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 3).ToArgb (), "4,3");
1769 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 4).ToArgb (), "4,4");
1770 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 5).ToArgb (), "4,5");
1771 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 6).ToArgb (), "4,6");
1772 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (4, 7).ToArgb (), "4,7");
1773 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 3).ToArgb (), "5,3");
1774 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 4).ToArgb (), "5,4");
1775 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 5).ToArgb (), "5,5");
1776 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (5, 6).ToArgb (), "5,6");
1777 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (5, 7).ToArgb (), "5,7");
1779 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 3).ToArgb (), "13,3");
1780 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 4).ToArgb (), "13,4");
1781 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 5).ToArgb (), "13,5");
1782 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (13, 6).ToArgb (), "13,6");
1783 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 7).ToArgb (), "13,7");
1784 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 3).ToArgb (), "14,3");
1785 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 4).ToArgb (), "14,4");
1786 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 5).ToArgb (), "14,5");
1787 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 6).ToArgb (), "14,6");
1788 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 7).ToArgb (), "14,7");
1789 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 3).ToArgb (), "15,3");
1790 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 4).ToArgb (), "15,4");
1791 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 5).ToArgb (), "15,5");
1792 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 6).ToArgb (), "15,6");
1793 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 7).ToArgb (), "15,7");
1794 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 3).ToArgb (), "16,3");
1795 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 4).ToArgb (), "16,4");
1796 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 5).ToArgb (), "16,5");
1797 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 6).ToArgb (), "16,6");
1798 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 7).ToArgb (), "16,7");
1799 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 3).ToArgb (), "17,3");
1800 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 4).ToArgb (), "17,4");
1801 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 5).ToArgb (), "17,5");
1802 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 6).ToArgb (), "17,6");
1803 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 7).ToArgb (), "17,7");
1805 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 14).ToArgb (), "13,14");
1806 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (14, 14).ToArgb (), "14,14");
1807 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (15, 14).ToArgb (), "15,14");
1808 Assert.AreEqual (0xFF0000FF, (uint) bitmap.GetPixel (16, 14).ToArgb (), "16,14");
1809 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 14).ToArgb (), "17,14");
1810 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (13, 15).ToArgb (), "13,15");
1811 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (14, 15).ToArgb (), "14,15");
1812 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (15, 15).ToArgb (), "15,15");
1813 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (16, 15).ToArgb (), "16,15");
1814 Assert.AreEqual (0xFFFF0000, (uint) bitmap.GetPixel (17, 15).ToArgb (), "17,15");
1819 public void MeasureString_StringFont ()
1821 using (Bitmap bitmap = new Bitmap (20, 20)) {
1822 using (Graphics g = Graphics.FromImage (bitmap)) {
1823 SizeF size = g.MeasureString (null, font);
1824 Assert.IsTrue (size.IsEmpty, "MeasureString(null,font)");
1825 size = g.MeasureString (String.Empty, font);
1826 Assert.IsTrue (size.IsEmpty, "MeasureString(empty,font)");
1828 size = g.MeasureString (null, null);
1829 Assert.IsTrue (size.IsEmpty, "MeasureString(null,null)");
1830 size = g.MeasureString (String.Empty, null);
1831 Assert.IsTrue (size.IsEmpty, "MeasureString(empty,null)");
1837 public void MeasureString_StringFont_Null ()
1839 using (Bitmap bitmap = new Bitmap (20, 20)) {
1840 using (Graphics g = Graphics.FromImage (bitmap)) {
1841 Assert.Throws<ArgumentNullException> (() => g.MeasureString ("a", null));
1847 public void MeasureString_StringFontSizeF ()
1850 Assert.Ignore ("Couldn't create required font");
1852 using (Bitmap bitmap = new Bitmap (20, 20)) {
1853 using (Graphics g = Graphics.FromImage (bitmap)) {
1854 SizeF size = g.MeasureString ("a", font, SizeF.Empty);
1855 Assert.IsFalse (size.IsEmpty, "MeasureString(a,font,empty)");
1857 size = g.MeasureString (String.Empty, font, SizeF.Empty);
1858 Assert.IsTrue (size.IsEmpty, "MeasureString(empty,font,empty)");
1863 private void MeasureString_StringFontInt (string s)
1866 Assert.Ignore ("Couldn't create required font");
1868 using (Bitmap bitmap = new Bitmap (20, 20)) {
1869 using (Graphics g = Graphics.FromImage (bitmap)) {
1870 SizeF size0 = g.MeasureString (s, font, 0);
1871 SizeF sizeN = g.MeasureString (s, font, Int32.MinValue);
1872 SizeF sizeP = g.MeasureString (s, font, Int32.MaxValue);
1873 Assert.AreEqual (size0, sizeN, "0-Min");
1874 Assert.AreEqual (size0, sizeP, "0-Max");
1880 public void MeasureString_StringFontInt_ShortString ()
1882 MeasureString_StringFontInt ("a");
1886 public void MeasureString_StringFontInt_LongString ()
1888 HostIgnoreList.CheckTest ("MonoTests.System.Drawing.GraphicsTest.MeasureString_StringFontInt_LongString");
1889 MeasureString_StringFontInt ("A very long string..."); // see bug #79643
1893 public void MeasureString_StringFormat_Alignment ()
1896 Assert.Ignore ("Couldn't create required font");
1898 string text = "Hello Mono::";
1899 StringFormat string_format = new StringFormat ();
1901 using (Bitmap bitmap = new Bitmap (20, 20)) {
1902 using (Graphics g = Graphics.FromImage (bitmap)) {
1903 string_format.Alignment = StringAlignment.Near;
1904 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1906 string_format.Alignment = StringAlignment.Center;
1907 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1909 string_format.Alignment = StringAlignment.Far;
1910 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1912 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1913 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1915 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1916 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1922 public void MeasureString_StringFormat_Alignment_DirectionVertical ()
1925 Assert.Ignore ("Couldn't create required font");
1927 string text = "Hello Mono::";
1928 StringFormat string_format = new StringFormat ();
1929 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
1931 using (Bitmap bitmap = new Bitmap (20, 20)) {
1932 using (Graphics g = Graphics.FromImage (bitmap)) {
1933 string_format.Alignment = StringAlignment.Near;
1934 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1936 string_format.Alignment = StringAlignment.Center;
1937 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1939 string_format.Alignment = StringAlignment.Far;
1940 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1942 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1943 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1945 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1946 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1952 public void MeasureString_StringFormat_LineAlignment ()
1955 Assert.Ignore ("Couldn't create required font");
1957 string text = "Hello Mono::";
1958 StringFormat string_format = new StringFormat ();
1960 using (Bitmap bitmap = new Bitmap (20, 20)) {
1961 using (Graphics g = Graphics.FromImage (bitmap)) {
1962 string_format.LineAlignment = StringAlignment.Near;
1963 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1965 string_format.LineAlignment = StringAlignment.Center;
1966 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1968 string_format.LineAlignment = StringAlignment.Far;
1969 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
1971 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
1972 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
1974 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
1975 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
1981 public void MeasureString_StringFormat_LineAlignment_DirectionVertical ()
1984 Assert.Ignore ("Couldn't create required font");
1986 string text = "Hello Mono::";
1987 StringFormat string_format = new StringFormat ();
1988 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
1990 using (Bitmap bitmap = new Bitmap (20, 20)) {
1991 using (Graphics g = Graphics.FromImage (bitmap)) {
1992 string_format.LineAlignment = StringAlignment.Near;
1993 SizeF near = g.MeasureString (text, font, Int32.MaxValue, string_format);
1995 string_format.LineAlignment = StringAlignment.Center;
1996 SizeF center = g.MeasureString (text, font, Int32.MaxValue, string_format);
1998 string_format.LineAlignment = StringAlignment.Far;
1999 SizeF far = g.MeasureString (text, font, Int32.MaxValue, string_format);
2001 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2002 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2004 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2005 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2011 public void MeasureString_MultlineString_Width ()
2013 using (Bitmap bitmap = new Bitmap (20, 20)) {
2014 using (Graphics g = Graphics.FromImage (bitmap)) {
2015 StringFormat string_format = new StringFormat ();
2017 string text1 = "Test\nTest123\nTest 456\nTest 1,2,3,4,5...";
2018 string text2 = "Test 1,2,3,4,5...";
2020 SizeF size1 = g.MeasureString (text1, font, SizeF.Empty, string_format);
2021 SizeF size2 = g.MeasureString (text2, font, SizeF.Empty, string_format);
2023 Assert.AreEqual ((int) size1.Width, (int) size2.Width, "Multiline Text Width");
2029 public void MeasureString_Bug76664 ()
2032 Assert.Ignore ("Couldn't create required font");
2034 using (Bitmap bitmap = new Bitmap (20, 20)) {
2035 using (Graphics g = Graphics.FromImage (bitmap)) {
2036 string s = "aaa aa aaaa a aaa";
2037 SizeF size = g.MeasureString (s, font);
2040 SizeF size2 = g.MeasureString (s, font, new SizeF (80, size.Height), null, out chars, out lines);
2043 Assert.IsTrue (size2.Width < size.Width, "Width/pixel");
2044 Assert.AreEqual (size2.Height, size.Height, "Height/pixel");
2046 Assert.AreEqual (1, lines, "lines fitted");
2047 // LAMESPEC: documentation seems to suggest chars is total length
2048 Assert.IsTrue (chars < s.Length, "characters fitted");
2054 public void MeasureString_Bug80680 ()
2057 Assert.Ignore ("Couldn't create required font");
2059 using (Bitmap bitmap = new Bitmap (20, 20)) {
2060 using (Graphics g = Graphics.FromImage (bitmap)) {
2061 string s = String.Empty;
2062 SizeF size = g.MeasureString (s, font);
2063 Assert.AreEqual (0, size.Height, "Empty.Height");
2064 Assert.AreEqual (0, size.Width, "Empty.Width");
2067 SizeF expected = g.MeasureString (s, font);
2068 for (int i = 1; i < 10; i++) {
2070 size = g.MeasureString (s, font);
2071 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2072 Assert.AreEqual (expected.Width, size.Width, 0.1, ">" + s + "< Width");
2076 expected = g.MeasureString (s, font);
2078 size = g.MeasureString (s, font);
2079 float space_width = size.Width - expected.Width;
2080 for (int i = 1; i < 10; i++) {
2081 size = g.MeasureString (s, font);
2082 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2083 Assert.AreEqual (expected.Width + i * space_width, size.Width, 0.1, ">" + s + "< Width");
2088 expected = g.MeasureString (s, font);
2089 for (int i = 1; i < 10; i++) {
2091 size = g.MeasureString (s, font);
2092 Assert.AreEqual (expected.Height, size.Height, 0.1, ">" + s + "< Height");
2093 Assert.AreEqual (expected.Width, size.Width, 0.1, ">" + s + "< Width");
2100 public void MeasureCharacterRanges_NullOrEmptyText ()
2102 using (Bitmap bitmap = new Bitmap (20, 20)) {
2103 using (Graphics g = Graphics.FromImage (bitmap)) {
2104 Region[] regions = g.MeasureCharacterRanges (null, font, new RectangleF (), null);
2105 Assert.AreEqual (0, regions.Length, "text null");
2106 regions = g.MeasureCharacterRanges (String.Empty, font, new RectangleF (), null);
2107 Assert.AreEqual (0, regions.Length, "text empty");
2108 // null font is ok with null or empty string
2109 regions = g.MeasureCharacterRanges (null, null, new RectangleF (), null);
2110 Assert.AreEqual (0, regions.Length, "text null/null font");
2111 regions = g.MeasureCharacterRanges (String.Empty, null, new RectangleF (), null);
2112 Assert.AreEqual (0, regions.Length, "text empty/null font");
2118 public void MeasureCharacterRanges_EmptyStringFormat ()
2121 Assert.Ignore ("Couldn't create required font");
2123 using (Bitmap bitmap = new Bitmap (20, 20)) {
2124 using (Graphics g = Graphics.FromImage (bitmap)) {
2125 // string format without character ranges
2126 Region[] regions = g.MeasureCharacterRanges ("Mono", font, new RectangleF (), new StringFormat ());
2127 Assert.AreEqual (0, regions.Length, "empty stringformat");
2133 public void MeasureCharacterRanges_FontNull ()
2135 using (Bitmap bitmap = new Bitmap (20, 20)) {
2136 using (Graphics g = Graphics.FromImage (bitmap)) {
2137 Assert.Throws<ArgumentNullException> (() => g.MeasureCharacterRanges ("a", null, new RectangleF (), null));
2142 [Test] // adapted from bug #78777
2143 public void MeasureCharacterRanges_TwoLines ()
2146 Assert.Ignore ("Couldn't create required font");
2148 string text = "this\nis a test";
2149 CharacterRange[] ranges = new CharacterRange[2];
2150 ranges[0] = new CharacterRange (0, 5);
2151 ranges[1] = new CharacterRange (5, 9);
2153 StringFormat string_format = new StringFormat ();
2154 string_format.FormatFlags = StringFormatFlags.NoClip;
2155 string_format.SetMeasurableCharacterRanges (ranges);
2157 using (Bitmap bitmap = new Bitmap (20, 20)) {
2158 using (Graphics g = Graphics.FromImage (bitmap)) {
2159 SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2160 RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2161 Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2163 Assert.AreEqual (2, regions.Length, "Length");
2164 Assert.AreEqual (regions[0].GetBounds (g).Height, regions[1].GetBounds (g).Height, "Height");
2169 private void MeasureCharacterRanges (string text, int first, int length)
2172 Assert.Ignore ("Couldn't create required font");
2174 CharacterRange[] ranges = new CharacterRange[1];
2175 ranges[0] = new CharacterRange (first, length);
2177 StringFormat string_format = new StringFormat ();
2178 string_format.FormatFlags = StringFormatFlags.NoClip;
2179 string_format.SetMeasurableCharacterRanges (ranges);
2181 using (Bitmap bitmap = new Bitmap (20, 20)) {
2182 using (Graphics g = Graphics.FromImage (bitmap)) {
2183 SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2184 RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2185 g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2191 public void MeasureCharacterRanges_FirstTooFar ()
2193 string text = "this\nis a test";
2194 Assert.Throws<ArgumentException> (() => MeasureCharacterRanges(text, text.Length, 1));
2198 public void MeasureCharacterRanges_LengthTooLong ()
2200 string text = "this\nis a test";
2201 Assert.Throws<ArgumentException> (() => MeasureCharacterRanges(text, 0, text.Length + 1));
2205 public void MeasureCharacterRanges_Prefix ()
2208 Assert.Ignore ("Couldn't create required font");
2210 string text = "Hello &Mono::";
2211 CharacterRange[] ranges = new CharacterRange[1];
2212 ranges[0] = new CharacterRange (5, 4);
2214 StringFormat string_format = new StringFormat ();
2215 string_format.SetMeasurableCharacterRanges (ranges);
2217 using (Bitmap bitmap = new Bitmap (20, 20)) {
2218 using (Graphics g = Graphics.FromImage (bitmap)) {
2219 SizeF size = g.MeasureString (text, font, new Point (0, 0), string_format);
2220 RectangleF layout_rect = new RectangleF (0.0f, 0.0f, size.Width, size.Height);
2222 // here & is part of the measure and visible
2223 string_format.HotkeyPrefix = HotkeyPrefix.None;
2224 Region[] regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2225 RectangleF bounds_none = regions[0].GetBounds (g);
2227 // here & is part of the measure (range) but visible as an underline
2228 string_format.HotkeyPrefix = HotkeyPrefix.Show;
2229 regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2230 RectangleF bounds_show = regions[0].GetBounds (g);
2231 Assert.IsTrue (bounds_show.Width < bounds_none.Width, "Show<None");
2233 // here & is part of the measure (range) but invisible
2234 string_format.HotkeyPrefix = HotkeyPrefix.Hide;
2235 regions = g.MeasureCharacterRanges (text, font, layout_rect, string_format);
2236 RectangleF bounds_hide = regions[0].GetBounds (g);
2237 Assert.AreEqual (bounds_hide.Width, bounds_show.Width, "Hide==None");
2243 public void MeasureCharacterRanges_NullStringFormat ()
2246 Assert.Ignore ("Couldn't create required font");
2248 using (Bitmap bitmap = new Bitmap (20, 20)) {
2249 using (Graphics g = Graphics.FromImage (bitmap)) {
2250 Assert.Throws<ArgumentException> (() => g.MeasureCharacterRanges ("Mono", font, new RectangleF (), null));
2256 [Category ("NotWorking")]
2257 public void MeasureCharacterRanges_StringFormat_Alignment ()
2260 Assert.Ignore ("Couldn't create required font");
2262 string text = "Hello Mono::";
2263 CharacterRange[] ranges = new CharacterRange[1];
2264 ranges[0] = new CharacterRange (5, 4);
2265 StringFormat string_format = new StringFormat ();
2266 string_format.SetMeasurableCharacterRanges (ranges);
2268 using (Bitmap bitmap = new Bitmap (20, 20)) {
2269 using (Graphics g = Graphics.FromImage (bitmap)) {
2270 string_format.Alignment = StringAlignment.Near;
2271 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2272 Assert.AreEqual (1, regions.Length, "Near.Region");
2273 RectangleF near = regions[0].GetBounds (g);
2275 string_format.Alignment = StringAlignment.Center;
2276 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2277 Assert.AreEqual (1, regions.Length, "Center.Region");
2278 RectangleF center = regions[0].GetBounds (g);
2280 string_format.Alignment = StringAlignment.Far;
2281 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2282 Assert.AreEqual (1, regions.Length, "Far.Region");
2283 RectangleF far = regions[0].GetBounds (g);
2285 Assert.IsTrue (near.X < center.X, "near-center/X");
2286 Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y");
2287 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2288 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2290 Assert.IsTrue (center.X < far.X, "center-far/X");
2291 Assert.AreEqual (center.Y, far.Y, "center-far/Y");
2292 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2293 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2299 [Category ("NotWorking")]
2300 public void MeasureCharacterRanges_StringFormat_LineAlignment ()
2303 Assert.Ignore ("Couldn't create required font");
2305 string text = "Hello Mono::";
2306 CharacterRange[] ranges = new CharacterRange[1];
2307 ranges[0] = new CharacterRange (5, 4);
2308 StringFormat string_format = new StringFormat ();
2309 string_format.SetMeasurableCharacterRanges (ranges);
2311 using (Bitmap bitmap = new Bitmap (20, 20)) {
2312 using (Graphics g = Graphics.FromImage (bitmap)) {
2313 string_format.LineAlignment = StringAlignment.Near;
2314 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2315 Assert.AreEqual (1, regions.Length, "Near.Region");
2316 RectangleF near = regions[0].GetBounds (g);
2318 string_format.LineAlignment = StringAlignment.Center;
2319 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2320 Assert.AreEqual (1, regions.Length, "Center.Region");
2321 RectangleF center = regions[0].GetBounds (g);
2323 string_format.LineAlignment = StringAlignment.Far;
2324 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2325 Assert.AreEqual (1, regions.Length, "Far.Region");
2326 RectangleF far = regions[0].GetBounds (g);
2328 Assert.AreEqual (near.X, center.X, 0.1, "near-center/X");
2329 Assert.IsTrue (near.Y < center.Y, "near-center/Y");
2330 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2331 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2333 Assert.AreEqual (center.X, far.X, 0.1, "center-far/X");
2334 Assert.IsTrue (center.Y < far.Y, "center-far/Y");
2335 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2336 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2342 [Category ("NotWorking")]
2343 public void MeasureCharacterRanges_StringFormat_Alignment_DirectionVertical ()
2346 Assert.Ignore ("Couldn't create required font");
2348 string text = "Hello Mono::";
2349 CharacterRange[] ranges = new CharacterRange[1];
2350 ranges[0] = new CharacterRange (5, 4);
2351 StringFormat string_format = new StringFormat ();
2352 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2353 string_format.SetMeasurableCharacterRanges (ranges);
2355 using (Bitmap bitmap = new Bitmap (20, 20)) {
2356 using (Graphics g = Graphics.FromImage (bitmap)) {
2357 string_format.Alignment = StringAlignment.Near;
2358 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2359 Assert.AreEqual (1, regions.Length, "Near.Region");
2360 RectangleF near = regions[0].GetBounds (g);
2362 string_format.Alignment = StringAlignment.Center;
2363 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2364 Assert.AreEqual (1, regions.Length, "Center.Region");
2365 RectangleF center = regions[0].GetBounds (g);
2367 string_format.Alignment = StringAlignment.Far;
2368 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2369 Assert.AreEqual (1, regions.Length, "Far.Region");
2370 RectangleF far = regions[0].GetBounds (g);
2372 Assert.IsTrue (near.X == center.X, "near-center/X"); // ???
2373 Assert.IsTrue (near.Y < center.Y, "near-center/Y");
2374 Assert.IsTrue (near.Width == center.Width, "near-center/Width"); // ???
2375 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2377 Assert.AreEqual (center.X, far.X, 0.1, "center-far/X");
2378 Assert.IsTrue (center.Y < far.Y, "center-far/Y");
2379 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2380 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2386 [Category ("NotWorking")]
2387 public void MeasureCharacterRanges_StringFormat_LineAlignment_DirectionVertical ()
2390 Assert.Ignore ("Couldn't create required font");
2392 string text = "Hello Mono::";
2393 CharacterRange[] ranges = new CharacterRange[1];
2394 ranges[0] = new CharacterRange (5, 4);
2395 StringFormat string_format = new StringFormat ();
2396 string_format.FormatFlags = StringFormatFlags.DirectionVertical;
2397 string_format.SetMeasurableCharacterRanges (ranges);
2399 using (Bitmap bitmap = new Bitmap (20, 20)) {
2400 using (Graphics g = Graphics.FromImage (bitmap)) {
2401 string_format.LineAlignment = StringAlignment.Near;
2402 Region[] regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2403 Assert.AreEqual (1, regions.Length, "Near.Region");
2404 RectangleF near = regions[0].GetBounds (g);
2406 string_format.LineAlignment = StringAlignment.Center;
2407 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2408 Assert.AreEqual (1, regions.Length, "Center.Region");
2409 RectangleF center = regions[0].GetBounds (g);
2411 string_format.LineAlignment = StringAlignment.Far;
2412 regions = g.MeasureCharacterRanges (text, font, new RectangleF (0, 0, 320, 32), string_format);
2413 Assert.AreEqual (1, regions.Length, "Far.Region");
2414 RectangleF far = regions[0].GetBounds (g);
2416 Assert.IsTrue (near.X < center.X, "near-center/X");
2417 Assert.AreEqual (near.Y, center.Y, 0.1, "near-center/Y");
2418 Assert.AreEqual (near.Width, center.Width, 0.1, "near-center/Width");
2419 Assert.AreEqual (near.Height, center.Height, 0.1, "near-center/Height");
2421 Assert.IsTrue (center.X < far.X, "center-far/X");
2422 Assert.AreEqual (center.Y, far.Y, 0.1, "center-far/Y");
2423 Assert.AreEqual (center.Width, far.Width, 0.1, "center-far/Width");
2424 Assert.AreEqual (center.Height, far.Height, 0.1, "center-far/Height");
2429 static CharacterRange [] ranges = new CharacterRange [] {
2430 new CharacterRange (0, 1),
2431 new CharacterRange (1, 1),
2432 new CharacterRange (2, 1)
2435 Region [] Measure (Graphics gfx, RectangleF rect)
2437 using (StringFormat format = StringFormat.GenericTypographic) {
2438 format.SetMeasurableCharacterRanges (ranges);
2440 using (Font font = new Font (FontFamily.GenericSerif, 11.0f)) {
2441 return gfx.MeasureCharacterRanges ("abc", font, rect, format);
2447 public void Measure ()
2449 using (Graphics gfx = Graphics.FromImage (new Bitmap (1, 1))) {
2450 Region [] zero = Measure (gfx, new RectangleF (0, 0, 0, 0));
2451 Assert.AreEqual (3, zero.Length, "zero.Length");
2453 Region [] small = Measure (gfx, new RectangleF (0, 0, 100, 100));
2454 Assert.AreEqual (3, small.Length, "small.Length");
2455 for (int i = 0; i < 3; i++ ) {
2456 RectangleF zb = zero [i].GetBounds (gfx);
2457 RectangleF sb = small [i].GetBounds (gfx);
2458 Assert.AreEqual (sb.X, zb.X, "sx" + i.ToString ());
2459 Assert.AreEqual (sb.Y, zb.Y, "sy" + i.ToString ());
2460 Assert.AreEqual (sb.Width, zb.Width, "sw" + i.ToString ());
2461 Assert.AreEqual (sb.Height, zb.Height, "sh" + i.ToString ());
2464 Region [] max = Measure (gfx, new RectangleF (0, 0, Single.MaxValue, Single.MaxValue));
2465 Assert.AreEqual (3, max.Length, "empty.Length");
2466 for (int i = 0; i < 3; i++) {
2467 RectangleF zb = zero [i].GetBounds (gfx);
2468 RectangleF mb = max [i].GetBounds (gfx);
2469 Assert.AreEqual (mb.X, zb.X, "mx" + i.ToString ());
2470 Assert.AreEqual (mb.Y, zb.Y, "my" + i.ToString ());
2471 Assert.AreEqual (mb.Width, zb.Width, "mw" + i.ToString ());
2472 Assert.AreEqual (mb.Height, zb.Height, "mh" + i.ToString ());
2478 public void MeasureLimits ()
2480 using (Graphics gfx = Graphics.FromImage (new Bitmap (1, 1))) {
2481 Region [] min = Measure (gfx, new RectangleF (0, 0, Single.MinValue, Single.MinValue));
2482 Assert.AreEqual (3, min.Length, "origin.Length");
2483 for (int i = 0; i < 3; i++) {
2484 RectangleF mb = min [i].GetBounds (gfx);
2485 Assert.AreEqual (-4194304.0f, mb.X, "minx" + i.ToString ());
2486 Assert.AreEqual (-4194304.0f, mb.Y, "miny" + i.ToString ());
2487 Assert.AreEqual (8388608.0f, mb.Width, "minw" + i.ToString ());
2488 Assert.AreEqual (8388608.0f, mb.Height, "minh" + i.ToString ());
2491 Region [] neg = Measure (gfx, new RectangleF (0, 0, -20, -20));
2492 Assert.AreEqual (3, neg.Length, "neg.Length");
2493 for (int i = 0; i < 3; i++) {
2494 RectangleF mb = neg [i].GetBounds (gfx);
2495 Assert.AreEqual (-4194304.0f, mb.X, "minx" + i.ToString ());
2496 Assert.AreEqual (-4194304.0f, mb.Y, "miny" + i.ToString ());
2497 Assert.AreEqual (8388608.0f, mb.Width, "minw" + i.ToString ());
2498 Assert.AreEqual (8388608.0f, mb.Height, "minh" + i.ToString ());
2504 public void DrawString_EndlessLoop_Bug77699 ()
2507 Assert.Ignore ("Couldn't create required font");
2509 using (Bitmap bitmap = new Bitmap (20, 20)) {
2510 using (Graphics g = Graphics.FromImage (bitmap)) {
2511 Rectangle rect = Rectangle.Empty;
2512 rect.Location = new Point (10, 10);
2513 rect.Size = new Size (1, 20);
2514 StringFormat fmt = new StringFormat ();
2515 fmt.Alignment = StringAlignment.Center;
2516 fmt.LineAlignment = StringAlignment.Center;
2517 fmt.FormatFlags = StringFormatFlags.NoWrap;
2518 fmt.Trimming = StringTrimming.EllipsisWord;
2519 g.DrawString ("Test String", font, Brushes.Black, rect, fmt);
2525 public void DrawString_EndlessLoop_Wrapping ()
2528 Assert.Ignore ("Couldn't create required font");
2530 using (Bitmap bitmap = new Bitmap (20, 20)) {
2531 using (Graphics g = Graphics.FromImage (bitmap)) {
2532 Rectangle rect = Rectangle.Empty;
2533 rect.Location = new Point (10, 10);
2534 rect.Size = new Size (1, 20);
2535 StringFormat fmt = new StringFormat ();
2536 fmt.Alignment = StringAlignment.Center;
2537 fmt.LineAlignment = StringAlignment.Center;
2538 fmt.Trimming = StringTrimming.EllipsisWord;
2539 g.DrawString ("Test String", font, Brushes.Black, rect, fmt);
2545 public void MeasureString_Wrapping_Dots ()
2547 HostIgnoreList.CheckTest ("MonoTests.System.Drawing.GraphicsTest.MeasureString_Wrapping_Dots");
2550 Assert.Ignore ("Couldn't create required font");
2552 string text = "this is really long text........................................... with a lot o periods.";
2553 using (Bitmap bitmap = new Bitmap (20, 20)) {
2554 using (Graphics g = Graphics.FromImage (bitmap)) {
2555 using (StringFormat format = new StringFormat ()) {
2556 format.Alignment = StringAlignment.Center;
2557 SizeF sz = g.MeasureString (text, font, 80, format);
2558 Assert.IsTrue (sz.Width < 80, "Width");
2559 Assert.IsTrue (sz.Height > font.Height * 2, "Height");
2566 public void GetReleaseHdcInternal ()
2568 using (Bitmap b = new Bitmap (10, 10)) {
2569 using (Graphics g = Graphics.FromImage (b)) {
2570 IntPtr hdc1 = g.GetHdc ();
2571 g.ReleaseHdcInternal (hdc1);
2572 IntPtr hdc2 = g.GetHdc ();
2573 g.ReleaseHdcInternal (hdc2);
2574 Assert.AreEqual (hdc1, hdc2, "hdc");
2580 public void ReleaseHdcInternal_IntPtrZero ()
2582 using (Bitmap b = new Bitmap (10, 10)) {
2583 using (Graphics g = Graphics.FromImage (b)) {
2584 Assert.Throws<ArgumentException> (() => g.ReleaseHdcInternal (IntPtr.Zero));
2590 public void ReleaseHdcInternal_TwoTimes ()
2592 using (Bitmap b = new Bitmap (10, 10)) {
2593 using (Graphics g = Graphics.FromImage (b)) {
2594 IntPtr hdc = g.GetHdc ();
2595 g.ReleaseHdcInternal (hdc);
2596 Assert.Throws<ArgumentException> (() => g.ReleaseHdcInternal (hdc));
2601 public void TestReleaseHdc ()
2603 using (Bitmap b = new Bitmap (10, 10)) {
2604 using (Graphics g = Graphics.FromImage (b)) {
2605 IntPtr hdc1 = g.GetHdc ();
2607 IntPtr hdc2 = g.GetHdc ();
2609 Assert.AreEqual (hdc1, hdc2, "hdc");
2615 public void TestReleaseHdcException ()
2617 using (Bitmap b = new Bitmap (10, 10)) {
2618 using (Graphics g = Graphics.FromImage (b)) {
2619 Assert.Throws<ArgumentException> (() => g.ReleaseHdc ());
2625 public void TestReleaseHdcException2 ()
2627 using (Bitmap b = new Bitmap (10, 10)) {
2628 using (Graphics g = Graphics.FromImage (b)) {
2631 Assert.Throws<ArgumentException> (() => g.ReleaseHdc ());
2636 public void VisibleClipBound ()
2639 using (Bitmap bmp = new Bitmap (100, 100)) {
2640 using (Graphics g = Graphics.FromImage (bmp)) {
2641 RectangleF noclip = g.VisibleClipBounds;
2642 Assert.AreEqual (0, noclip.X, "noclip.X");
2643 Assert.AreEqual (0, noclip.Y, "noclip.Y");
2644 Assert.AreEqual (100, noclip.Width, "noclip.Width");
2645 Assert.AreEqual (100, noclip.Height, "noclip.Height");
2647 // note: libgdiplus regions are precise to multiple of multiple of 8
2648 g.Clip = new Region (new RectangleF (0, 0, 32, 32));
2649 RectangleF clip = g.VisibleClipBounds;
2650 Assert.AreEqual (0, clip.X, "clip.X");
2651 Assert.AreEqual (0, clip.Y, "clip.Y");
2652 Assert.AreEqual (32, clip.Width, "clip.Width");
2653 Assert.AreEqual (32, clip.Height, "clip.Height");
2655 g.RotateTransform (90);
2656 RectangleF rotclip = g.VisibleClipBounds;
2657 Assert.AreEqual (0, rotclip.X, "rotclip.X");
2658 Assert.AreEqual (-32, rotclip.Y, "rotclip.Y");
2659 Assert.AreEqual (32, rotclip.Width, "rotclip.Width");
2660 Assert.AreEqual (32, rotclip.Height, "rotclip.Height");
2666 public void VisibleClipBound_BigClip ()
2668 using (Bitmap bmp = new Bitmap (100, 100)) {
2669 using (Graphics g = Graphics.FromImage (bmp)) {
2670 RectangleF noclip = g.VisibleClipBounds;
2671 Assert.AreEqual (0, noclip.X, "noclip.X");
2672 Assert.AreEqual (0, noclip.Y, "noclip.Y");
2673 Assert.AreEqual (100, noclip.Width, "noclip.Width");
2674 Assert.AreEqual (100, noclip.Height, "noclip.Height");
2676 // clip is larger than bitmap
2677 g.Clip = new Region (new RectangleF (0, 0, 200, 200));
2678 RectangleF clipbound = g.ClipBounds;
2679 Assert.AreEqual (0, clipbound.X, "clipbound.X");
2680 Assert.AreEqual (0, clipbound.Y, "clipbound.Y");
2681 Assert.AreEqual (200, clipbound.Width, "clipbound.Width");
2682 Assert.AreEqual (200, clipbound.Height, "clipbound.Height");
2684 RectangleF clip = g.VisibleClipBounds;
2685 Assert.AreEqual (0, clip.X, "clip.X");
2686 Assert.AreEqual (0, clip.Y, "clip.Y");
2687 Assert.AreEqual (100, clip.Width, "clip.Width");
2688 Assert.AreEqual (100, clip.Height, "clip.Height");
2690 g.RotateTransform (90);
2691 RectangleF rotclipbound = g.ClipBounds;
2692 Assert.AreEqual (0, rotclipbound.X, "rotclipbound.X");
2693 Assert.AreEqual (-200, rotclipbound.Y, "rotclipbound.Y");
2694 Assert.AreEqual (200, rotclipbound.Width, "rotclipbound.Width");
2695 Assert.AreEqual (200, rotclipbound.Height, "rotclipbound.Height");
2697 RectangleF rotclip = g.VisibleClipBounds;
2698 Assert.AreEqual (0, rotclip.X, "rotclip.X");
2699 Assert.AreEqual (-100, rotclip.Y, "rotclip.Y");
2700 Assert.AreEqual (100, rotclip.Width, "rotclip.Width");
2701 Assert.AreEqual (100, rotclip.Height, "rotclip.Height");
2707 public void Rotate ()
2709 using (Bitmap bmp = new Bitmap (100, 50)) {
2710 using (Graphics g = Graphics.FromImage (bmp)) {
2711 RectangleF vcb = g.VisibleClipBounds;
2712 Assert.AreEqual (0, vcb.X, "vcb.X");
2713 Assert.AreEqual (0, vcb.Y, "vcb.Y");
2714 Assert.AreEqual (100, vcb.Width, "vcb.Width");
2715 Assert.AreEqual (50, vcb.Height, "vcb.Height");
2717 g.RotateTransform (90);
2718 RectangleF rvcb = g.VisibleClipBounds;
2719 Assert.AreEqual (0, rvcb.X, "rvcb.X");
2720 Assert.AreEqual (-100, rvcb.Y, "rvcb.Y");
2721 Assert.AreEqual (50.0f, rvcb.Width, 0.0001, "rvcb.Width");
2722 Assert.AreEqual (100, rvcb.Height, "rvcb.Height");
2728 public void Scale ()
2730 using (Bitmap bmp = new Bitmap (100, 50)) {
2731 using (Graphics g = Graphics.FromImage (bmp)) {
2732 RectangleF vcb = g.VisibleClipBounds;
2733 Assert.AreEqual (0, vcb.X, "vcb.X");
2734 Assert.AreEqual (0, vcb.Y, "vcb.Y");
2735 Assert.AreEqual (100, vcb.Width, "vcb.Width");
2736 Assert.AreEqual (50, vcb.Height, "vcb.Height");
2738 g.ScaleTransform (2, 0.5f);
2739 RectangleF svcb = g.VisibleClipBounds;
2740 Assert.AreEqual (0, svcb.X, "svcb.X");
2741 Assert.AreEqual (0, svcb.Y, "svcb.Y");
2742 Assert.AreEqual (50, svcb.Width, "svcb.Width");
2743 Assert.AreEqual (100, svcb.Height, "svcb.Height");
2749 public void Translate ()
2751 using (Bitmap bmp = new Bitmap (100, 50)) {
2752 using (Graphics g = Graphics.FromImage (bmp)) {
2753 RectangleF vcb = g.VisibleClipBounds;
2754 Assert.AreEqual (0, vcb.X, "vcb.X");
2755 Assert.AreEqual (0, vcb.Y, "vcb.Y");
2756 Assert.AreEqual (100, vcb.Width, "vcb.Width");
2757 Assert.AreEqual (50, vcb.Height, "vcb.Height");
2759 g.TranslateTransform (-25, 25);
2760 RectangleF tvcb = g.VisibleClipBounds;
2761 Assert.AreEqual (25, tvcb.X, "tvcb.X");
2762 Assert.AreEqual (-25, tvcb.Y, "tvcb.Y");
2763 Assert.AreEqual (100, tvcb.Width, "tvcb.Width");
2764 Assert.AreEqual (50, tvcb.Height, "tvcb.Height");
2770 public void DrawIcon_NullRectangle ()
2772 using (Bitmap bmp = new Bitmap (40, 40)) {
2773 using (Graphics g = Graphics.FromImage (bmp)) {
2774 Assert.Throws<ArgumentNullException> (() => g.DrawIcon (null, new Rectangle (0, 0, 32, 32)));
2780 public void DrawIcon_IconRectangle ()
2782 using (Bitmap bmp = new Bitmap (40, 40)) {
2783 using (Graphics g = Graphics.FromImage (bmp)) {
2784 g.DrawIcon (SystemIcons.Application, new Rectangle (0, 0, 40, 20));
2785 // Rectangle is empty when X, Y, Width and Height == 0
2786 // (yep X and Y too, RectangleF only checks for Width and Height)
2787 g.DrawIcon (SystemIcons.Asterisk, new Rectangle (0, 0, 0, 0));
2788 // so this one is half-empty ;-)
2789 g.DrawIcon (SystemIcons.Error, new Rectangle (20, 40, 0, 0));
2790 // negative width or height isn't empty (for Rectangle)
2791 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (10, 20, -1, 0));
2792 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (20, 10, 0, -1));
2798 public void DrawIcon_NullIntInt ()
2800 using (Bitmap bmp = new Bitmap (40, 40)) {
2801 using (Graphics g = Graphics.FromImage (bmp)) {
2802 Assert.Throws<ArgumentNullException> (() => g.DrawIcon (null, 4, 2));
2808 public void DrawIcon_IconIntInt ()
2810 using (Bitmap bmp = new Bitmap (40, 40)) {
2811 using (Graphics g = Graphics.FromImage (bmp)) {
2812 g.DrawIcon (SystemIcons.Exclamation, 4, 2);
2813 g.DrawIcon (SystemIcons.Hand, 0, 0);
2819 public void DrawIconUnstretched_NullRectangle ()
2821 using (Bitmap bmp = new Bitmap (40, 40)) {
2822 using (Graphics g = Graphics.FromImage (bmp)) {
2823 Assert.Throws<ArgumentNullException> (() => g.DrawIconUnstretched (null, new Rectangle (0, 0, 40, 20)));
2829 public void DrawIconUnstretched_IconRectangle ()
2831 using (Bitmap bmp = new Bitmap (40, 40)) {
2832 using (Graphics g = Graphics.FromImage (bmp)) {
2833 g.DrawIconUnstretched (SystemIcons.Information, new Rectangle (0, 0, 40, 20));
2834 // Rectangle is empty when X, Y, Width and Height == 0
2835 // (yep X and Y too, RectangleF only checks for Width and Height)
2836 g.DrawIconUnstretched (SystemIcons.Question, new Rectangle (0, 0, 0, 0));
2837 // so this one is half-empty ;-)
2838 g.DrawIconUnstretched (SystemIcons.Warning, new Rectangle (20, 40, 0, 0));
2839 // negative width or height isn't empty (for Rectangle)
2840 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (10, 20, -1, 0));
2841 g.DrawIconUnstretched (SystemIcons.WinLogo, new Rectangle (20, 10, 0, -1));
2847 public void DrawImage_NullRectangleF ()
2849 using (Bitmap bmp = new Bitmap (40, 40)) {
2850 using (Graphics g = Graphics.FromImage (bmp)) {
2851 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new RectangleF (0, 0, 0, 0)));
2857 public void DrawImage_ImageRectangleF ()
2859 using (Bitmap bmp = new Bitmap (40, 40)) {
2860 using (Graphics g = Graphics.FromImage (bmp)) {
2861 g.DrawImage (bmp, new RectangleF (0, 0, 0, 0));
2862 g.DrawImage (bmp, new RectangleF (20, 40, 0, 0));
2863 g.DrawImage (bmp, new RectangleF (10, 20, -1, 0));
2864 g.DrawImage (bmp, new RectangleF (20, 10, 0, -1));
2870 public void DrawImage_NullPointF ()
2872 using (Bitmap bmp = new Bitmap (40, 40)) {
2873 using (Graphics g = Graphics.FromImage (bmp)) {
2874 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new PointF (0, 0)));
2880 public void DrawImage_ImagePointF ()
2882 using (Bitmap bmp = new Bitmap (40, 40)) {
2883 using (Graphics g = Graphics.FromImage (bmp)) {
2884 g.DrawImage (bmp, new PointF (0, 0));
2890 public void DrawImage_NullPointFArray ()
2892 using (Bitmap bmp = new Bitmap (40, 40)) {
2893 using (Graphics g = Graphics.FromImage (bmp)) {
2894 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new PointF[0]));
2900 public void DrawImage_ImagePointFArrayNull ()
2902 using (Bitmap bmp = new Bitmap (40, 40)) {
2903 using (Graphics g = Graphics.FromImage (bmp)) {
2904 Assert.Throws<ArgumentNullException> (() => g.DrawImage (bmp, (PointF[]) null));
2910 public void DrawImage_ImagePointFArrayEmpty ()
2912 using (Bitmap bmp = new Bitmap (40, 40)) {
2913 using (Graphics g = Graphics.FromImage (bmp)) {
2914 Assert.Throws<ArgumentException> (() => g.DrawImage (bmp, new PointF[0]));
2920 public void DrawImage_ImagePointFArray ()
2922 using (Bitmap bmp = new Bitmap (40, 40)) {
2923 using (Graphics g = Graphics.FromImage (bmp)) {
2924 g.DrawImage (bmp, new PointF[] {
2925 new PointF (0, 0), new PointF (1, 1), new PointF (2, 2) });
2931 public void DrawImage_NullRectangle ()
2933 using (Bitmap bmp = new Bitmap (40, 40)) {
2934 using (Graphics g = Graphics.FromImage (bmp)) {
2935 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Rectangle (0, 0, 0, 0)));
2941 public void DrawImage_ImageRectangle ()
2943 using (Bitmap bmp = new Bitmap (40, 40)) {
2944 using (Graphics g = Graphics.FromImage (bmp)) {
2945 // Rectangle is empty when X, Y, Width and Height == 0
2946 // (yep X and Y too, RectangleF only checks for Width and Height)
2947 g.DrawImage (bmp, new Rectangle (0, 0, 0, 0));
2948 // so this one is half-empty ;-)
2949 g.DrawImage (bmp, new Rectangle (20, 40, 0, 0));
2950 // negative width or height isn't empty (for Rectangle)
2951 g.DrawImage (bmp, new Rectangle (10, 20, -1, 0));
2952 g.DrawImage (bmp, new Rectangle (20, 10, 0, -1));
2958 public void DrawImage_NullPoint ()
2960 using (Bitmap bmp = new Bitmap (40, 40)) {
2961 using (Graphics g = Graphics.FromImage (bmp)) {
2962 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Point (0, 0)));
2968 public void DrawImage_ImagePoint ()
2970 using (Bitmap bmp = new Bitmap (40, 40)) {
2971 using (Graphics g = Graphics.FromImage (bmp)) {
2972 g.DrawImage (bmp, new Point (0, 0));
2978 public void DrawImage_NullPointArray ()
2980 using (Bitmap bmp = new Bitmap (40, 40)) {
2981 using (Graphics g = Graphics.FromImage (bmp)) {
2982 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Point[0]));
2988 public void DrawImage_ImagePointArrayNull ()
2990 using (Bitmap bmp = new Bitmap (40, 40)) {
2991 using (Graphics g = Graphics.FromImage (bmp)) {
2992 Assert.Throws<ArgumentNullException> (() => g.DrawImage (bmp, (Point[]) null));
2998 public void DrawImage_ImagePointArrayEmpty ()
3000 using (Bitmap bmp = new Bitmap (40, 40)) {
3001 using (Graphics g = Graphics.FromImage (bmp)) {
3002 Assert.Throws<ArgumentException> (() => g.DrawImage (bmp, new Point[0]));
3008 public void DrawImage_ImagePointArray ()
3010 using (Bitmap bmp = new Bitmap (40, 40)) {
3011 using (Graphics g = Graphics.FromImage (bmp)) {
3012 g.DrawImage (bmp, new Point[] {
3013 new Point (0, 0), new Point (1, 1), new Point (2, 2) });
3019 public void DrawImage_NullIntInt ()
3021 using (Bitmap bmp = new Bitmap (40, 40)) {
3022 using (Graphics g = Graphics.FromImage (bmp)) {
3023 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, Int32.MaxValue, Int32.MinValue));
3029 public void DrawImage_ImageIntInt_Overflow ()
3031 using (Bitmap bmp = new Bitmap (40, 40)) {
3032 using (Graphics g = Graphics.FromImage (bmp)) {
3033 Assert.Throws<OverflowException> (() => g.DrawImage (bmp, Int32.MaxValue, Int32.MinValue));
3039 public void DrawImage_ImageIntInt ()
3041 using (Bitmap bmp = new Bitmap (40, 40)) {
3042 using (Graphics g = Graphics.FromImage (bmp)) {
3043 g.DrawImage (bmp, -40, -40);
3049 public void DrawImage_NullFloat ()
3051 using (Bitmap bmp = new Bitmap (40, 40)) {
3052 using (Graphics g = Graphics.FromImage (bmp)) {
3053 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, Single.MaxValue, Single.MinValue));
3059 public void DrawImage_ImageFloatFloat_Overflow ()
3061 using (Bitmap bmp = new Bitmap (40, 40)) {
3062 using (Graphics g = Graphics.FromImage (bmp)) {
3063 Assert.Throws<OverflowException> (() => g.DrawImage (bmp, Single.MaxValue, Single.MinValue));
3069 public void DrawImage_ImageFloatFloat ()
3071 using (Bitmap bmp = new Bitmap (40, 40)) {
3072 using (Graphics g = Graphics.FromImage (bmp)) {
3073 g.DrawImage (bmp, -40.0f, -40.0f);
3079 public void DrawImage_NullRectangleRectangleGraphicsUnit ()
3081 using (Bitmap bmp = new Bitmap (40, 40)) {
3082 using (Graphics g = Graphics.FromImage (bmp)) {
3083 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, new Rectangle (), new Rectangle (), GraphicsUnit.Display));
3088 private void DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit unit)
3090 using (Bitmap bmp = new Bitmap (40, 40)) {
3091 using (Graphics g = Graphics.FromImage (bmp)) {
3092 Rectangle r = new Rectangle (0, 0, 40, 40);
3093 g.DrawImage (bmp, r, r, unit);
3099 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Display ()
3101 Assert.Throws<ArgumentException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Display));
3105 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Document ()
3107 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Document));
3111 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Inch ()
3113 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit(GraphicsUnit.Inch));
3117 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Millimeter ()
3119 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Millimeter));
3123 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Pixel ()
3126 DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Pixel);
3130 public void DrawImage_ImageRectangleRectangleGraphicsUnit_Point ()
3132 Assert.Throws<NotImplementedException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.Point));
3136 public void DrawImage_ImageRectangleRectangleGraphicsUnit_World ()
3138 Assert.Throws<ArgumentException> (() => DrawImage_ImageRectangleRectangleGraphicsUnit (GraphicsUnit.World));
3142 public void DrawImage_NullPointRectangleGraphicsUnit ()
3144 Rectangle r = new Rectangle (1, 2, 3, 4);
3145 Point[] pts = new Point[3] { new Point (1, 1), new Point (2, 2), new Point (3, 3) };
3146 using (Bitmap bmp = new Bitmap (40, 40)) {
3147 using (Graphics g = Graphics.FromImage (bmp)) {
3148 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, pts, r, GraphicsUnit.Pixel));
3153 private void DrawImage_ImagePointRectangleGraphicsUnit (Point[] pts)
3155 Rectangle r = new Rectangle (1, 2, 3, 4);
3156 using (Bitmap bmp = new Bitmap (40, 40)) {
3157 using (Graphics g = Graphics.FromImage (bmp)) {
3158 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel);
3164 public void DrawImage_ImageNullRectangleGraphicsUnit ()
3166 Assert.Throws<ArgumentNullException> (() => DrawImage_ImagePointRectangleGraphicsUnit (null));
3170 public void DrawImage_ImagePoint0RectangleGraphicsUnit ()
3172 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[0]));
3176 public void DrawImage_ImagePoint1RectangleGraphicsUnit ()
3178 Point p = new Point (1, 1);
3179 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[1] { p }));
3183 public void DrawImage_ImagePoint2RectangleGraphicsUnit ()
3185 Point p = new Point (1, 1);
3186 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[2] { p, p }));
3190 public void DrawImage_ImagePoint3RectangleGraphicsUnit ()
3192 Point p = new Point (1, 1);
3193 DrawImage_ImagePointRectangleGraphicsUnit (new Point[3] { p, p, p });
3197 public void DrawImage_ImagePoint4RectangleGraphicsUnit ()
3199 Point p = new Point (1, 1);
3200 Assert.Throws<NotImplementedException> (() => DrawImage_ImagePointRectangleGraphicsUnit (new Point[4] { p, p, p, p }));
3204 public void DrawImage_NullPointFRectangleGraphicsUnit ()
3206 Rectangle r = new Rectangle (1, 2, 3, 4);
3207 PointF[] pts = new PointF[3] { new PointF (1, 1), new PointF (2, 2), new PointF (3, 3) };
3208 using (Bitmap bmp = new Bitmap (40, 40)) {
3209 using (Graphics g = Graphics.FromImage (bmp)) {
3210 Assert.Throws<ArgumentNullException> (() => g.DrawImage (null, pts, r, GraphicsUnit.Pixel));
3215 private void DrawImage_ImagePointFRectangleGraphicsUnit (PointF[] pts)
3217 Rectangle r = new Rectangle (1, 2, 3, 4);
3218 using (Bitmap bmp = new Bitmap (40, 40)) {
3219 using (Graphics g = Graphics.FromImage (bmp)) {
3220 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel);
3226 public void DrawImage_ImageNullFRectangleGraphicsUnit ()
3228 Assert.Throws<ArgumentNullException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (null));
3232 public void DrawImage_ImagePointF0RectangleGraphicsUnit ()
3234 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[0]));
3238 public void DrawImage_ImagePointF1RectangleGraphicsUnit ()
3240 PointF p = new PointF (1, 1);
3241 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[1] { p }));
3245 public void DrawImage_ImagePointF2RectangleGraphicsUnit ()
3247 PointF p = new PointF (1, 1);
3248 Assert.Throws<ArgumentException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[2] { p, p }));
3252 public void DrawImage_ImagePointF3RectangleGraphicsUnit ()
3254 PointF p = new PointF (1, 1);
3255 DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[3] { p, p, p });
3259 public void DrawImage_ImagePointF4RectangleGraphicsUnit ()
3261 PointF p = new PointF (1, 1);
3262 Assert.Throws<NotImplementedException> (() => DrawImage_ImagePointFRectangleGraphicsUnit (new PointF[4] { p, p, p, p }));
3266 public void DrawImage_ImagePointRectangleGraphicsUnitNull ()
3268 Point p = new Point (1, 1);
3269 Point[] pts = new Point[3] { p, p, p };
3270 Rectangle r = new Rectangle (1, 2, 3, 4);
3271 using (Bitmap bmp = new Bitmap (40, 40)) {
3272 using (Graphics g = Graphics.FromImage (bmp)) {
3273 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel, null);
3279 public void DrawImage_ImagePointRectangleGraphicsUnitAttributes ()
3281 Point p = new Point (1, 1);
3282 Point[] pts = new Point[3] { p, p, p };
3283 Rectangle r = new Rectangle (1, 2, 3, 4);
3284 using (Bitmap bmp = new Bitmap (40, 40)) {
3285 using (Graphics g = Graphics.FromImage (bmp)) {
3286 ImageAttributes ia = new ImageAttributes ();
3287 g.DrawImage (bmp, pts, r, GraphicsUnit.Pixel, ia);
3293 public void DrawImageUnscaled_NullPoint ()
3295 using (Bitmap bmp = new Bitmap (40, 40)) {
3296 using (Graphics g = Graphics.FromImage (bmp)) {
3297 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, new Point (0, 0)));
3303 public void DrawImageUnscaled_ImagePoint ()
3305 using (Bitmap bmp = new Bitmap (40, 40)) {
3306 using (Graphics g = Graphics.FromImage (bmp)) {
3307 g.DrawImageUnscaled (bmp, new Point (0, 0));
3313 public void DrawImageUnscaled_NullRectangle ()
3315 using (Bitmap bmp = new Bitmap (40, 40)) {
3316 using (Graphics g = Graphics.FromImage (bmp)) {
3317 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, new Rectangle (0, 0, -1, -1)));
3323 public void DrawImageUnscaled_ImageRectangle ()
3325 using (Bitmap bmp = new Bitmap (40, 40)) {
3326 using (Graphics g = Graphics.FromImage (bmp)) {
3327 g.DrawImageUnscaled (bmp, new Rectangle (0, 0, -1, -1));
3333 public void DrawImageUnscaled_NullIntInt ()
3335 using (Bitmap bmp = new Bitmap (40, 40)) {
3336 using (Graphics g = Graphics.FromImage (bmp)) {
3337 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, 0, 0));
3343 public void DrawImageUnscaled_ImageIntInt ()
3345 using (Bitmap bmp = new Bitmap (40, 40)) {
3346 using (Graphics g = Graphics.FromImage (bmp)) {
3347 g.DrawImageUnscaled (bmp, 0, 0);
3353 public void DrawImageUnscaled_NullIntIntIntInt ()
3355 using (Bitmap bmp = new Bitmap (40, 40)) {
3356 using (Graphics g = Graphics.FromImage (bmp)) {
3357 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaled (null, 0, 0, -1, -1));
3363 public void DrawImageUnscaled_ImageIntIntIntInt ()
3365 using (Bitmap bmp = new Bitmap (40, 40)) {
3366 using (Graphics g = Graphics.FromImage (bmp)) {
3367 g.DrawImageUnscaled (bmp, 0, 0, -1, -1);
3372 public void DrawImageUnscaledAndClipped_Null ()
3374 using (Bitmap bmp = new Bitmap (40, 40)) {
3375 using (Graphics g = Graphics.FromImage (bmp)) {
3376 Assert.Throws<ArgumentNullException> (() => g.DrawImageUnscaledAndClipped (null, new Rectangle (0, 0, 0, 0)));
3382 public void DrawImageUnscaledAndClipped ()
3384 using (Bitmap bmp = new Bitmap (40, 40)) {
3385 using (Graphics g = Graphics.FromImage (bmp)) {
3386 // Rectangle is empty when X, Y, Width and Height == 0
3387 // (yep X and Y too, RectangleF only checks for Width and Height)
3388 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 0, 0));
3389 // so this one is half-empty ;-)
3390 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (20, 40, 0, 0));
3391 // negative width or height isn't empty (for Rectangle)
3392 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (10, 20, -1, 0));
3393 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (20, 10, 0, -1));
3395 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 10, 20));
3396 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 40, 10));
3397 g.DrawImageUnscaledAndClipped (bmp, new Rectangle (0, 0, 80, 20));
3403 public void DrawPath_Pen_Null ()
3405 using (Bitmap bmp = new Bitmap (20, 20)) {
3406 using (Graphics g = Graphics.FromImage (bmp)) {
3407 using (GraphicsPath path = new GraphicsPath ()) {
3408 Assert.Throws<ArgumentNullException> (() => g.DrawPath (null, path));
3415 public void DrawPath_Path_Null ()
3417 using (Bitmap bmp = new Bitmap (20, 20)) {
3418 using (Graphics g = Graphics.FromImage (bmp)) {
3419 Assert.Throws<ArgumentNullException> (() => g.DrawPath (Pens.Black, null));
3425 public void DrawPath_82202 ()
3427 // based on test case from bug #82202
3428 using (Bitmap bmp = new Bitmap (20, 20)) {
3429 using (Graphics g = Graphics.FromImage (bmp)) {
3430 using (GraphicsPath path = new GraphicsPath ()) {
3432 Rectangle baserect = new Rectangle (0, 0, 19, 19);
3433 Rectangle arcrect = new Rectangle (baserect.Location, new Size (d, d));
3435 path.AddArc (arcrect, 180, 90);
3436 arcrect.X = baserect.Right - d;
3437 path.AddArc (arcrect, 270, 90);
3438 arcrect.Y = baserect.Bottom - d;
3439 path.AddArc (arcrect, 0, 90);
3440 arcrect.X = baserect.Left;
3441 path.AddArc (arcrect, 90, 90);
3442 path.CloseFigure ();
3443 g.Clear (Color.White);
3444 g.DrawPath (Pens.SteelBlue, path);
3446 Assert.AreEqual (-12156236, bmp.GetPixel (0, 9).ToArgb (), "0,9");
3447 Assert.AreEqual (-1, bmp.GetPixel (1, 9).ToArgb (), "1,9");
3454 public void FillPath_Brush_Null ()
3456 using (Bitmap bmp = new Bitmap (20, 20)) {
3457 using (Graphics g = Graphics.FromImage (bmp)) {
3458 using (GraphicsPath path = new GraphicsPath ()) {
3459 Assert.Throws<ArgumentNullException> (() => g.FillPath (null, path));
3466 public void FillPath_Path_Null ()
3468 using (Bitmap bmp = new Bitmap (20, 20)) {
3469 using (Graphics g = Graphics.FromImage (bmp)) {
3470 Assert.Throws<ArgumentNullException> (() => g.FillPath (Brushes.Black, null));
3476 public void FillPath_82202 ()
3478 // based on test case from bug #82202
3479 using (Bitmap bmp = new Bitmap (20, 20)) {
3480 using (Graphics g = Graphics.FromImage (bmp)) {
3481 using (GraphicsPath path = new GraphicsPath ()) {
3483 Rectangle baserect = new Rectangle (0, 0, 19, 19);
3484 Rectangle arcrect = new Rectangle (baserect.Location, new Size (d, d));
3486 path.AddArc (arcrect, 180, 90);
3487 arcrect.X = baserect.Right - d;
3488 path.AddArc (arcrect, 270, 90);
3489 arcrect.Y = baserect.Bottom - d;
3490 path.AddArc (arcrect, 0, 90);
3491 arcrect.X = baserect.Left;
3492 path.AddArc (arcrect, 90, 90);
3493 path.CloseFigure ();
3494 g.Clear (Color.White);
3495 g.FillPath (Brushes.SteelBlue, path);
3497 Assert.AreEqual (-12156236, bmp.GetPixel (0, 9).ToArgb (), "0,9");
3498 Assert.AreEqual (-12156236, bmp.GetPixel (1, 9).ToArgb (), "1,9");
3505 public void TransformPoints_349800 ()
3507 using (Bitmap bmp = new Bitmap (10, 10)) {
3508 using (Graphics g = Graphics.FromImage (bmp)) {
3509 Point [] pts = new Point [5];
3510 PointF [] ptf = new PointF [5];
3511 for (int i = 0; i < 5; i++) {
3512 pts [i] = new Point (i, i);
3513 ptf [i] = new PointF (i, i);
3516 g.TransformPoints (CoordinateSpace.Page, CoordinateSpace.Device, pts);
3517 g.TransformPoints (CoordinateSpace.Page, CoordinateSpace.Device, ptf);
3519 for (int i = 0; i < 5; i++) {
3520 Assert.AreEqual (i, pts [i].X, "Point.X " + i.ToString ());
3521 Assert.AreEqual (i, pts [i].Y, "Point.Y " + i.ToString ());
3522 Assert.AreEqual (i, ptf [i].X, "PointF.X " + i.ToString ());
3523 Assert.AreEqual (i, ptf [i].Y, "PointF.Y " + i.ToString ());
3530 public void Dpi_556181 ()
3533 using (Bitmap bmp = new Bitmap (10, 10)) {
3534 using (Graphics g = Graphics.FromImage (bmp)) {
3538 bmp.SetResolution (x, y);
3539 using (Graphics g = Graphics.FromImage (bmp)) {
3540 Assert.AreEqual (x, g.DpiX, "DpiX");
3541 Assert.AreEqual (y, g.DpiY, "DpiY");
3548 public class GraphicsFullTrustTest {
3550 // note: this test would fail, on ReleaseHdc, without fulltrust
3551 // i.e. it's a demand and not a linkdemand
3553 public void GetReleaseHdc ()
3555 using (Bitmap b = new Bitmap (100, 100)) {
3556 using (Graphics g = Graphics.FromImage (b)) {
3557 IntPtr hdc1 = g.GetHdc ();
3558 g.ReleaseHdc (hdc1);
3559 IntPtr hdc2 = g.GetHdc ();
3560 g.ReleaseHdc (hdc2);
3561 Assert.AreEqual (hdc1, hdc2, "hdc");