Example #1
0
        /// <summary>
        /// Computes the result icon image size.
        /// </summary>
        /// <param name="originalSize">The original size.</param>
        /// <param name="parameter">The conversion parameter.</param>
        /// <returns>Icon image size.</returns>
        /// <seealso cref="ResizeKind"/>
        private Size ComputeIconSize(Size originalSize, ConversionParameter parameter)
        {
            // If the resize kind is None, do nothing.
            if (parameter.ResizeKind == ResizeKind.None)
            {
                return(new Size(originalSize.Width, originalSize.Height));
            }

            // If the original image size is smaller then MaxSize, do noting.
            if (originalSize.Width <= parameter.MaxSize && originalSize.Height <= parameter.MaxSize)
            {
                return(new Size(originalSize.Width, originalSize.Height));
            }

            var isMaximumSmaller = parameter.ResizeKind == ResizeKind.KeepAspect ||
                                   parameter.ResizeKind == ResizeKind.MaximumSizeSmallerThanSquare;

            return(Resize(originalSize, parameter.MaxSize, isMaximumSmaller));
        }
Example #2
0
        /// <summary>
        /// Convert an image to the image whose format is ".ico".
        /// </summary>
        /// <param name="image">Source image.</param>
        /// <param name="parameter">Conversion parameter. <see cref="ConversionParameter"/></param>
        /// <returns>Icon image.</returns>
        public Image ConvertImageToIconBitmap(Image image, ConversionParameter parameter)
        {
            var iconSize = ComputeIconSize(new Size(image.Width, image.Height), parameter);

            var edgeLength = Math.Max(iconSize.Width, iconSize.Height);

            using var bmp = new Bitmap(edgeLength, edgeLength);

            using (var g = Graphics.FromImage(bmp))
            {
                g.PageUnit = GraphicsUnit.Pixel;
                var offsetX = (int)(Math.Round((bmp.Width - iconSize.Width) / 2.0));
                var offsetY = (int)(Math.Round((bmp.Height - iconSize.Height) / 2.0));
                g.DrawImage(image, new Rectangle(offsetX, offsetY, iconSize.Width, iconSize.Height),
                            new Rectangle(0, 0, image.Width, image.Height), GraphicsUnit.Pixel);
            }

            var filePath = CreateTempFilePath();

            bmp.Save(filePath, ImageFormat.Icon);
            return(new Bitmap(new MemoryStream(_fileSystem.File.ReadAllBytes(filePath))));
        }
Example #3
0
        public Image CreateIconImageFromFilePath(string sourceImageFilePath, ConversionParameter parameter)
        {
            var image = LoadImage(sourceImageFilePath);

            return(ConvertImageToIconBitmap(image, parameter));
        }