2 // Bitmap class testing unit
6 // Jordi Mas i Hernàndez (jmas@softcatala.org>
8 // (C) 2004 Ximian, Inc. http://www.ximian.com
12 using System.Drawing.Imaging;
13 using NUnit.Framework;
15 namespace MonoTests.System.Drawing{
18 public class BitMapTest : Assertion {
21 public void Clean() {}
24 public void GetReady()
30 public void TestPixels()
32 // Tests GetSetPixel/SetPixel
33 Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
34 bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));
35 Color color = bmp.GetPixel(0,0);
37 AssertEquals (Color.FromArgb(255,128,128,128), color);
39 bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));
40 Color color2 = bmp.GetPixel(99,99);
41 AssertEquals (Color.FromArgb(255,255,0,155), color2);
44 /* Get the right directory depending on the runtime*/
45 internal string getSubDir()
49 if (Environment.GetEnvironmentVariable("MSNet")==null)
58 public void BitmapLoadAndSave()
60 string sOutFile = getSubDir() + "linerect.bmp";
63 Bitmap bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);
64 Graphics gr = Graphics.FromImage(bmp);
66 Pen p = new Pen(Color.Red, 2);
67 gr.DrawLine(p, 10.0F, 10.0F, 90.0F, 90.0F);
68 gr.DrawRectangle(p, 10.0F, 10.0F, 80.0F, 80.0F);
70 bmp.Save(sOutFile, ImageFormat.Bmp);
75 Bitmap bmpLoad = new Bitmap(sOutFile);
77 Console.WriteLine("Unable to load "+ sOutFile);
79 Color color = bmpLoad.GetPixel(10,10);
81 Console.WriteLine("Color "+ color);
82 AssertEquals (Color.FromArgb(255,255,0,0), color);
86 public void MakeTransparent()
88 string sInFile = "bitmaps/maketransparent.bmp";
89 string sOutFile = getSubDir() + "transparent.bmp";
91 Bitmap bmp = new Bitmap(sInFile);
92 Console.WriteLine("Bitmap loaded OK", bmp != null);
94 bmp.MakeTransparent();
97 Color color = bmp.GetPixel(1,1);
98 AssertEquals (Color.Black.R, color.R);
99 AssertEquals (Color.Black.G, color.G);
100 AssertEquals (Color.Black.B, color.B);
106 string sInFile = "bitmaps/almogaver24bits.bmp";
107 string sOutFile = getSubDir() + "clone24.bmp";
109 Rectangle rect = new Rectangle(0,0,50,50);
110 Bitmap bmp = new Bitmap(sInFile);
112 Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);
113 bmpNew.Save(sOutFile);
115 Color colororg0 = bmp.GetPixel(0,0);
116 Color colororg50 = bmp.GetPixel(49,49);
117 Color colornew0 = bmpNew.GetPixel(0,0);
118 Color colornew50 = bmpNew.GetPixel(49,49);
120 AssertEquals (colororg0, colornew0);
121 AssertEquals (colororg50, colornew50);
125 public void Handles()
127 // This is a simple test. We should test really if there are valid
128 // handles for Win32/Wine
130 string sInFile = "bitmaps/almogaver24bits.bmp";
132 Bitmap bmp = new Bitmap(sInFile);
133 IntPtr HandleBmp = bmp.GetHbitmap();
134 Console.WriteLine("Handles.GetHbitmap()-> " + HandleBmp);
135 Assert(HandleBmp!=IntPtr.Zero);
137 IntPtr HandleIcon = bmp.GetHicon();
138 Console.WriteLine("Handles.GetHicon()-> " + HandleIcon);
139 Assert(HandleIcon!=IntPtr.Zero);