TestBmpCodec.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. //
  2. // BMPCodec class testing unit
  3. //
  4. // Author:
  5. //
  6. // Jordi Mas i Hernàndez ([email protected])
  7. //
  8. // (C) 2004 Ximian, Inc. http://www.ximian.com
  9. //
  10. using System;
  11. using System.Drawing;
  12. using System.Drawing.Imaging;
  13. using NUnit.Framework;
  14. using System.IO;
  15. using System.Security.Cryptography;
  16. using System.Text;
  17. namespace MonoTests.System.Drawing
  18. {
  19. [TestFixture]
  20. public class TestBmpCodec : Assertion
  21. {
  22. [TearDown]
  23. public void Clean() {}
  24. [SetUp]
  25. public void GetReady()
  26. {
  27. }
  28. /* Get the output directory depending on the runtime and location*/
  29. internal string getOutSubDir()
  30. {
  31. string sSub, sRslt;
  32. if (Environment.GetEnvironmentVariable("MSNet")==null)
  33. sSub = "mono/";
  34. else
  35. sSub = "MSNet/";
  36. sRslt = Path.GetFullPath ("../System.Drawing/" + sSub);
  37. if (Directory.Exists(sRslt) == false)
  38. sRslt = "Test/System.Drawing/" + sSub;
  39. if (sRslt.Length > 0)
  40. if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
  41. sRslt += "/";
  42. return sRslt;
  43. }
  44. /* Get the input directory depending on the runtime*/
  45. internal string getInFile(string file)
  46. {
  47. string sRslt, local;
  48. local = "../System.Drawing/" + file;
  49. sRslt = Path.GetFullPath (local);
  50. if (File.Exists(sRslt)==false)
  51. sRslt = "Test/System.Drawing/" + file;
  52. return sRslt;
  53. }
  54. /* Checks bitmap features on a know 24-bits bitmap */
  55. [Test]
  56. public void Bitmap24bitFeatures()
  57. {
  58. string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
  59. Bitmap bmp = new Bitmap(sInFile);
  60. RectangleF rect;
  61. GraphicsUnit unit = GraphicsUnit.World;
  62. rect = bmp.GetBounds(ref unit);
  63. AssertEquals (PixelFormat.Format24bppRgb, bmp.PixelFormat);
  64. AssertEquals (173, bmp.Width);
  65. AssertEquals (183, bmp.Height);
  66. AssertEquals (0, rect.X);
  67. AssertEquals (0, rect.Y);
  68. AssertEquals (173, rect.Width);
  69. AssertEquals (183, rect.Height);
  70. AssertEquals (173, bmp.Size.Width);
  71. AssertEquals (183, bmp.Size.Height);
  72. }
  73. /* Checks bitmap features on a know 32-bits bitmap (codec)*/
  74. [Test]
  75. public void Bitmap32bitFeatures()
  76. {
  77. string sInFile = getInFile ("bitmaps/almogaver32bits.bmp");
  78. Bitmap bmp = new Bitmap(sInFile);
  79. RectangleF rect;
  80. GraphicsUnit unit = GraphicsUnit.World;
  81. rect = bmp.GetBounds(ref unit);
  82. //AssertEquals (PixelFormat.Format32bppArgb, bmp.PixelFormat);
  83. AssertEquals (173, bmp.Width);
  84. AssertEquals (183, bmp.Height);
  85. AssertEquals (0, rect.X);
  86. AssertEquals (0, rect.Y);
  87. AssertEquals (173, rect.Width);
  88. AssertEquals (183, rect.Height);
  89. AssertEquals (173, bmp.Size.Width);
  90. AssertEquals (183, bmp.Size.Height);
  91. }
  92. [Test]
  93. public void Save()
  94. {
  95. string sOutFile = getOutSubDir() + "linerect.bmp";
  96. // Save
  97. Bitmap bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);
  98. Graphics gr = Graphics.FromImage(bmp);
  99. Pen p = new Pen(Color.Red, 2);
  100. gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
  101. gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
  102. p.Dispose();
  103. bmp.Save(sOutFile, ImageFormat.Bmp);
  104. gr.Dispose();
  105. bmp.Dispose();
  106. // Load
  107. Bitmap bmpLoad = new Bitmap(sOutFile);
  108. Color color = bmpLoad.GetPixel(10,10);
  109. AssertEquals (Color.FromArgb(255,255,0,0), color);
  110. }
  111. }
  112. }