Exemple #1
0
        public static void WriteImageThumbnailToFile(byte[] imageData, string path, int width = 128, int height = 96)
        {
            WrappedImage wi = null;

            try
            {
                if (imageData == null || imageData.Length == 0)
                {
                    return;
                }
                FileInfo file = new FileInfo(path);
                Util.EnsureDirectoryExists(file.Directory.FullName);
                wi = new WrappedImage(imageData);
                File.WriteAllBytes(file.FullName, wi.ToByteArray(ImageFormat.Jpeg));
            }
            catch (Exception ex)
            {
                Logger.Debug(ex);
            }
            finally
            {
                if (wi != null)
                {
                    wi.Dispose();
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Converts the image to the specified format.
        /// </summary>
        /// <param name="img">Source image</param>
        /// <param name="size">Percentage size.  100 does not affect image size and is the most efficient option.  This parameter is overridden if maxWidth and maxHeight are both specified.</param>
        /// <param name="quality">Compression quality.  Acceptable range depends on image format:
        /// JPG: 0 to 100
        /// WebP: 0 to 100 or -1 for lossless encode
        /// PNG: No effect</param>
        /// <param name="format">Jpeg, PNG, or WebP</param>
        /// <param name="rotateFlipType">Image rotation and flipping options.</param>
        /// <param name="maxWidth">Maximum width of the output image.  Original image aspect ratio will be preserved.  Requires maxHeight to be specified also.  Overrides the "size" parameter.</param>
        /// <param name="maxHeight">Maximum height of the output image.  Original image aspect ratio will be preserved.  Requires maxWidth to be specified also.  Overrides the "size" parameter.</param>
        /// <param name="special">A string indicating a special image transformation, if any.</param>
        /// <returns>A byte array containing image data in the specified format.</returns>
        /// <remarks>This is called internally by ConvertImage(...)</remarks>
        public static byte[] EncodeImage(WrappedImage img, int size = 100, int quality = 80, ImageFormat format = ImageFormat.Jpeg, RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone, int maxWidth = 0, int maxHeight = 0, string special = "")
        {
            byte[] output = new byte[0];
            if (size < 1)
            {
                size = 1;
            }
            if (size > 100)
            {
                size = 100;
            }

            // Calculate new size
            int w = img.Width;
            int h = img.Height;

            if (size < 100)
            {
                w = Math.Max(1, (int)(img.Width * (size / 100.0)));
                h = Math.Max(1, (int)(img.Height * (size / 100.0)));
            }
            if (maxWidth > 0 && maxHeight > 0)
            {
                if (w > maxWidth)
                {
                    double diff = w / maxWidth;
                    w = maxWidth;
                    h = (int)(h / diff);
                }
                if (h > maxHeight)
                {
                    double diff = h / maxHeight;
                    h = maxHeight;
                    w = (int)(w / diff);
                }
            }
            if (w != img.Width || h != img.Height)
            {
                img.Resize(w, h);
            }
            if (rotateFlipType != RotateFlipType.RotateNoneFlipNone)
            {
                img.RotateFlip(rotateFlipType);
            }

            if (special == "red")
            {
                img.TurnRed();
            }

            output = img.ToByteArray(format, quality);
            return(output);
        }
Exemple #3
0
        /// <summary>
        /// Converts the image to the specified format.
        /// </summary>
        /// <param name="input">byte array containing the source image data</param>
        /// <param name="size">Percentage size.  100 does not affect image size and is the most efficient option.  This parameter is overridden if maxWidth and maxHeight are both specified.</param>
        /// <param name="quality">Compression quality.  Acceptable range depends on image format:
        /// JPG: 0 to 100
        /// WebP: 0 to 100 or -1 for lossless encode
        /// PNG: No effect</param>
        /// <param name="format">Jpeg, PNG, or WebP</param>
        /// <param name="rotateFlipType">Image rotation and flipping options.</param>
        /// <param name="maxWidth">Maximum width of the output image.  Original image aspect ratio will be preserved.  Requires maxHeight to be specified also.  Overrides the "size" parameter.</param>
        /// <param name="maxHeight">Maximum height of the output image.  Original image aspect ratio will be preserved.  Requires maxWidth to be specified also.  Overrides the "size" parameter.</param>
        /// <param name="srcImageFormat">The image format of the source data.  If this format matches the "format" parameter and no other operations are requested, the image may not be re-encoded at all.</param>
        /// <param name="special">A string indicating a special image transformation, if any.</param>
        /// <returns>A byte array containing image data in the specified format.</returns>
        /// <remarks>Calls EncodeBitmap(...) internally.</remarks>
        public static byte[] ConvertImage(byte[] input, int size = 100, int quality = 80, ImageFormat format = ImageFormat.Jpeg, RotateFlipType rotateFlipType = RotateFlipType.RotateNoneFlipNone, int maxWidth = 0, int maxHeight = 0, ImageFormat srcImageFormat = ImageFormat.Jpeg, string special = "", string errorHelp = "")
        {
            if (srcImageFormat == ImageFormat.Webp)
            {
                return(input);                // Cannot currently read WebP images
            }
            byte[] output = input;
            if (input.Length == 0)
            {
                return(input);
            }

            WrappedImage wi = null;

            try
            {
                wi = new WrappedImage(input);
                byte[] encoded = EncodeImage(wi, size, quality, format, rotateFlipType, maxWidth, maxHeight, special);
                if (encoded.Length > 0)
                {
                    output = encoded;
                }
            }
            catch (Exception ex)
            {
                StringBuilder inputstart = new StringBuilder();
                for (int i = 0; i < input.Length && i < 50; i++)
                {
                    inputstart.Append(input[i]).Append(',');
                }
                StringBuilder inputend = new StringBuilder();
                for (int i = input.Length - 50; i < input.Length && i > 0; i++)                 // If i <= 0, inputend would just be the same as inputstart anyway.  This would happen if input.Length was <= 50.
                {
                    inputend.Append(input[i]).Append(',');
                }
                Logger.Debug(ex, errorHelp + " - input length: " + input.Length + ", " + inputstart.ToString() + "  " + inputend.ToString());
            }
            finally
            {
                if (wi != null)
                {
                    wi.Dispose();
                }
            }
            return(output);
        }