revert Sanjay's patch. It broke the TestBimap test case and also saving on existing...
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestBitmap.cs
1 //
2 // Bitmap class testing unit
3 //
4 // Author:
5 //
6 //       Jordi Mas i Hernàndez (jmas@softcatala.org>
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
15 namespace MonoTests.System.Drawing{
16
17         [TestFixture]   
18         public class BitMapTest : Assertion {
19                 
20                 [TearDown]
21                 public void Clean() {}
22                 
23                 [SetUp]
24                 public void GetReady()          
25                 {
26                 
27                 }
28                         
29                 //[Test]
30                 public void TestPixels() 
31                 {               
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);                                
36                                                 
37                         AssertEquals (Color.FromArgb(255,128,128,128), color);
38                         
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);                   
42                 }
43                 
44                 /* Get the right directory depending on the runtime*/
45                 internal string getSubDir()
46                 {                               
47                         string sRslt;                   
48                         
49                         if (Environment.GetEnvironmentVariable("MSNet")==null)
50                                 sRslt = "mono/";
51                         else
52                                 sRslt = "MSNet/";                       
53                                 
54                         return sRslt;
55                 }
56                 
57                 [Test]
58                 public void BitmapLoadAndSave() 
59                 {                               
60                         string sOutFile = getSubDir() + "linerect.bmp";
61                                                 
62                         // Save         
63                         Bitmap  bmp = new Bitmap(100,100, PixelFormat.Format32bppRgb);                                          
64                         Graphics gr = Graphics.FromImage(bmp);          
65                         
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);
69                         p.Dispose();                                    
70                         bmp.Save(sOutFile, ImageFormat.Bmp);
71                         gr.Dispose();
72                         bmp.Dispose();                                                  
73                         
74                         // Load                 
75                         Bitmap  bmpLoad = new Bitmap(sOutFile);
76                         if( bmpLoad == null) 
77                                 Console.WriteLine("Unable to load "+ sOutFile);                                         
78                         
79                         Color color = bmpLoad.GetPixel(10,10);          
80                         
81                         Console.WriteLine("Color "+ color);                     
82                         AssertEquals (Color.FromArgb(255,255,0,0), color);                                                                                      
83                 }
84
85                 [Test]
86                 public void MakeTransparent() 
87                 {
88                         string sInFile = "bitmaps/maketransparent.bmp";
89                         string sOutFile =  getSubDir() + "transparent.bmp";
90                                                 
91                         Bitmap  bmp = new Bitmap(sInFile);
92                         Console.WriteLine("Bitmap loaded OK", bmp != null);
93                                         
94                         bmp.MakeTransparent();
95                         bmp.Save(sOutFile);                                                     
96                         
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);                                                                          
101                 }
102                 
103                 [Test]
104                 public void Clone ()
105                 {
106                         string sInFile = "bitmaps/almogaver24bits.bmp";
107                         string sOutFile =  getSubDir() + "clone24.bmp";                 
108                         
109                         Rectangle rect = new Rectangle(0,0,50,50);                                              
110                         Bitmap  bmp = new Bitmap(sInFile);                      
111                         
112                         Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);                  
113                         bmpNew.Save(sOutFile);                                                  
114                         
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);                              
119                         
120                         AssertEquals (colororg0, colornew0);                                                                                    
121                         AssertEquals (colororg50, colornew50);                          
122                 }       
123         
124         }
125 }