f24179b9cbdeb375826903c25980c600359b2859
[mono.git] / mcs / class / System.Drawing / Test / System.Drawing / TestBitmap.cs
1 //
2 // Bitmap class testing unit
3 //
4 // Authors:
5 //      Jordi Mas i Hernàndez (jmas@softcatala.org>
6 //      Jonathan Gilbert <logic@deltaq.org>
7 //      Sebastien Pouliot  <sebastien@ximian.com>
8 //
9 // (C) 2004 Ximian, Inc.  http://www.ximian.com
10 // Copyright (C) 2004,2006-2007 Novell, Inc (http://www.novell.com)
11 //
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
19 // 
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
22 // 
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
30 //
31
32 using System;
33 using System.Drawing;
34 using System.Drawing.Imaging;
35 using System.IO;
36 using System.Runtime.InteropServices;
37 using System.Runtime.Serialization;
38 using System.Runtime.Serialization.Formatters.Binary;
39 using System.Runtime.Serialization.Formatters.Soap;
40 using System.Security.Cryptography;
41 using System.Security.Permissions;
42 using System.Text;
43 using System.Xml.Serialization;
44 using NUnit.Framework;
45
46 namespace MonoTests.System.Drawing {
47
48         [TestFixture]
49         [SecurityPermission (SecurityAction.Deny, UnmanagedCode = true)]
50 #if TARGET_JVM
51         [Category ("NotWorking")]
52 #endif
53         public class TestBitmap {
54                 
55                 [Test]
56                 public void TestPixels() 
57                 {               
58                         // Tests GetSetPixel/SetPixel                   
59                         Bitmap bmp= new Bitmap(100,100, PixelFormat.Format32bppRgb);
60                         bmp.SetPixel(0,0,Color.FromArgb(255,128,128,128));                                      
61                         Color color = bmp.GetPixel(0,0);                                
62                                                 
63                         Assert.AreEqual (Color.FromArgb(255,128,128,128), color);
64                         
65                         bmp.SetPixel(99,99,Color.FromArgb(255,255,0,155));                                      
66                         Color color2 = bmp.GetPixel(99,99);                                                                             
67                         Assert.AreEqual (Color.FromArgb(255,255,0,155), color2);                        
68                 }
69
70                 [Test]
71                 public void LockBits_32_32_NonIndexedWrite ()
72                 {
73                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
74                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
75                                 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
76                                 Assert.AreEqual (100, data.Height, "Height");
77                                 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
78                                 Assert.AreEqual (400, data.Stride, "Stride");
79                                 Assert.AreEqual (100, data.Width, "Width");
80                                 bmp.UnlockBits (data);
81                         }
82                 }
83
84                 [Test]
85                 public void LockBits_32_24_NonIndexedWrite ()
86                 {
87                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
88                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
89                                 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
90                                 Assert.AreEqual (100, data.Height, "Height");
91                                 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
92                                 Assert.AreEqual (300, data.Stride, "Stride");
93                                 Assert.AreEqual (100, data.Width, "Width");
94                                 bmp.UnlockBits (data);
95                         }
96                 }
97
98                 [Test]
99                 public void LockBits_24_24_NonIndexedWrite ()
100                 {
101                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
102                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
103                                 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
104                                 Assert.AreEqual (100, data.Height, "Height");
105                                 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
106                                 Assert.AreEqual (300, data.Stride, "Stride");
107                                 Assert.AreEqual (100, data.Width, "Width");
108                                 bmp.UnlockBits (data);
109                         }
110                 }
111
112                 [Test]
113                 public void LockBits_24_32_NonIndexedWrite ()
114                 {
115                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
116                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
117                                 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
118                                 Assert.AreEqual (100, data.Height, "Height");
119                                 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
120                                 Assert.AreEqual (400, data.Stride, "Stride");
121                                 Assert.AreEqual (100, data.Width, "Width");
122                                 bmp.UnlockBits (data);
123                         }
124                 }
125
126                 [Test]
127                 [ExpectedException (typeof (ArgumentException))]
128                 public void LockBits_IndexedWrite_NonIndexed ()
129                 {
130                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format8bppIndexed)) {
131                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
132                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
133                         }
134                 }
135
136                 [Test]
137                 [ExpectedException (typeof (ArgumentException))]
138                 public void LockBits_NonIndexedWrite_ToIndexed ()
139                 {
140                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
141                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
142 #if NET_2_0
143                                 BitmapData bd = new BitmapData ();
144                                 try {
145                                         bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed, bd);
146                                 }
147                                 catch (ArgumentException) {
148                                         // test to see if there's a leak or not in this case
149                                         Assert.AreEqual (IntPtr.Zero, bd.Scan0, "Scan0");
150                                         throw;
151                                 }
152 #else
153                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
154 #endif
155                         }
156                 }
157
158                 [Test]
159                 public void LockBits_IndexedWrite_SameIndexedFormat ()
160                 {
161                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format8bppIndexed)) {
162                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
163                                 BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
164                                 Assert.AreEqual (100, data.Height, "Height");
165                                 Assert.AreEqual (PixelFormat.Format8bppIndexed, data.PixelFormat, "PixelFormat");
166                                 Assert.AreEqual (100, data.Stride, "Stride");
167                                 Assert.AreEqual (100, data.Width, "Width");
168                                 bmp.UnlockBits (data);
169                         }
170                 }
171
172                 [Test]
173                 public void LockBits_ImageLockMode_Invalid ()
174                 {
175                         using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format24bppRgb)) {
176                                 Rectangle r = new Rectangle (4, 4, 4, 4);
177                                 BitmapData data = bmp.LockBits (r, (ImageLockMode)0, PixelFormat.Format24bppRgb);
178                                 try {
179                                         Assert.AreEqual (4, data.Height, "Height");
180                                         Assert.AreEqual (4, data.Width, "Width");
181                                         Assert.IsTrue (data.Stride >= 12, "Stride");
182                                         Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
183                                         Assert.IsFalse (IntPtr.Zero.Equals (data.Scan0), "Scan0");
184                                 }
185                                 finally {
186                                         bmp.UnlockBits (data);
187                                 }
188                         }
189                 }
190
191                 [Test]
192                 [ExpectedException (typeof (InvalidOperationException))]
193                 public void LockBits_Double ()
194                 {
195                         using (Bitmap bmp = new Bitmap (10, 10, PixelFormat.Format24bppRgb)) {
196                                 Rectangle r = new Rectangle (4, 4, 4, 4);
197                                 BitmapData data = bmp.LockBits (r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
198                                 try {
199                                         bmp.LockBits (r, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
200                                 }
201                                 finally {
202                                         bmp.UnlockBits (data);
203                                 }
204                         }
205                 }
206
207                 [Test]
208                 [ExpectedException (typeof (ArgumentException))]
209                 public void LockBits_Disposed ()
210                 {
211                         Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);
212                         Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
213                         bmp.Dispose ();
214                         bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
215                 }
216
217                 [Test]
218                 [ExpectedException (typeof (ArgumentException))]
219                 [Category ("Valgrind")] // this test is known to leak memory (API design limitation)
220                 public void UnlockBits_Disposed ()
221                 {
222                         Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb);
223                         Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
224                         BitmapData data = bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb);
225                         bmp.Dispose ();
226                         bmp.UnlockBits (data);
227                         // and that results in something like this when executed under Valgrind 
228                         // "40,000 bytes in 1 blocks are possibly lost in loss record 88 of 92"
229                 }
230
231                 [Test]
232                 [ExpectedException (typeof (ArgumentException))]
233                 public void UnlockBits_Null ()
234                 {
235                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
236                                 bmp.UnlockBits (null);
237                         }
238                 }
239 #if NET_2_0
240                 [Test]
241                 [ExpectedException (typeof (ArgumentException))]
242 #if TARGET_JVM
243                 [Ignore ("Bitmap.LockBits is not implemented")]
244 #endif
245                 public void LockBits_BitmapData_Null ()
246                 {
247 #if !TARGET_JVM
248                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
249                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
250                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb, null);
251                         }
252 #endif
253                 }
254
255                 [Test]
256 #if TARGET_JVM
257                 [Ignore ("Bitmap.LockBits is not implemented")]
258 #endif
259                 public void LockBits_32_32_BitmapData ()
260                 {
261 #if !TARGET_JVM
262                         BitmapData data = new BitmapData ();
263                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
264                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
265                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb, data);
266                                 Assert.AreEqual (100, data.Height, "Height");
267                                 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
268                                 Assert.AreEqual (400, data.Stride, "Stride");
269                                 Assert.AreEqual (100, data.Width, "Width");
270                                 bmp.UnlockBits (data);
271                         }
272 #endif
273                 }
274
275                 [Test]
276 #if TARGET_JVM
277                 [Ignore ("Bitmap.LockBits is not implemented")]
278 #endif
279                 public void LockBits_32_24_BitmapData ()
280                 {
281 #if !TARGET_JVM
282                         BitmapData data = new BitmapData ();
283                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format32bppRgb)) {
284                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
285                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb, data);
286                                 Assert.AreEqual (100, data.Height, "Height");
287                                 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
288                                 Assert.AreEqual (300, data.Stride, "Stride");
289                                 Assert.AreEqual (100, data.Width, "Width");
290                                 bmp.UnlockBits (data);
291                         }
292 #endif
293                 }
294
295                 [Test]
296 #if TARGET_JVM
297                 [Ignore ("Bitmap.LockBits is not implemented")]
298 #endif
299                 public void LockBits_24_24_BitmapData ()
300                 {
301 #if !TARGET_JVM
302                         BitmapData data = new BitmapData ();
303                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
304                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
305                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb, data);
306                                 Assert.AreEqual (100, data.Height, "Height");
307                                 Assert.AreEqual (PixelFormat.Format24bppRgb, data.PixelFormat, "PixelFormat");
308                                 Assert.AreEqual (300, data.Stride, "Stride");
309                                 Assert.AreEqual (100, data.Width, "Width");
310                                 bmp.UnlockBits (data);
311                         }
312 #endif
313                 }
314
315                 [Test]
316 #if TARGET_JVM
317                 [Ignore ("Bitmap.LockBits is not implemented")]
318 #endif
319                 public void LockBits_24_32_BitmapData ()
320                 {
321 #if !TARGET_JVM
322                         BitmapData data = new BitmapData ();
323                         using (Bitmap bmp = new Bitmap (100, 100, PixelFormat.Format24bppRgb)) {
324                                 Rectangle rect = new Rectangle (0, 0, bmp.Width, bmp.Height);
325                                 bmp.LockBits (rect, ImageLockMode.ReadWrite, PixelFormat.Format32bppRgb, data);
326                                 Assert.AreEqual (100, data.Height, "Height");
327                                 Assert.AreEqual (PixelFormat.Format32bppRgb, data.PixelFormat, "PixelFormat");
328                                 Assert.AreEqual (400, data.Stride, "Stride");
329                                 Assert.AreEqual (100, data.Width, "Width");
330                                 bmp.UnlockBits (data);
331                         }
332 #endif
333                 }
334 #endif
335
336                 private void FormatTest (PixelFormat format)
337                 {
338                         bool alpha = Image.IsAlphaPixelFormat (format);
339                         int size = Image.GetPixelFormatSize (format) / 8 * 2;
340                         using (Bitmap bmp = new Bitmap (2, 1, format)) {
341                                 Color a = Color.FromArgb (128, 64, 32, 16);
342                                 Color b = Color.FromArgb (192, 96, 48, 24);
343                                 bmp.SetPixel (0, 0, a);
344                                 bmp.SetPixel (1, 0, b);
345                                 Color c = bmp.GetPixel (0, 0);
346                                 Color d = bmp.GetPixel (1, 0);
347                                 if (alpha) {
348                                         if (format == PixelFormat.Format32bppPArgb) {
349                                                 Assert.AreEqual (a.A, c.A, "0,0-alpha-A");
350                                                 // note sure why the -1
351                                                 Assert.AreEqual (a.R - 1, c.R, "0,0-alpha-premultiplied-R");
352                                                 Assert.AreEqual (a.G - 1, c.G, "0,0-alpha-premultiplied-G");
353                                                 Assert.AreEqual (a.B - 1, c.B, "0,0-alpha-premultiplied-B");
354
355                                                 Assert.AreEqual (b.A, d.A, "1,0-alpha-A");
356                                                 // note sure why the -1
357                                                 Assert.AreEqual (b.R - 1, d.R, "1,0-alpha-premultiplied-R");
358                                                 Assert.AreEqual (b.G - 1, d.G, "1,0-alpha-premultiplied-G");
359                                                 Assert.AreEqual (b.B - 1, d.B, "1,0-alpha-premultiplied-B");
360                                         } else {
361                                                 Assert.AreEqual (a, c, "0,0-alpha");
362                                                 Assert.AreEqual (b, d, "1,0-alpha");
363                                         }
364                                 } else {
365                                         Assert.AreEqual (Color.FromArgb (255, 64, 32, 16), c, "0,0-non-alpha");
366                                         Assert.AreEqual (Color.FromArgb (255, 96, 48, 24), d, "1,0-non-alpha");
367                                 }
368                                 BitmapData bd = bmp.LockBits (new Rectangle (0, 0, 2, 1), ImageLockMode.ReadOnly, format);
369                                 try {
370                                         byte[] data = new byte[size];
371                                         Marshal.Copy (bd.Scan0, data, 0, size);
372                                         if (format == PixelFormat.Format32bppPArgb) {
373                                                 Assert.AreEqual (Math.Ceiling ((float)c.B * c.A / 255), data[0], "0.alpha-premultiplied-B");
374                                                 Assert.AreEqual (Math.Ceiling ((float)c.G * c.A / 255), data[1], "0.alpha-premultiplied-R");
375                                                 Assert.AreEqual (Math.Ceiling ((float)c.R * c.A / 255), data[2], "0.alpha-premultiplied-G");
376                                                 Assert.AreEqual (c.A, data[3], "0.alpha-A");
377                                                 Assert.AreEqual (Math.Ceiling ((float)d.B * d.A / 255), data[4], "1.alpha-premultiplied-B");
378                                                 Assert.AreEqual (Math.Ceiling ((float)d.G * d.A / 255), data[5], "1.alpha-premultiplied-R");
379                                                 Assert.AreEqual (Math.Ceiling ((float)d.R * d.A / 255), data[6], "1.alpha-premultiplied-G");
380                                                 Assert.AreEqual (d.A, data[7], "1.alpha-A");
381                                         } else {
382                                                 int n = 0;
383                                                 Assert.AreEqual (c.B, data[n++], "0.B");
384                                                 Assert.AreEqual (c.G, data[n++], "0.R");
385                                                 Assert.AreEqual (c.R, data[n++], "0.G");
386                                                 if (size % 4 == 0)
387                                                         Assert.AreEqual (c.A, data[n++], "0.A");
388                                                 Assert.AreEqual (d.B, data[n++], "1.B");
389                                                 Assert.AreEqual (d.G, data[n++], "1.R");
390                                                 Assert.AreEqual (d.R, data[n++], "1.G");
391                                                 if (size % 4 == 0)
392                                                         Assert.AreEqual (d.A, data[n++], "1.A");
393                                         }
394                                 }
395                                 finally {
396                                         bmp.UnlockBits (bd);
397                                 }
398                         }
399                 }
400
401                 [Test]
402                 public void Format32bppArgb ()
403                 {
404                         FormatTest (PixelFormat.Format32bppArgb);
405                 }
406
407                 [Test]
408                 [Category ("NotWorking")] // I'm not sure we're handling this format anywhere (Cairo itself use it)
409                 public void Format32bppPArgb ()
410                 {
411                         FormatTest (PixelFormat.Format32bppPArgb);
412                 }
413
414                 [Test]
415                 public void Format32bppRgb ()
416                 {
417                         FormatTest (PixelFormat.Format32bppRgb);
418                 }
419
420                 [Test]
421                 public void Format24bppRgb ()
422                 {
423                         FormatTest (PixelFormat.Format24bppRgb);
424                 }
425
426                 /* Get the output directory depending on the runtime and location*/
427                 public static string getOutSubDir()
428                 {                               
429                         string sSub, sRslt;                     
430                         
431                         if (Environment.GetEnvironmentVariable("MSNet")==null)
432                                 sSub = "mono/";
433                         else
434                                 sSub = "MSNet/";                        
435                         
436                         sRslt = Path.GetFullPath (sSub);
437                                 
438                         if (Directory.Exists(sRslt) ==  false) 
439                                 sRslt = "Test/System.Drawing/" + sSub;                                                  
440                         
441                         if (sRslt.Length > 0)
442                                 if (sRslt[sRslt.Length-1] != '\\' && sRslt[sRslt.Length-1] != '/')
443                                         sRslt += "/";                                   
444                         
445                         return sRslt;
446                 }
447                 
448                 /* Get the input directory depending on the runtime*/
449                 public static string getInFile(string file)
450                 {                               
451                         string sRslt = Path.GetFullPath ("../System.Drawing/" + file);
452                         if (!File.Exists (sRslt))
453                                 sRslt = "Test/System.Drawing/" + file;
454                         return sRslt;
455                 }
456
457                 // note: this test fails when saving (for the same reason) on Mono and MS.NET
458                 //[Test]
459                 public void MakeTransparent() 
460                 {
461                         string sInFile =   getInFile("bitmaps/maketransparent.bmp");
462                         string sOutFile =  getOutSubDir() + "transparent.bmp";
463                                                 
464                         Bitmap  bmp = new Bitmap(sInFile);
465                                         
466                         bmp.MakeTransparent();
467                         bmp.Save(sOutFile);                                                     
468                         
469                         Color color = bmp.GetPixel(1,1);                                                        
470                         Assert.AreEqual (Color.Black.R, color.R);                                                                                       
471                         Assert.AreEqual (Color.Black.G, color.G);                                                                                       
472                         Assert.AreEqual (Color.Black.B, color.B);                                                                               
473                 }
474                 
475                 [Test]
476                 public void Clone()
477                 {
478                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");
479                         Rectangle rect = new Rectangle(0,0,50,50);                                              
480                         Bitmap  bmp = new Bitmap(sInFile);                      
481                         
482                         Bitmap bmpNew = bmp.Clone (rect, PixelFormat.Format32bppArgb);                                                                  
483                         Color colororg0 = bmp.GetPixel(0,0);            
484                         Color colororg50 = bmp.GetPixel(49,49);                                 
485                         Color colornew0 = bmpNew.GetPixel(0,0);         
486                         Color colornew50 = bmpNew.GetPixel(49,49);                              
487                         
488                         Assert.AreEqual (colororg0, colornew0);                                                                                 
489                         Assert.AreEqual (colororg50, colornew50);                               
490                 }       
491                 
492                 [Test]
493                 public void CloneImage()
494                 {
495                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");                     
496                         Bitmap  bmp = new Bitmap(sInFile);                      
497                         
498                         Bitmap bmpNew = (Bitmap) bmp.Clone ();                  
499                         
500                         Assert.AreEqual (bmp.Width, bmpNew.Width);
501                         Assert.AreEqual (bmp.Height, bmpNew.Height);            
502                         Assert.AreEqual (bmp.PixelFormat, bmpNew.PixelFormat);
503                         
504                 }       
505
506                 [Test]
507                 public void Frames()
508                 {
509                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");                     
510                         Bitmap  bmp = new Bitmap(sInFile);                                              
511                         int cnt = bmp.GetFrameCount(FrameDimension.Page);                       
512                         int active = bmp.SelectActiveFrame (FrameDimension.Page, 0);
513                         
514                         Assert.AreEqual (1, cnt);                                                               
515                         Assert.AreEqual (0, active);                                                                                    
516                 }
517                 
518                 [Test]
519                 [ExpectedException (typeof (ArgumentException))]
520 #if TARGET_JVM
521                 [Category ("NotWorking")]
522 #endif
523                 public void FileDoesNotExists ()
524                 {                       
525                         Bitmap  bmp = new Bitmap ("FileDoesNotExists.jpg");                     
526                 }
527
528                 static string ByteArrayToString(byte[] arrInput)
529                 {
530                         int i;
531                         StringBuilder sOutput = new StringBuilder(arrInput.Length);
532                         for (i=0;i < arrInput.Length -1; i++) 
533                         {
534                                 sOutput.Append(arrInput[i].ToString("X2"));
535                         }
536                         return sOutput.ToString();
537                 }
538
539
540                 public string RotateBmp (Bitmap src, RotateFlipType rotate)
541                 {                       
542                         int width = 150, height = 150, index = 0;                       
543                         byte[] pixels = new byte [width * height * 3];
544                         Bitmap bmp_rotate;
545                         byte[] hash;
546                         Color clr;
547
548                         bmp_rotate = src.Clone (new RectangleF (0,0, width, height), PixelFormat.Format32bppArgb);      
549                         bmp_rotate.RotateFlip (rotate);                 
550
551                         for (int y = 0; y < height; y++) {
552                                 for (int x = 0; x < width; x++) {
553                                         clr = bmp_rotate.GetPixel (x,y);
554                                         pixels[index++] = clr.R; pixels[index++] = clr.G; pixels[index++]  = clr.B;     
555                                 }                               
556                         }
557                 
558                         hash = new MD5CryptoServiceProvider().ComputeHash (pixels);
559                         return ByteArrayToString (hash);
560                 }
561 #if !TARGET_JVM
562                 public string RotateIndexedBmp (Bitmap src, RotateFlipType type)
563                 {
564                         int pixels_per_byte;
565
566                         switch (src.PixelFormat)
567                         {
568                                 case PixelFormat.Format1bppIndexed: pixels_per_byte = 8; break;
569                                 case PixelFormat.Format4bppIndexed: pixels_per_byte = 2; break;
570                                 case PixelFormat.Format8bppIndexed: pixels_per_byte = 1; break;
571
572                                 default: throw new Exception("Cannot pass a bitmap of format " + src.PixelFormat + " to RotateIndexedBmp");
573                         }
574
575                         Bitmap test = src.Clone () as Bitmap;
576
577                         test.RotateFlip (type);
578
579                         BitmapData data = null;
580                         byte[] pixel_data;
581
582                         try
583                         {
584                                 data = test.LockBits (new Rectangle (0, 0, test.Width, test.Height), ImageLockMode.ReadOnly, test.PixelFormat);
585
586                                 int scan_size = (data.Width + pixels_per_byte - 1) / pixels_per_byte;
587                                 pixel_data = new byte[data.Height * scan_size];
588
589                                 for (int y=0; y < data.Height; y++) {
590                                         IntPtr src_ptr = (IntPtr)(y * data.Stride + data.Scan0.ToInt64 ());
591                                         int dest_offset = y * scan_size;
592                                         for (int x=0; x < scan_size; x++)
593                                                 pixel_data[dest_offset + x] = Marshal.ReadByte (src_ptr, x);
594                                 }
595                         }
596                         finally
597                         {
598                                 if (test != null) {
599                                         if (data != null)
600                                                 try { test.UnlockBits(data); } catch {}
601
602                                         try { test.Dispose(); } catch {}
603                                 }
604                         }
605
606                         if (pixel_data == null)
607                                 return "--ERROR--";
608
609                         byte[] hash = new MD5CryptoServiceProvider().ComputeHash (pixel_data);
610                         return ByteArrayToString (hash);
611                 }
612 #endif          
613                 
614                 
615                 /*
616                         Rotate bitmap in diffent ways, and check the result
617                         pixels using MD5
618                 */
619                 [Test]
620                 public void Rotate()
621                 {
622                         string sInFile = getInFile ("bitmaps/almogaver24bits.bmp");     
623                         Bitmap  bmp = new Bitmap(sInFile);
624                         
625                         Assert.AreEqual ("312958A3C67402E1299413794988A3", RotateBmp (bmp, RotateFlipType.Rotate90FlipNone));   
626                         Assert.AreEqual ("BF70D8DA4F1545AEDD77D0296B47AE", RotateBmp (bmp, RotateFlipType.Rotate180FlipNone));
627                         Assert.AreEqual ("15AD2ADBDC7090C0EC744D0F7ACE2F", RotateBmp (bmp, RotateFlipType.Rotate270FlipNone));
628                         Assert.AreEqual ("2E10FEC1F4FD64ECC51D7CE68AEB18", RotateBmp (bmp, RotateFlipType.RotateNoneFlipX));
629                         Assert.AreEqual ("E63204779B566ED01162B90B49BD9E", RotateBmp (bmp, RotateFlipType.Rotate90FlipX));
630                         Assert.AreEqual ("B1ECB17B5093E13D04FF55CFCF7763", RotateBmp (bmp, RotateFlipType.Rotate180FlipX));
631                         Assert.AreEqual ("71A173882C16755D86F4BC26532374", RotateBmp (bmp, RotateFlipType.Rotate270FlipX));
632
633                 }
634
635 #if !TARGET_JVM
636                 /*
637                         Rotate 1- and 4-bit bitmaps in different ways and check the
638                         resulting pixels using MD5
639                 */
640                 [Test]
641                 public void Rotate1bit4bit()
642                 {
643                         if ((Environment.OSVersion.Platform != (PlatformID)4)
644                          && (Environment.OSVersion.Platform != (PlatformID)128))
645                                 Assert.Ignore("This does not work with Microsoft's GDIPLUS.DLL due to off-by-1 errors in their GdipBitmapRotateFlip function.");
646
647                         string[] files = {
648                                            getInFile ("bitmaps/1bit.png"),
649                                            getInFile ("bitmaps/4bit.png")
650                                          };
651
652                         StringBuilder md5s = new StringBuilder();
653
654                         foreach (string file in files)
655                                 using (Bitmap bmp = new Bitmap(file))
656                                         foreach (RotateFlipType type in Enum.GetValues (typeof(RotateFlipType)))
657                                                 md5s.Append (RotateIndexedBmp (bmp, type));
658
659                         using (StreamWriter writer = new StreamWriter("/tmp/md5s.txt"))
660                                 writer.WriteLine(md5s);
661
662                         Assert.AreEqual (
663                                 "A4DAF507C92BDE10626BC7B34FEFE5" + // 1-bit RotateNoneFlipNone
664                                 "A4DAF507C92BDE10626BC7B34FEFE5" + // 1-bit Rotate180FlipXY
665                                 "C0975EAFD2FC1CC9CC7AF20B92FC9F" + // 1-bit Rotate90FlipNone
666                                 "C0975EAFD2FC1CC9CC7AF20B92FC9F" + // 1-bit Rotate270FlipXY
667                                 "64AE60858A02228F7B1B18C7812FB6" + // 1-bit Rotate180FlipNone
668                                 "64AE60858A02228F7B1B18C7812FB6" + // 1-bit RotateNoneFlipXY
669                                 "E96D3390938350F9DE2608C4364424" + // 1-bit Rotate270FlipNone
670                                 "E96D3390938350F9DE2608C4364424" + // 1-bit Rotate90FlipXY
671                                 "23947CE822C1DDE6BEA69C01F8D0D9" + // 1-bit RotateNoneFlipX
672                                 "23947CE822C1DDE6BEA69C01F8D0D9" + // 1-bit Rotate180FlipY
673                                 "BE45F685BDEBD7079AA1B2CBA46723" + // 1-bit Rotate90FlipX
674                                 "BE45F685BDEBD7079AA1B2CBA46723" + // 1-bit Rotate270FlipY
675                                 "353E937CFF31B1BF6C3DD0A031ACB5" + // 1-bit Rotate180FlipX
676                                 "353E937CFF31B1BF6C3DD0A031ACB5" + // 1-bit RotateNoneFlipY
677                                 "AEA18A770A845E25B6A8CE28DD6DCB" + // 1-bit Rotate270FlipX
678                                 "AEA18A770A845E25B6A8CE28DD6DCB" + // 1-bit Rotate90FlipY
679                                 "3CC874B571902366AACED5D619E87D" + // 4-bit RotateNoneFlipNone
680                                 "3CC874B571902366AACED5D619E87D" + // 4-bit Rotate180FlipXY
681                                 "8DE25C7E1BE4A3B535DB5D83198D83" + // 4-bit Rotate90FlipNone
682                                 "8DE25C7E1BE4A3B535DB5D83198D83" + // 4-bit Rotate270FlipXY
683                                 "27CF5E9CE70BE9EBC47FB996721B95" + // 4-bit Rotate180FlipNone
684                                 "27CF5E9CE70BE9EBC47FB996721B95" + // 4-bit RotateNoneFlipXY
685                                 "A919CCB8F97CAD7DC1F01026D11A5D" + // 4-bit Rotate270FlipNone
686                                 "A919CCB8F97CAD7DC1F01026D11A5D" + // 4-bit Rotate90FlipXY
687                                 "545876C99ACF833E69FBFFBF436034" + // 4-bit RotateNoneFlipX
688                                 "545876C99ACF833E69FBFFBF436034" + // 4-bit Rotate180FlipY
689                                 "5DB56687757CDEFC52D89C77CA9223" + // 4-bit Rotate90FlipX
690                                 "5DB56687757CDEFC52D89C77CA9223" + // 4-bit Rotate270FlipY
691                                 "05A77EDDCDF20D5B0AC0169E95D7D7" + // 4-bit Rotate180FlipX
692                                 "05A77EDDCDF20D5B0AC0169E95D7D7" + // 4-bit RotateNoneFlipY
693                                 "B6B6245796C836923ABAABDF368B29" + // 4-bit Rotate270FlipX
694                                 "B6B6245796C836923ABAABDF368B29",  // 4-bit Rotate90FlipY
695                                 md5s.ToString ());
696                 }
697
698                 private Bitmap CreateBitmap (int width, int height, PixelFormat fmt)
699                 {
700                         Bitmap bmp = new Bitmap (width, height, fmt);
701                         using (Graphics gr = Graphics.FromImage (bmp)) {
702                                 Color c = Color.FromArgb (255, 100, 200, 250);
703                                 for (int x = 1; x < 80; x++) {
704                                         bmp.SetPixel (x, 1, c);
705                                         bmp.SetPixel (x, 2, c);
706                                         bmp.SetPixel (x, 78, c);
707                                         bmp.SetPixel (x, 79, c);
708                                 }
709                                 for (int y = 3; y < 78; y++) {
710                                         bmp.SetPixel (1, y, c);
711                                         bmp.SetPixel (2, y, c);
712                                         bmp.SetPixel (78, y, c);
713                                         bmp.SetPixel (79, y, c);
714                                 }
715                         }
716                         return bmp;
717                 }
718
719                 private byte[] HashPixels (Bitmap bmp)
720                 {
721                         int len = bmp.Width * bmp.Height * 4;
722                         int index = 0;
723                         byte[] pixels = new byte [len];
724
725                         for (int y = 0; y < bmp.Height; y++) {
726                                 for (int x = 0; x < bmp.Width; x++) {
727                                         Color clr = bmp.GetPixel (x, y);
728                                         pixels[index++] = clr.R;
729                                         pixels[index++] = clr.G;
730                                         pixels[index++] = clr.B;
731                                 }
732                         }
733                         return MD5.Create ().ComputeHash (pixels);
734                 }
735
736                 private byte[] HashLock (Bitmap bmp, int width, int height, PixelFormat fmt, ImageLockMode mode)
737                 {
738                         int len = bmp.Width * bmp.Height * 4;
739                         byte[] pixels = new byte[len];
740                         BitmapData bd = bmp.LockBits (new Rectangle (0, 0, width, height), mode, fmt);
741                         try {
742                                 int index = 0;
743                                 int bbps = Image.GetPixelFormatSize (fmt);
744                                 int pos = bd.Scan0.ToInt32 ();
745                                 byte[] btv = new byte[1];
746                                 for (int y = 0; y < bd.Height; y++) {
747                                         for (int x = 0; x < bd.Width; x++) {
748
749                                                 /* Read the pixels*/
750                                                 for (int bt = 0; bt < bbps / 8; bt++, index++) {
751                                                         int cur = pos;
752                                                         cur += y * bd.Stride;
753                                                         cur += x * bbps / 8;
754                                                         cur += bt;
755                                                         Marshal.Copy ((IntPtr) cur, btv, 0, 1);
756                                                         pixels[index] = btv[0];
757
758                                                         /* Make change of all the colours = 250 to 10*/
759                                                         if (btv[0] == 250) {
760                                                                 btv[0] = 10;
761                                                                 Marshal.Copy (btv, 0, (IntPtr) cur, 1);
762                                                         }
763                                                 }
764                                         }
765                                 }
766
767                                 for (int i = index; i < len; i++)
768                                         pixels[index] = 0;
769                         }
770                         finally {
771                                 bmp.UnlockBits (bd);
772                         }
773                         return MD5.Create ().ComputeHash (pixels);
774                 }
775
776                 /*
777                         Tests the LockBitmap functions. Makes a hash of the block of pixels that it returns
778                         firsts, changes them, and then using GetPixel does another check of the changes.
779                         The results match the .Net framework
780                 */
781                 private static byte[] DefaultBitmapHash = new byte[] { 0xD8, 0xD3, 0x68, 0x9C, 0x86, 0x7F, 0xB6, 0xA0, 0x76, 0xD6, 0x00, 0xEF, 0xFF, 0xE5, 0x8E, 0x1B };
782                 private static byte[] FinalWholeBitmapHash = new byte[] { 0x5F, 0x52, 0x98, 0x37, 0xE3, 0x94, 0xE1, 0xA6, 0x06, 0x6C, 0x5B, 0xF1, 0xA9, 0xC2, 0xA9, 0x43 };
783
784                 [Test]
785                 public void LockBitmap_Format32bppArgb_Format32bppArgb_ReadWrite_Whole ()
786                 {
787                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
788                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
789                                 byte[] expected = { 0x89, 0x6A, 0x6B, 0x35, 0x5C, 0x89, 0xD9, 0xE9, 0xF4, 0x51, 0xD5, 0x89, 0xED, 0x28, 0x68, 0x5C };
790                                 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format32bppArgb, ImageLockMode.ReadWrite);
791                                 Assert.AreEqual (expected, actual, "Full-Format32bppArgb");
792                                 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
793                         }
794                 }
795
796                 [Test]
797                 public void LockBitmap_Format32bppArgb_Format32bppPArgb_ReadWrite_Whole ()
798                 {
799                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
800                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
801                                 byte[] expected = { 0x89, 0x6A, 0x6B, 0x35, 0x5C, 0x89, 0xD9, 0xE9, 0xF4, 0x51, 0xD5, 0x89, 0xED, 0x28, 0x68, 0x5C };
802                                 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format32bppPArgb, ImageLockMode.ReadWrite);
803                                 Assert.AreEqual (expected, actual, "Full-Format32bppPArgb");
804                                 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
805                         }
806                 }
807
808                 [Test]
809                 public void LockBitmap_Format32bppArgb_Format32bppRgb_ReadWrite_Whole ()
810                 {
811                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
812                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
813                                 byte[] expected = { 0xC0, 0x28, 0xB5, 0x2E, 0x86, 0x90, 0x6F, 0x37, 0x09, 0x5F, 0x49, 0xA4, 0x91, 0xDA, 0xEE, 0xB9 };
814                                 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format32bppRgb, ImageLockMode.ReadWrite);
815                                 Assert.AreEqual (expected, actual, "Full-Format32bppRgb");
816                                 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
817                         }
818                 }
819
820                 [Test]
821                 public void LockBitmap_Format32bppArgb_Format24bppRgb_ReadWrite_Whole ()
822                 {
823                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
824                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
825                                 byte[] expected = { 0xA7, 0xB2, 0x50, 0x04, 0x11, 0x12, 0x64, 0x68, 0x6B, 0x7D, 0x2F, 0x6E, 0x69, 0x24, 0xCB, 0x14 };
826                                 byte[] actual = HashLock (bmp, bmp.Width, bmp.Height, PixelFormat.Format24bppRgb, ImageLockMode.ReadWrite);
827                                 Assert.AreEqual (expected, actual, "Full-Format24bppRgb");
828                                 Assert.AreEqual (FinalWholeBitmapHash, HashPixels (bmp), "Final");
829                         }
830                 }
831
832                 private static byte[] FinalPartialBitmapHash = new byte[] { 0xED, 0xD8, 0xDC, 0x9B, 0x44, 0x00, 0x22, 0x9B, 0x07, 0x06, 0x4A, 0x21, 0x70, 0xA7, 0x31, 0x1D };
833
834                 [Test]
835                 public void LockBitmap_Format32bppArgb_Format32bppArgb_ReadWrite_Partial ()
836                 {
837                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
838                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
839                                 byte[] expected = { 0x5D, 0xFF, 0x02, 0x34, 0xEB, 0x7C, 0xF7, 0x42, 0xD4, 0xB7, 0x70, 0x49, 0xB4, 0x06, 0x79, 0xBC };
840                                 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format32bppArgb, ImageLockMode.ReadWrite);
841                                 Assert.AreEqual (expected, actual, "Partial-Format32bppArgb");
842                                 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
843                         }
844                 }
845
846                 [Test]
847                 public void LockBitmap_Format32bppArgb_Format32bppPArgb_ReadWrite_Partial ()
848                 {
849                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
850                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
851                                 byte[] expected = { 0x5D, 0xFF, 0x02, 0x34, 0xEB, 0x7C, 0xF7, 0x42, 0xD4, 0xB7, 0x70, 0x49, 0xB4, 0x06, 0x79, 0xBC };
852                                 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format32bppPArgb, ImageLockMode.ReadWrite);
853                                 Assert.AreEqual (expected, actual, "Partial-Format32bppPArgb");
854                                 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
855                         }
856                 }
857
858                 [Test]
859                 public void LockBitmap_Format32bppArgb_Format32bppRgb_ReadWrite_Partial ()
860                 {
861                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
862                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
863                                 byte[] expected = { 0x72, 0x33, 0x09, 0x67, 0x53, 0x65, 0x38, 0xF9, 0xE4, 0x58, 0xE1, 0x0A, 0xAA, 0x6A, 0xCC, 0xB8 };
864                                 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format32bppRgb, ImageLockMode.ReadWrite);
865                                 Assert.AreEqual (expected, actual, "Partial-Format32bppRgb");
866                                 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
867                         }
868                 }
869
870                 [Test]
871                 public void LockBitmap_Format32bppArgb_Format24bppRgb_ReadWrite_Partial ()
872                 {
873                         using (Bitmap bmp = CreateBitmap (100, 100, PixelFormat.Format32bppArgb)) {
874                                 Assert.AreEqual (DefaultBitmapHash, HashPixels (bmp), "Initial");
875                                 byte[] expected = { 0x4D, 0x39, 0x21, 0x88, 0xC2, 0x17, 0x14, 0x5F, 0x89, 0x9E, 0x02, 0x75, 0xF3, 0x64, 0xD8, 0xF0 };
876                                 byte[] actual = HashLock (bmp, 50, 50, PixelFormat.Format24bppRgb, ImageLockMode.ReadWrite);
877                                 Assert.AreEqual (expected, actual, "Partial-Format24bppRgb");
878                                 Assert.AreEqual (FinalPartialBitmapHash, HashPixels (bmp), "Final");
879                         }
880                 }
881
882                 /*
883                         Tests the LockBitmap and UnlockBitmap functions, specifically the copying
884                         of bitmap data in the directions indicated by the ImageLockMode.
885                 */
886                 [Test]
887                 public void LockUnlockBitmap()
888                 {
889                         BitmapData data;
890                         int pixel_value;
891                         Color pixel_colour;
892
893                         Color red  = Color.FromArgb (Color.Red.A,  Color.Red.R,  Color.Red.G,  Color.Red.B);
894                         Color blue = Color.FromArgb (Color.Blue.A, Color.Blue.R, Color.Blue.G, Color.Blue.B);
895
896                         using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format32bppRgb))
897                         {
898                                 bmp.SetPixel (0, 0, red);
899                                 pixel_colour = bmp.GetPixel (0, 0);
900                                 Assert.AreEqual (red, pixel_colour, "Set/Get-Red");
901
902                                 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb);
903
904                                 pixel_value = Marshal.ReadByte (data.Scan0, 0);
905                                 pixel_value |= Marshal.ReadByte (data.Scan0, 1) << 8;
906                                 pixel_value |= Marshal.ReadByte (data.Scan0, 2) << 16;
907                                 pixel_value |= Marshal.ReadByte (data.Scan0, 3) << 24;
908
909                                 pixel_colour = Color.FromArgb (pixel_value);
910
911                                 // Disregard alpha information in the test
912                                 pixel_colour = Color.FromArgb(red.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
913
914                                 Assert.AreEqual (red, pixel_colour, "Red-FromLockedBitmap");
915
916                                 Marshal.WriteInt32 (data.Scan0, blue.ToArgb ());
917
918                                 bmp.UnlockBits (data);
919
920                                 pixel_colour = bmp.GetPixel (0, 0);
921
922                                 // Disregard alpha information in the test
923                                 pixel_colour = Color.FromArgb(red.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
924
925                                 Assert.AreEqual (red, pixel_colour);
926
927                                 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadWrite, PixelFormat.Format32bppArgb);
928
929                                 Marshal.WriteInt32 (data.Scan0, blue.ToArgb ());
930
931                                 bmp.UnlockBits (data);
932
933                                 pixel_colour = bmp.GetPixel (0, 0);
934
935                                 // Disregard alpha information in the test
936                                 pixel_colour = Color.FromArgb(blue.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
937
938                                 Assert.AreEqual (blue, pixel_colour);
939                         }
940
941                         using (Bitmap bmp = new Bitmap(1, 1, PixelFormat.Format32bppArgb))
942                         {
943                                 bmp.SetPixel (0, 0, red);
944
945                                 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
946
947                                 int r, g, b;
948
949                                 b = Marshal.ReadByte (data.Scan0, 0);
950                                 g = Marshal.ReadByte (data.Scan0, 1);
951                                 r = Marshal.ReadByte (data.Scan0, 2);
952                                 pixel_colour = Color.FromArgb (red.A, r, g, b);
953
954                                 Assert.AreEqual (red, pixel_colour);
955
956                                 Marshal.WriteByte (data.Scan0, 0, blue.B);
957                                 Marshal.WriteByte (data.Scan0, 1, blue.G);
958                                 Marshal.WriteByte (data.Scan0, 2, blue.R);
959
960                                 bmp.UnlockBits (data);
961
962                                 pixel_colour = bmp.GetPixel (0, 0);
963
964                                 // Disregard alpha information in the test
965                                 pixel_colour = Color.FromArgb(red.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
966
967                                 Assert.AreEqual (red, bmp.GetPixel (0, 0));
968
969                                 data = bmp.LockBits (new Rectangle (0, 0, 1, 1), ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb);
970
971                                 Marshal.WriteByte (data.Scan0, 0, blue.B);
972                                 Marshal.WriteByte (data.Scan0, 1, blue.G);
973                                 Marshal.WriteByte (data.Scan0, 2, blue.R);
974
975                                 bmp.UnlockBits(data);
976
977                                 pixel_colour = bmp.GetPixel (0, 0);
978
979                                 // Disregard alpha information in the test
980                                 pixel_colour = Color.FromArgb(blue.A, pixel_colour.R, pixel_colour.G, pixel_colour.B);
981
982                                 Assert.AreEqual (blue, bmp.GetPixel (0, 0));
983                         }
984                 }
985 #endif          
986                 [Test]
987                 public void DefaultFormat1 ()
988                 {
989                         using (Bitmap bmp = new Bitmap (20, 20)) {
990                                 Assert.AreEqual (ImageFormat.MemoryBmp, bmp.RawFormat);
991                         }
992                 }
993
994                 [Test]
995                 public void DefaultFormat2 ()
996                 {
997                         string filename =  Path.GetTempFileName ();
998                         using (Bitmap bmp = new Bitmap (20, 20)) {
999                                 bmp.Save (filename);
1000                         }
1001
1002                         using (Bitmap other = new Bitmap (filename)) {
1003                                 Assert.AreEqual (ImageFormat.Png, other.RawFormat);
1004                         }
1005                         File.Delete (filename);
1006                 }
1007
1008                 [Test]
1009                 public void BmpDataStride1 ()
1010                 {
1011                         Bitmap bmp = new Bitmap (184, 184, PixelFormat.Format1bppIndexed);
1012                         BitmapData data = bmp.LockBits (new Rectangle (0, 0, bmp.Width, bmp.Height), ImageLockMode.ReadWrite, PixelFormat.Format1bppIndexed);
1013                         try {
1014                                 Assert.AreEqual (24, data.Stride);
1015                         } finally {
1016                                 bmp.UnlockBits (data);
1017                                 bmp.Dispose ();
1018                         }
1019                 }
1020
1021                 private Stream Serialize (object o)
1022                 {
1023                         MemoryStream ms = new MemoryStream ();
1024                         IFormatter formatter = new BinaryFormatter ();
1025                         formatter.Serialize (ms, o);
1026                         ms.Position = 0;
1027                         return ms;
1028                 }
1029
1030                 private object Deserialize (Stream s)
1031                 {
1032                         return new BinaryFormatter ().Deserialize (s);
1033                 }
1034
1035                 [Test]
1036                 public void Serialize_Icon ()
1037                 {
1038                         // this cause a problem with resgen, see http://bugzilla.ximian.com/show_bug.cgi?id=80565
1039                         string filename = getInFile ("bitmaps/16x16x16.ico");
1040                         using (Bitmap icon = new Bitmap (filename)) {
1041                                 using (Stream s = Serialize (icon)) {
1042                                         using (Bitmap copy = (Bitmap)Deserialize (s)) {
1043                                                 Assert.AreEqual (icon.Height, copy.Height, "Height");
1044                                                 Assert.AreEqual (icon.Width, copy.Width, "Width");
1045                                                 Assert.AreEqual (icon.PixelFormat, copy.PixelFormat, "PixelFormat");
1046                                                 Assert.IsTrue (icon.RawFormat.Equals (ImageFormat.Icon), "Icon");
1047                                                 Assert.IsTrue (copy.RawFormat.Equals (ImageFormat.Png), "Png");
1048                                         }
1049                                 }
1050                         }
1051                 }
1052
1053                 private Stream SoapSerialize (object o)
1054                 {
1055                         MemoryStream ms = new MemoryStream ();
1056                         IFormatter formatter = new SoapFormatter ();
1057                         formatter.Serialize (ms, o);
1058                         ms.Position = 0;
1059                         return ms;
1060                 }
1061
1062                 private object SoapDeserialize (Stream s)
1063                 {
1064                         return new SoapFormatter ().Deserialize (s);
1065                 }
1066
1067                 [Test]
1068                 public void SoapSerialize_Icon ()
1069                 {
1070                         string filename = getInFile ("bitmaps/16x16x16.ico");
1071                         using (Bitmap icon = new Bitmap (filename)) {
1072                                 using (Stream s = SoapSerialize (icon)) {
1073                                         using (Bitmap copy = (Bitmap) SoapDeserialize (s)) {
1074                                                 Assert.AreEqual (icon.Height, copy.Height, "Height");
1075                                                 Assert.AreEqual (icon.Width, copy.Width, "Width");
1076                                                 Assert.AreEqual (icon.PixelFormat, copy.PixelFormat, "PixelFormat");
1077                                                 Assert.AreEqual (16, icon.Palette.Entries.Length, "icon Palette");
1078                                                 Assert.IsTrue (icon.RawFormat.Equals (ImageFormat.Icon), "Icon");
1079                                                 Assert.AreEqual (0, copy.Palette.Entries.Length, "copy Palette");
1080                                                 Assert.IsTrue (copy.RawFormat.Equals (ImageFormat.Png), "Png");
1081                                         }
1082                                 }
1083                         }
1084                 }
1085
1086                 [Test]
1087                 public void SoapSerialize_Bitmap8 ()
1088                 {
1089                         string filename = getInFile ("bitmaps/almogaver8bits.bmp");
1090                         using (Bitmap bmp = new Bitmap (filename)) {
1091                                 using (Stream s = SoapSerialize (bmp)) {
1092                                         using (Bitmap copy = (Bitmap) SoapDeserialize (s)) {
1093                                                 Assert.AreEqual (bmp.Height, copy.Height, "Height");
1094                                                 Assert.AreEqual (bmp.Width, copy.Width, "Width");
1095                                                 Assert.AreEqual (bmp.PixelFormat, copy.PixelFormat, "PixelFormat");
1096                                                 Assert.AreEqual (256, copy.Palette.Entries.Length, "Palette");
1097                                                 Assert.AreEqual (bmp.RawFormat, copy.RawFormat, "RawFormat");
1098                                         }
1099                                 }
1100                         }
1101                 }
1102
1103                 [Test]
1104                 public void SoapSerialize_Bitmap24 ()
1105                 {
1106                         string filename = getInFile ("bitmaps/almogaver24bits.bmp");
1107                         using (Bitmap bmp = new Bitmap (filename)) {
1108                                 using (Stream s = SoapSerialize (bmp)) {
1109                                         using (Bitmap copy = (Bitmap) SoapDeserialize (s)) {
1110                                                 Assert.AreEqual (bmp.Height, copy.Height, "Height");
1111                                                 Assert.AreEqual (bmp.Width, copy.Width, "Width");
1112                                                 Assert.AreEqual (bmp.PixelFormat, copy.PixelFormat, "PixelFormat");
1113                                                 Assert.AreEqual (bmp.Palette.Entries.Length, copy.Palette.Entries.Length, "Palette");
1114                                                 Assert.AreEqual (bmp.RawFormat, copy.RawFormat, "RawFormat");
1115                                         }
1116                                 }
1117                         }
1118                 }
1119
1120                 [Test]
1121 #if NET_2_0
1122                 [Category ("NotWorking")]       // http://bugzilla.ximian.com/show_bug.cgi?id=80558
1123 #else
1124                 [ExpectedException (typeof (InvalidOperationException))]
1125 #endif
1126                 public void XmlSerialize ()
1127                 {
1128                         new XmlSerializer (typeof (Bitmap));
1129                 }
1130
1131                 static int[] palette1 = {
1132                         -16777216,
1133                         -1,
1134                 };
1135
1136                 [Test]
1137                 public void Format1bppIndexed_Palette ()
1138                 {
1139                         using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format1bppIndexed)) {
1140                                 ColorPalette pal = bmp.Palette;
1141                                 Assert.AreEqual (2, pal.Entries.Length, "Length");
1142                                 for (int i = 0; i < pal.Entries.Length; i++) {
1143                                         Assert.AreEqual (palette1[i], pal.Entries[i].ToArgb (), i.ToString ());
1144                                 }
1145                                 Assert.AreEqual (2, pal.Flags, "Flags");
1146                         }
1147                 }
1148
1149                 static int[] palette16 = {
1150                         -16777216,
1151                         -8388608,
1152                         -16744448,
1153                         -8355840,
1154                         -16777088,
1155                         -8388480,
1156                         -16744320,
1157                         -8355712,
1158                         -4144960,
1159                         -65536,
1160                         -16711936,
1161                         -256,
1162                         -16776961,
1163                         -65281,
1164                         -16711681,
1165                         -1,
1166                 };
1167
1168                 [Test]
1169                 public void Format4bppIndexed_Palette ()
1170                 {
1171                         using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format4bppIndexed)) {
1172                                 ColorPalette pal = bmp.Palette;
1173                                 Assert.AreEqual (16, pal.Entries.Length, "Length");
1174                                 for (int i = 0; i < pal.Entries.Length; i++) {
1175                                         Assert.AreEqual (palette16 [i], pal.Entries[i].ToArgb (), i.ToString ());
1176                                 }
1177                                 Assert.AreEqual (0, pal.Flags, "Flags");
1178                         }
1179                 }
1180
1181                 static int[] palette256 = {
1182                         -16777216,
1183                         -8388608,
1184                         -16744448,
1185                         -8355840,
1186                         -16777088,
1187                         -8388480,
1188                         -16744320,
1189                         -8355712,
1190                         -4144960,
1191                         -65536,
1192                         -16711936,
1193                         -256,
1194                         -16776961,
1195                         -65281,
1196                         -16711681,
1197                         -1,
1198                         0,
1199                         0,
1200                         0,
1201                         0,
1202                         0,
1203                         0,
1204                         0,
1205                         0,
1206                         0,
1207                         0,
1208                         0,
1209                         0,
1210                         0,
1211                         0,
1212                         0,
1213                         0,
1214                         0,
1215                         0,
1216                         0,
1217                         0,
1218                         0,
1219                         0,
1220                         0,
1221                         0,
1222                         -16777216,
1223                         -16777165,
1224                         -16777114,
1225                         -16777063,
1226                         -16777012,
1227                         -16776961,
1228                         -16764160,
1229                         -16764109,
1230                         -16764058,
1231                         -16764007,
1232                         -16763956,
1233                         -16763905,
1234                         -16751104,
1235                         -16751053,
1236                         -16751002,
1237                         -16750951,
1238                         -16750900,
1239                         -16750849,
1240                         -16738048,
1241                         -16737997,
1242                         -16737946,
1243                         -16737895,
1244                         -16737844,
1245                         -16737793,
1246                         -16724992,
1247                         -16724941,
1248                         -16724890,
1249                         -16724839,
1250                         -16724788,
1251                         -16724737,
1252                         -16711936,
1253                         -16711885,
1254                         -16711834,
1255                         -16711783,
1256                         -16711732,
1257                         -16711681,
1258                         -13434880,
1259                         -13434829,
1260                         -13434778,
1261                         -13434727,
1262                         -13434676,
1263                         -13434625,
1264                         -13421824,
1265                         -13421773,
1266                         -13421722,
1267                         -13421671,
1268                         -13421620,
1269                         -13421569,
1270                         -13408768,
1271                         -13408717,
1272                         -13408666,
1273                         -13408615,
1274                         -13408564,
1275                         -13408513,
1276                         -13395712,
1277                         -13395661,
1278                         -13395610,
1279                         -13395559,
1280                         -13395508,
1281                         -13395457,
1282                         -13382656,
1283                         -13382605,
1284                         -13382554,
1285                         -13382503,
1286                         -13382452,
1287                         -13382401,
1288                         -13369600,
1289                         -13369549,
1290                         -13369498,
1291                         -13369447,
1292                         -13369396,
1293                         -13369345,
1294                         -10092544,
1295                         -10092493,
1296                         -10092442,
1297                         -10092391,
1298                         -10092340,
1299                         -10092289,
1300                         -10079488,
1301                         -10079437,
1302                         -10079386,
1303                         -10079335,
1304                         -10079284,
1305                         -10079233,
1306                         -10066432,
1307                         -10066381,
1308                         -10066330,
1309                         -10066279,
1310                         -10066228,
1311                         -10066177,
1312                         -10053376,
1313                         -10053325,
1314                         -10053274,
1315                         -10053223,
1316                         -10053172,
1317                         -10053121,
1318                         -10040320,
1319                         -10040269,
1320                         -10040218,
1321                         -10040167,
1322                         -10040116,
1323                         -10040065,
1324                         -10027264,
1325                         -10027213,
1326                         -10027162,
1327                         -10027111,
1328                         -10027060,
1329                         -10027009,
1330                         -6750208,
1331                         -6750157,
1332                         -6750106,
1333                         -6750055,
1334                         -6750004,
1335                         -6749953,
1336                         -6737152,
1337                         -6737101,
1338                         -6737050,
1339                         -6736999,
1340                         -6736948,
1341                         -6736897,
1342                         -6724096,
1343                         -6724045,
1344                         -6723994,
1345                         -6723943,
1346                         -6723892,
1347                         -6723841,
1348                         -6711040,
1349                         -6710989,
1350                         -6710938,
1351                         -6710887,
1352                         -6710836,
1353                         -6710785,
1354                         -6697984,
1355                         -6697933,
1356                         -6697882,
1357                         -6697831,
1358                         -6697780,
1359                         -6697729,
1360                         -6684928,
1361                         -6684877,
1362                         -6684826,
1363                         -6684775,
1364                         -6684724,
1365                         -6684673,
1366                         -3407872,
1367                         -3407821,
1368                         -3407770,
1369                         -3407719,
1370                         -3407668,
1371                         -3407617,
1372                         -3394816,
1373                         -3394765,
1374                         -3394714,
1375                         -3394663,
1376                         -3394612,
1377                         -3394561,
1378                         -3381760,
1379                         -3381709,
1380                         -3381658,
1381                         -3381607,
1382                         -3381556,
1383                         -3381505,
1384                         -3368704,
1385                         -3368653,
1386                         -3368602,
1387                         -3368551,
1388                         -3368500,
1389                         -3368449,
1390                         -3355648,
1391                         -3355597,
1392                         -3355546,
1393                         -3355495,
1394                         -3355444,
1395                         -3355393,
1396                         -3342592,
1397                         -3342541,
1398                         -3342490,
1399                         -3342439,
1400                         -3342388,
1401                         -3342337,
1402                         -65536,
1403                         -65485,
1404                         -65434,
1405                         -65383,
1406                         -65332,
1407                         -65281,
1408                         -52480,
1409                         -52429,
1410                         -52378,
1411                         -52327,
1412                         -52276,
1413                         -52225,
1414                         -39424,
1415                         -39373,
1416                         -39322,
1417                         -39271,
1418                         -39220,
1419                         -39169,
1420                         -26368,
1421                         -26317,
1422                         -26266,
1423                         -26215,
1424                         -26164,
1425                         -26113,
1426                         -13312,
1427                         -13261,
1428                         -13210,
1429                         -13159,
1430                         -13108,
1431                         -13057,
1432                         -256,
1433                         -205,
1434                         -154,
1435                         -103,
1436                         -52,
1437                         -1,
1438                 };
1439
1440                 [Test]
1441                 public void Format8bppIndexed_Palette ()
1442                 {
1443                         using (Bitmap bmp = new Bitmap (1, 1, PixelFormat.Format8bppIndexed)) {
1444                                 ColorPalette pal = bmp.Palette;
1445                                 Assert.AreEqual (256, pal.Entries.Length, "Length");
1446                                 for (int i = 0; i < pal.Entries.Length; i++) {
1447                                         Assert.AreEqual (palette256[i], pal.Entries[i].ToArgb (), i.ToString ());
1448                                 }
1449                                 Assert.AreEqual (4, pal.Flags, "Flags");
1450                         }
1451                 }
1452         }
1453
1454         [TestFixture]
1455 #if TARGET_JVM
1456         [Ignore ("Unsafe code is not supported")]
1457 #endif
1458         public class BitmapFullTrustTest {
1459 #if !TARGET_JVM
1460                 // BitmapFromHicon## is *almost* the same as IconTest.Icon##ToBitmap except
1461                 // for the Flags property
1462
1463                 private void HiconTest (string msg, Bitmap b, int size)
1464                 {
1465                         Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, msg + ".PixelFormat");
1466                         // unlike the GDI+ icon decoder the palette isn't kept
1467                         Assert.AreEqual (0, b.Palette.Entries.Length, msg + ".Palette");
1468                         Assert.AreEqual (size, b.Height, msg + ".Height");
1469                         Assert.AreEqual (size, b.Width, msg + ".Width");
1470                         Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), msg + ".RawFormat");
1471                         Assert.AreEqual (335888, b.Flags, msg + ".Flags");
1472                 }
1473
1474                 [Test]
1475                 public void Hicon16 ()
1476                 {
1477                         IntPtr hicon;
1478                         int size;
1479                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
1480                                 size = icon.Width;
1481                                 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1482                                         HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1483                                         hicon = bitmap.GetHicon ();
1484                                 }
1485                         }
1486                         using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1487                                 // hicon survives bitmap and icon disposal
1488                                 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1489                         }
1490                 }
1491
1492                 [Test]
1493                 public void Hicon32 ()
1494                 {
1495                         IntPtr hicon;
1496                         int size;
1497                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/32x32x16.ico"))) {
1498                                 size = icon.Width;
1499                                 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1500                                         HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1501                                         hicon = bitmap.GetHicon ();
1502                                 }
1503                         }
1504                         using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1505                                 // hicon survives bitmap and icon disposal
1506                                 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1507                         }
1508                 }
1509
1510                 [Test]
1511                 [ExpectedException (typeof (ArgumentException))]
1512                 [Category ("NotWorking")] // libgdiplus has lost track of the original 1bpp state
1513                 public void Hicon48 ()
1514                 {
1515                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/48x48x1.ico"))) {
1516                                 // looks like 1bbp icons aren't welcome as bitmaps ;-)
1517                                 Bitmap.FromHicon (icon.Handle);
1518                         }
1519                 }
1520
1521                 [Test]
1522                 public void Hicon64 ()
1523                 {
1524                         IntPtr hicon;
1525                         int size;
1526                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/64x64x256.ico"))) {
1527                                 size = icon.Width;
1528                                 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1529                                         HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1530                                         hicon = bitmap.GetHicon ();
1531                                 }
1532                         }
1533                         using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1534                                 // hicon survives bitmap and icon disposal
1535                                 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1536                         }
1537                 }
1538
1539                 [Test]
1540                 public void Hicon96 ()
1541                 {
1542                         IntPtr hicon;
1543                         int size;
1544                         using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/96x96x256.ico"))) {
1545                                 size = icon.Width;
1546                                 using (Bitmap bitmap = Bitmap.FromHicon (icon.Handle)) {
1547                                         HiconTest ("Icon.Handle/FromHicon", bitmap, size);
1548                                         hicon = bitmap.GetHicon ();
1549                                 }
1550                         }
1551                         using (Bitmap bitmap2 = Bitmap.FromHicon (hicon)) {
1552                                 // hicon survives bitmap and icon disposal
1553                                 HiconTest ("GetHicon/FromHicon", bitmap2, size);
1554                         }
1555                 }
1556
1557                 [Test]
1558                 public void HBitmap ()
1559                 {
1560                         IntPtr hbitmap;
1561                         string sInFile = TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp");
1562                         using (Bitmap bitmap = new Bitmap (sInFile)) {
1563                                 Assert.AreEqual (PixelFormat.Format24bppRgb, bitmap.PixelFormat, "Original.PixelFormat");
1564                                 Assert.AreEqual (0, bitmap.Palette.Entries.Length, "Original.Palette");
1565                                 Assert.AreEqual (183, bitmap.Height, "Original.Height");
1566                                 Assert.AreEqual (173, bitmap.Width, "Original.Width");
1567                                 Assert.AreEqual (73744, bitmap.Flags, "Original.Flags");
1568                                 Assert.IsTrue (bitmap.RawFormat.Equals (ImageFormat.Bmp), "Original.RawFormat");
1569                                 hbitmap = bitmap.GetHbitmap ();
1570                         }
1571                         // hbitmap survives original bitmap disposal
1572                         using (Image image = Image.FromHbitmap (hbitmap)) {
1573                                 //Assert.AreEqual (PixelFormat.Format32bppRgb, image.PixelFormat, "FromHbitmap.PixelFormat");
1574                                 Assert.AreEqual (0, image.Palette.Entries.Length, "FromHbitmap.Palette");
1575                                 Assert.AreEqual (183, image.Height, "FromHbitmap.Height");
1576                                 Assert.AreEqual (173, image.Width, "FromHbitmap.Width");
1577                                 Assert.AreEqual (335888, image.Flags, "FromHbitmap.Flags");
1578                                 Assert.IsTrue (image.RawFormat.Equals (ImageFormat.MemoryBmp), "FromHbitmap.RawFormat");
1579                         }
1580                 }
1581
1582                 [Test]
1583                 public void CreateMultipleBitmapFromSameHBITMAP ()
1584                 {
1585                         IntPtr hbitmap;
1586                         string sInFile = TestBitmap.getInFile ("bitmaps/almogaver24bits.bmp");
1587                         using (Bitmap bitmap = new Bitmap (sInFile)) {
1588                                 Assert.AreEqual (PixelFormat.Format24bppRgb, bitmap.PixelFormat, "Original.PixelFormat");
1589                                 Assert.AreEqual (0, bitmap.Palette.Entries.Length, "Original.Palette");
1590                                 Assert.AreEqual (183, bitmap.Height, "Original.Height");
1591                                 Assert.AreEqual (173, bitmap.Width, "Original.Width");
1592                                 Assert.AreEqual (73744, bitmap.Flags, "Original.Flags");
1593                                 Assert.IsTrue (bitmap.RawFormat.Equals (ImageFormat.Bmp), "Original.RawFormat");
1594                                 hbitmap = bitmap.GetHbitmap ();
1595                         }
1596                         // hbitmap survives original bitmap disposal
1597                         using (Image image = Image.FromHbitmap (hbitmap)) {
1598                                 //Assert.AreEqual (PixelFormat.Format32bppRgb, image.PixelFormat, "1.PixelFormat");
1599                                 Assert.AreEqual (0, image.Palette.Entries.Length, "1.Palette");
1600                                 Assert.AreEqual (183, image.Height, "1.Height");
1601                                 Assert.AreEqual (173, image.Width, "1.Width");
1602                                 Assert.AreEqual (335888, image.Flags, "1.Flags");
1603                                 Assert.IsTrue (image.RawFormat.Equals (ImageFormat.MemoryBmp), "1.RawFormat");
1604                         }
1605                         using (Image image2 = Image.FromHbitmap (hbitmap)) {
1606                                 //Assert.AreEqual (PixelFormat.Format32bppRgb, image2.PixelFormat, "2.PixelFormat");
1607                                 Assert.AreEqual (0, image2.Palette.Entries.Length, "2.Palette");
1608                                 Assert.AreEqual (183, image2.Height, "2.Height");
1609                                 Assert.AreEqual (173, image2.Width, "2.Width");
1610                                 Assert.AreEqual (335888, image2.Flags, "2.Flags");
1611                                 Assert.IsTrue (image2.RawFormat.Equals (ImageFormat.MemoryBmp), "2.RawFormat");
1612                         }
1613                 }
1614 #endif
1615         }
1616 }
1617