+ // note: the Icon(string,Size) or Icon(string,int,int) doesn't exists under 1.x
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/32bpp.ico"))) {
+ using (Icon xp = new Icon (fs, size, size)) {
+ Assert.AreEqual (size, xp.Height, "Height");
+ Assert.AreEqual (size, xp.Width, "Width");
+ Assert.AreEqual (size, xp.Size.Width, "Size.Width");
+ Assert.AreEqual (size, xp.Size.Height, "Size.Height");
+
+ Bitmap bmp = xp.ToBitmap ();
+ Assert.AreEqual (size, bmp.Height, "Bitmap.Height");
+ Assert.AreEqual (size, bmp.Width, "Bitmap.Width");
+ Assert.AreEqual (size, bmp.Size.Width, "Bitmap.Size.Width");
+ Assert.AreEqual (size, bmp.Size.Height, "Bitmap.Size.Height");
+ }
+ }
+ }
+
+ [Test]
+ public void Icon32bits_XP16 ()
+ {
+ XPIcon (16);
+ }
+
+ [Test]
+ public void Icon32bits_XP32 ()
+ {
+ XPIcon (32);
+ }
+
+ [Test]
+ public void Icon32bits_XP48 ()
+ {
+ XPIcon (48);
+ }
+
+ [Test]
+#if TARGET_JVM
+ [Ignore ("SelectFromUnusualSize_Small16 Not Working")]
+#endif
+ public void SelectFromUnusualSize_Small16 ()
+ {
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
+ using (Icon xp = new Icon (fs, 16, 16)) {
+ Assert.AreEqual (16, xp.Height, "Height");
+ Assert.AreEqual (10, xp.Width, "Width");
+ Assert.AreEqual (10, xp.Size.Width, "Size.Width");
+ Assert.AreEqual (16, xp.Size.Height, "Size.Height");
+ }
+ }
+ }
+
+ [Test]
+ public void SelectFromUnusualSize_Normal32 ()
+ {
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/80509.ico"))) {
+ using (Icon xp = new Icon (fs, 32, 32)) {
+ Assert.AreEqual (22, xp.Height, "Height");
+ Assert.AreEqual (11, xp.Width, "Width");
+ Assert.AreEqual (11, xp.Size.Width, "Size.Width");
+ Assert.AreEqual (22, xp.Size.Height, "Size.Height");
+ }
+ }
+ }
+
+ internal static void SaveAndCompare (string msg, Icon icon, bool alpha)
+ {
+ using (MemoryStream ms = new MemoryStream ()) {
+ icon.Save (ms);
+ ms.Position = 0;
+
+ using (Icon loaded = new Icon (ms)) {
+ Assert.AreEqual (icon.Height, loaded.Height, msg + ".Loaded.Height");
+ Assert.AreEqual (icon.Width, loaded.Width, msg + ".Loaded.Width");
+
+ using (Bitmap expected = icon.ToBitmap ()) {
+ using (Bitmap actual = loaded.ToBitmap ()) {
+ Assert.AreEqual (expected.Height, actual.Height, msg + ".Bitmap.Height");
+ Assert.AreEqual (expected.Width, actual.Width, msg + ".Bitmap.Width");
+
+ for (int y = 0; y < expected.Height; y++) {
+ for (int x = 0; x < expected.Width; x++) {
+ Color e = expected.GetPixel (x, y);
+ Color a = actual.GetPixel (x, y);
+ if (alpha)
+ Assert.AreEqual (e.A, a.A, String.Format ("{0}:{1}x{2}:A", msg, x, y));
+ Assert.AreEqual (e.R, a.R, String.Format ("{0}:{1}x{2}:R", msg, x, y));
+ Assert.AreEqual (e.G, a.G, String.Format ("{0}:{1}x{2}:G", msg, x, y));
+ Assert.AreEqual (e.B, a.B, String.Format ("{0}:{1}x{2}:B", msg, x, y));
+ }
+ }
+ }
+ }
+ }
+ }
+ }
+
+ [Test]
+ public void Save ()
+ {
+ SaveAndCompare ("16", icon16, true);
+ SaveAndCompare ("32", icon32, true);
+ SaveAndCompare ("48", icon48, true);
+ SaveAndCompare ("64", icon64, true);
+ SaveAndCompare ("96", icon96, true);
+ }
+
+ [Test] // bug #410608
+ public void Save_256 ()
+ {
+ string filepath = TestBitmap.getInFile ("bitmaps/323511.ico");
+
+ using (Icon icon = new Icon (filepath)) {
+ // bug #415809 fixed
+ SaveAndCompare ("256", icon, true);
+ }
+
+ // binary comparison
+ var orig = new MemoryStream (File.ReadAllBytes (filepath));
+ var saved = new MemoryStream ();
+ using (Icon icon = new Icon (filepath))
+ icon.Save (saved);
+ FileAssert.AreEqual (orig, saved, "binary comparison");
+ }
+
+ [Test]
+ [ExpectedException (typeof (NullReferenceException))]
+#if TARGET_JVM
+ [Ignore ("Throws NullReference, do we need to follow?")]
+#endif
+ public void Save_Null ()
+ {
+ icon.Save (null);
+ }
+
+ [Test]
+#if TARGET_JVM
+ [Ignore ("Icon16ToBitmap Not Working")]
+#endif
+ public void Icon16ToBitmap ()
+ {
+ using (Bitmap b = icon16.ToBitmap ()) {
+ Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+ // unlike the GDI+ icon decoder the palette isn't kept
+ Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+ Assert.AreEqual (icon16.Height, b.Height, "Height");
+ Assert.AreEqual (icon16.Width, b.Width, "Width");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+ Assert.AreEqual (2, b.Flags, "Flags");
+ }
+ }
+
+ [Test]
+#if TARGET_JVM
+ [Ignore ("Icon32ToBitmap Not Working")]
+#endif
+ public void Icon32ToBitmap ()
+ {
+ using (Bitmap b = icon32.ToBitmap ()) {
+ Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+ // unlike the GDI+ icon decoder the palette isn't kept
+ Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+ Assert.AreEqual (icon32.Height, b.Height, "Height");
+ Assert.AreEqual (icon32.Width, b.Width, "Width");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+ Assert.AreEqual (2, b.Flags, "Flags");
+ }
+ }
+
+ [Test]
+#if TARGET_JVM
+ [Ignore ("Icon48ToBitmap Not Working")]
+#endif
+ public void Icon48ToBitmap ()
+ {
+ using (Bitmap b = icon48.ToBitmap ()) {
+ Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+ // unlike the GDI+ icon decoder the palette isn't kept
+ Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+ Assert.AreEqual (icon48.Height, b.Height, "Height");
+ Assert.AreEqual (icon48.Width, b.Width, "Width");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+ Assert.AreEqual (2, b.Flags, "Flags");
+ }
+ }
+
+ [Test]
+#if TARGET_JVM
+ [Ignore ("Icon64ToBitmap Not Working")]
+#endif
+ public void Icon64ToBitmap ()
+ {
+ using (Bitmap b = icon64.ToBitmap ()) {
+ Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+ // unlike the GDI+ icon decoder the palette isn't kept
+ Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+ Assert.AreEqual (icon64.Height, b.Height, "Height");
+ Assert.AreEqual (icon64.Width, b.Width, "Width");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+ Assert.AreEqual (2, b.Flags, "Flags");
+ }
+ }
+
+ [Test]
+#if TARGET_JVM
+ [Ignore ("Icon96ToBitmap Not Working")]
+#endif
+ public void Icon96ToBitmap ()
+ {
+ using (Bitmap b = icon96.ToBitmap ()) {
+ Assert.AreEqual (PixelFormat.Format32bppArgb, b.PixelFormat, "PixelFormat");
+ // unlike the GDI+ icon decoder the palette isn't kept
+ Assert.AreEqual (0, b.Palette.Entries.Length, "Palette");
+ Assert.AreEqual (icon96.Height, b.Height, "Height");
+ Assert.AreEqual (icon96.Width, b.Width, "Width");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "RawFormat");
+ Assert.AreEqual (2, b.Flags, "Flags");
+ }
+ }
+
+ [Test] // bug #415581
+#if TARGET_JVM
+ [Ignore ("Icon256ToBitmap Not Working")]
+#endif
+ public void Icon256ToBitmap ()
+ {
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
+ Icon icon = new Icon (fs, 48, 48);
+ using (Bitmap b = icon.ToBitmap ()) {
+ Assert.AreEqual (0, b.Palette.Entries.Length, "#A1");
+ Assert.AreEqual (48, b.Height, "#A2");
+ Assert.AreEqual (48, b.Width, "#A3");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#A4");
+ Assert.AreEqual (2, b.Flags, "#A5");
+ }
+ icon.Dispose ();
+ }
+
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
+ Icon icon = new Icon (fs, 256, 256);
+ using (Bitmap b = icon.ToBitmap ()) {
+ Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
+ Assert.AreEqual (48, b.Height, "#B2");
+ Assert.AreEqual (48, b.Width, "#B3");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
+ Assert.AreEqual (2, b.Flags, "#B5");
+ }
+ }
+ }
+
+ [Test]
+ public void Icon256ToBitmap_Request0 ()
+ {
+ // 415581.ico has 2 images, the 256 and 48
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/415581.ico"))) {
+ Icon icon = new Icon (fs, 0, 0);
+ using (Bitmap b = icon.ToBitmap ()) {
+ Assert.AreEqual (0, b.Palette.Entries.Length, "#B1");
+ Assert.AreEqual (48, b.Height, "#B2");
+ Assert.AreEqual (48, b.Width, "#B3");
+ Assert.IsTrue (b.RawFormat.Equals (ImageFormat.MemoryBmp), "#B4");
+ Assert.AreEqual (2, b.Flags, "#B5");
+ }
+ }
+ }
+
+ [Test, ExpectedException ()] //ToDo: System.ComponentModel.Win32Exception
+ public void Only256InFile ()
+ {
+ using (FileStream fs = File.OpenRead (TestBitmap.getInFile ("bitmaps/only256.ico"))) {
+ Icon icon = new Icon (fs, 0, 0);
+ }
+ }
+
+
+#if NET_2_0
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+#if TARGET_JVM
+ [Ignore ("ExtractAssociatedIcon is not implemented")]
+#endif
+ public void ExtractAssociatedIcon_Null ()
+ {
+#if !TARGET_JVM
+ Icon.ExtractAssociatedIcon (null);
+#endif
+ }
+
+ [Test]
+ [ExpectedException (typeof (ArgumentException))]
+#if TARGET_JVM
+ [Ignore ("ExtractAssociatedIcon is not implemented")]
+#endif
+ public void ExtractAssociatedIcon_Empty ()
+ {
+#if !TARGET_JVM
+ Icon.ExtractAssociatedIcon (String.Empty);
+#endif
+ }
+
+ [Test]
+ [ExpectedException (typeof (FileNotFoundException))]
+#if TARGET_JVM
+ [Ignore ("ExtractAssociatedIcon is not implemented")]
+#endif
+ public void ExtractAssociatedIcon_DoesNotExists ()
+ {
+#if !TARGET_JVM
+ Icon.ExtractAssociatedIcon ("does-not-exists.png");
+#endif
+ }
+#endif
+
+ private static bool RunningOnUnix {
+ get {
+ int p = (int) Environment.OSVersion.Platform;
+
+ return (p == 4) || (p == 6) || (p == 128);
+ }
+ }
+ }
+
+ [TestFixture]
+#if TARGET_JVM
+ [Ignore ("Unsafe code is not supported")]
+#endif
+ public class IconFullTrustTest {
+#if !TARGET_JVM
+#if NET_2_0
+ [Test]
+ public void ExtractAssociatedIcon ()
+ {
+ string filename_dll = Assembly.GetExecutingAssembly ().Location;
+ Assert.IsNotNull (Icon.ExtractAssociatedIcon (filename_dll), "dll");
+ }
+#endif
+
+ [Test]
+ public void HandleRoundtrip ()
+ {
+ IntPtr handle;
+ using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+ Assert.AreEqual (16, icon.Height, "Original.Height");
+ Assert.AreEqual (16, icon.Width, "Original.Width");
+ handle = icon.Handle;
+ using (Icon icon2 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (16, icon2.Height, "FromHandle.Height");
+ Assert.AreEqual (16, icon2.Width, "FromHandle.Width");
+ Assert.AreEqual (handle, icon2.Handle, "FromHandle.Handle");
+ IconTest.SaveAndCompare ("Handle", icon2, false);
+ }
+ }
+ // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
+ // commented / using freed memory is risky ;-)
+ /*using (Icon icon3 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (0, icon3.Height, "Survivor.Height");
+ Assert.AreEqual (0, icon3.Width, "Survivor.Width");
+ Assert.AreEqual (handle, icon3.Handle, "Survivor.Handle");
+ }*/
+ }
+
+ [Test]
+ public void CreateMultipleIconFromSameHandle ()
+ {
+ IntPtr handle;
+ using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+ Assert.AreEqual (16, icon.Height, "Original.Height");
+ Assert.AreEqual (16, icon.Width, "Original.Width");
+ handle = icon.Handle;
+ using (Icon icon2 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (16, icon2.Height, "2.Height");
+ Assert.AreEqual (16, icon2.Width, "2.Width");
+ Assert.AreEqual (handle, icon2.Handle, "2.Handle");
+ IconTest.SaveAndCompare ("Handle2", icon2, false);
+ }
+ using (Icon icon3 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (16, icon3.Height, "3.Height");
+ Assert.AreEqual (16, icon3.Width, "3.Width");
+ Assert.AreEqual (handle, icon3.Handle, "3.Handle");
+ IconTest.SaveAndCompare ("Handle3", icon3, false);
+ }
+ }
+ // unlike other cases (HICON, HBITMAP) handle DOESN'T survives original icon disposal
+ // commented / using freed memory is risky ;-)
+ }
+
+ [Test]
+ public void HiconRoundtrip ()
+ {
+ IntPtr handle;
+ using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+ Assert.AreEqual (16, icon.Height, "Original.Height");
+ Assert.AreEqual (16, icon.Width, "Original.Width");
+ handle = icon.ToBitmap ().GetHicon ();
+ }
+ // HICON survives
+ using (Icon icon2 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (16, icon2.Height, "Survivor.Height");
+ Assert.AreEqual (16, icon2.Width, "Survivor.Width");
+ Assert.AreEqual (handle, icon2.Handle, "Survivor.Handle");
+ IconTest.SaveAndCompare ("HICON", icon2, false);
+ }
+ }
+
+ [Test]
+ public void CreateMultipleIconFromSameHICON ()
+ {
+ IntPtr handle;
+ using (Icon icon = new Icon (TestBitmap.getInFile ("bitmaps/16x16x16.ico"))) {
+ Assert.AreEqual (16, icon.Height, "Original.Height");
+ Assert.AreEqual (16, icon.Width, "Original.Width");
+ handle = icon.ToBitmap ().GetHicon ();
+ }
+ // HICON survives
+ using (Icon icon2 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (16, icon2.Height, "2.Height");
+ Assert.AreEqual (16, icon2.Width, "2.Width");
+ Assert.AreEqual (handle, icon2.Handle, "2.Handle");
+ IconTest.SaveAndCompare ("HICON2", icon2, false);
+ }
+ using (Icon icon3 = Icon.FromHandle (handle)) {
+ Assert.AreEqual (16, icon3.Height, "3.Height");
+ Assert.AreEqual (16, icon3.Width, "3.Width");
+ Assert.AreEqual (handle, icon3.Handle, "3.Handle");
+ IconTest.SaveAndCompare ("HICON", icon3, false);
+ }