Exemplo n.º 1
0
        [return : NotNullIfNotNull("images")] public static Icon?Combine(Bitmap[]?images, Color[]?transparentColors, bool forceUncompressedResult)
        {
            int imageCount = images?.Length ?? 0;

            int colorCount = transparentColors?.Length ?? 0;

            if (imageCount != colorCount)
            {
                throw new ArgumentException(Res.IconExtensionsImagesColorsDifferentLength);
            }

            if (images == null || transparentColors == null || imageCount == 0)
            {
                return(null);
            }

            using (var rawIcon = new RawIcon())
            {
                for (int i = 0; i < imageCount; i++)
                {
                    rawIcon.Add(images[i], transparentColors[i]);
                }

                return(rawIcon.ToIcon(forceUncompressedResult) !);
            }
        }
Exemplo n.º 2
0
        private static RawIcon?DoGetStockIcon(StockIcon id)
        {
            if (id < 0 || !OSUtils.IsVistaOrLater)
            {
                return(null);
            }

            IntPtr largeHandle = Shell32.GetStockIconHandle(id, SystemIconSize.Large);

            if (largeHandle == IntPtr.Zero)
            {
                return(null);
            }

            var result = new RawIcon(Icon.FromHandle(largeHandle));

            User32.DestroyIcon(largeHandle);

            IntPtr smallHandle = Shell32.GetStockIconHandle(id, SystemIconSize.Small);

            if (largeHandle == IntPtr.Zero)
            {
                return(result);
            }

            result.Add(Icon.FromHandle(smallHandle));
            User32.DestroyIcon(smallHandle);
            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Gets a multi size version of a system icon provided in <paramref name="icon"/> by generating the small version internally.
        /// </summary>
        private static RawIcon ToCombinedIcon(Icon icon)
        {
            var result = new RawIcon(icon);

            if (result.ImageCount == 1)
            {
                using (Bitmap imageLarge = result.ExtractBitmap(0, false) !)
                    using (Bitmap imageSmall = imageLarge.Resize(new Size(16, 16), true))
                        result.Add(imageSmall);
            }

            return(result);
        }
Exemplo n.º 4
0
        [SecurityCritical] // GetHicon
        private static Icon FromBitmap(Bitmap bmp)
        {
            if (OSUtils.IsWindows)
            {
                return(Icon.FromHandle(bmp.GetHicon()).ToManagedIcon());
            }

            using (var rawIcon = new RawIcon())
            {
                rawIcon.Add(bmp);
                return(rawIcon.ToIcon(true) !);
            }
        }
Exemplo n.º 5
0
        [return : NotNullIfNotNull("images")] public static Icon?Combine(bool forceUncompressedResult, IEnumerable <Bitmap>?images)
        {
            if (images == null)
            {
                return(null);
            }

            using (var rawIcon = new RawIcon())
            {
                foreach (Bitmap image in images)
                {
                    rawIcon.Add(image);
                }

                return(rawIcon.ToIcon(forceUncompressedResult) !);
            }
        }
Exemplo n.º 6
0
        public static Icon Combine(bool forceUncompressedResult, IEnumerable <Bitmap> images)
        {
            if (images == null)
            {
                return(null);
            }

            using (RawIcon rawIcon = new RawIcon())
            {
                foreach (Bitmap image in images)
                {
                    rawIcon.Add(image);
                }

                return(rawIcon.ToIcon(forceUncompressedResult));
            }
        }
Exemplo n.º 7
0
        [return : NotNullIfNotNull("icons")] public static Icon?Combine(bool forceUncompressedResult, IEnumerable <Icon>?icons)
        {
            if (icons == null)
            {
                return(null);
            }

            using (var rawIcon = new RawIcon())
            {
                foreach (Icon icon in icons)
                {
                    rawIcon.Add(icon);
                }

                return(rawIcon.ToIcon(forceUncompressedResult) !);
            }
        }
Exemplo n.º 8
0
        public static Icon Combine(bool forceUncompressedResult, IEnumerable <Icon> icons)
        {
            if (icons == null)
            {
                return(null);
            }

            using (RawIcon rawIcon = new RawIcon())
            {
                foreach (Icon icon in icons)
                {
                    rawIcon.Add(icon);
                }

                return(rawIcon.ToIcon(forceUncompressedResult));
            }
        }
Exemplo n.º 9
0
        public void AddMultiResBitmapTest()
        {
            var ms       = new MemoryStream();
            var refImage = Icons.Information.ToMultiResBitmap();

            using (RawIcon icon = new RawIcon())
            {
                icon.Add(refImage);
                icon.Save(ms, true);
            }

            ms.Position = 0;
            var bmp = new Bitmap(ms);

            Assert.AreEqual(ImageFormat.Icon, bmp.RawFormat);
            Assert.AreEqual(PixelFormat.Format32bppArgb, bmp.PixelFormat);
            Assert.AreEqual(OSUtils.IsWindows ? 7 : 1, bmp.ExtractIconImages().Length);
            SaveImage("result", bmp, true);
        }
Exemplo n.º 10
0
        public void AddBitmapCustomBackgroundSaveUncompressedTest(string testName, PixelFormat pixelFormat, uint backColor)
        {
            var ms       = new MemoryStream();
            var size     = OSUtils.IsWindows ? new Size(256, 256) : new Size(64, 64);
            var refImage = Icons.Information.ExtractBitmap(size).ConvertPixelFormat(pixelFormat);

            using (RawIcon icon = new RawIcon())
            {
                icon.Add(refImage, Color.FromArgb((int)backColor));
                icon.Save(ms, true);
            }

            ms.Position = 0;
            var bmp = new Bitmap(ms);

            Assert.AreEqual(ImageFormat.Icon, bmp.RawFormat);
            Assert.AreEqual(PixelFormat.Format32bppArgb, bmp.PixelFormat);

            SaveImage(testName, bmp, true);
        }
Exemplo n.º 11
0
        public void AddBitmapSaveUncompressedTest(PixelFormat pixelFormat)
        {
            var        ms        = new MemoryStream();
            IQuantizer quantizer = pixelFormat.IsIndexed() ? OptimizedPaletteQuantizer.Octree(1 << pixelFormat.ToBitsPerPixel()) : null;
            var        size      = OSUtils.IsWindows ? new Size(256, 256) : new Size(64, 64);
            var        refImage  = Convert(Icons.Information.ExtractBitmap(size), pixelFormat, quantizer);

            using (RawIcon icon = new RawIcon())
            {
                icon.Add(refImage);
                icon.Save(ms, true);
            }

            ms.Position = 0;
            var bmp = new Bitmap(ms);

            Assert.AreEqual(ImageFormat.Icon, bmp.RawFormat);
            Assert.AreEqual(PixelFormat.Format32bppArgb, bmp.PixelFormat);
            Assert.IsTrue(!pixelFormat.IsIndexed() && !pixelFormat.HasAlpha() || bmp.GetPixel(0, 0).A == 0, "Transparency expected");
            SaveImage($"{pixelFormat}", bmp, true);
        }
Exemplo n.º 12
0
        public void AddLarge24BppBitmapTest()
        {
            var ms = new MemoryStream();

            using (var raw = new RawIcon())
            {
                raw.Add(Icons.Information.ExtractBitmap(new Size(256, 256)).ConvertPixelFormat(PixelFormat.Format24bppRgb), Color.Black);
                raw.Save(ms, false);
            }

            // <32 BPP icons are always saved uncompressed
            ms.Position = 0;

            AssertPlatformDependent(() =>
            {
                var icon = new Icon(ms);
                Assert.AreEqual(24, icon.GetBitsPerPixel());
                Assert.IsFalse(icon.IsCompressed());
                SaveIcon("result", icon);
            });
        }