TestGraphics.cs 131 KB

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