コード例 #1
0
        /// <summary>
        /// Resizes the image and encodes the BlurHash representation of the image.
        /// </summary>
        /// <param name="xComponent">The number x components.</param>
        /// <param name="yComponent">The number y components.</param>
        /// <param name="filename">The path to an encoded image on the file system.</param>
        /// <param name="maxWidth">The maximum width to resize the image to.</param>
        /// <param name="maxHeight">The maximum height to resize the image to.</param>
        /// <returns>BlurHash representation of the image.</returns>
        public static string Encode(int xComponent, int yComponent, string filename, int maxWidth, int maxHeight)
        {
            using (var bitmap = new Bitmap(filename))
            {
                var(scaledWidth, scaledHeight) = ScaleHelper.GetScaleDimensions(bitmap.Width, bitmap.Height, maxWidth, maxHeight);

                using (var scaledBitmap = new Bitmap(bitmap, new Size(scaledWidth, scaledHeight)))
                {
                    if (scaledBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format24bppRgb)
                    {
                        return(EncodeInternal(xComponent, yComponent, scaledBitmap, PixelFormat.BGR888));
                    }
                    else if (scaledBitmap.PixelFormat == System.Drawing.Imaging.PixelFormat.Format32bppArgb)
                    {
                        return(EncodeInternal(xComponent, yComponent, scaledBitmap, PixelFormat.BGR888x));
                    }

                    using (var temporaryBitmap = new Bitmap(scaledBitmap.Width, scaledBitmap.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb))
                    {
                        using (var graphics = Graphics.FromImage(temporaryBitmap))
                        {
                            graphics.DrawImageUnscaled(scaledBitmap, 0, 0);
                        }

                        return(EncodeInternal(xComponent, yComponent, temporaryBitmap, PixelFormat.BGR888));
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Resizes the image and encodes the BlurHash representation of the image.
        /// </summary>
        /// <param name="xComponent">The number x components.</param>
        /// <param name="yComponent">The number y components.</param>
        /// <param name="filename">The path to an encoded image on the file system.</param>
        /// <param name="maxWidth">The maximum width to resize the image to.</param>
        /// <param name="maxHeight">The maximum height to resize the image to.</param>
        /// <returns>BlurHash representation of the image.</returns>
        public static string Encode(int xComponent, int yComponent, string filename, int maxWidth, int maxHeight)
        {
            using (SKCodec codec = SKCodec.Create(filename))
            {
                var   width       = codec.Info.Width;
                var   height      = codec.Info.Height;
                float scaleFactor = 0;
                if (width > maxWidth || height > maxHeight)
                {
                    scaleFactor = ScaleHelper.GetScale(width, height, maxWidth, maxHeight);
                    SKSizeI supportedScale = codec.GetScaledDimensions(scaleFactor);
                    width  = supportedScale.Width;
                    height = supportedScale.Height;
                }

                var newInfo = new SKImageInfo()
                {
                    Width      = width,
                    Height     = height,
                    ColorType  = SKColorType.Rgba8888,
                    AlphaType  = SKAlphaType.Unpremul,
                    ColorSpace = SKColorSpace.CreateSrgb()
                };

                using (SKBitmap bitmap = SKBitmap.Decode(codec, newInfo))
                {
                    if (scaleFactor == 0)
                    {
                        return(EncodeInternal(xComponent, yComponent, bitmap));
                    }

                    var(scaledWidth, scaledHeight) = ScaleHelper.GetScaleDimensions(bitmap.Width, bitmap.Height, scaleFactor);

                    newInfo = newInfo.WithSize(scaledWidth, scaledHeight);

                    using (SKBitmap scaledBitmap = bitmap.Resize(newInfo, SKFilterQuality.Low))
                    {
                        return(EncodeInternal(xComponent, yComponent, scaledBitmap));
                    }
                }
            }
        }
コード例 #3
0
 public void Scale_Success(int width, int height, int maxWidth, int maxHeight, int scaledWidth, int scaledHeight)
 {
     var(w, h) = ScaleHelper.GetScaleDimensions(width, height, maxWidth, maxHeight);
     Assert.Equal(w, scaledWidth);
     Assert.Equal(h, scaledHeight);
 }