示例#1
0
        /// <summary>
        /// Gets the output format.
        /// </summary>
        /// <param name="image">The image.</param>
        /// <param name="outputFormat">The output format.</param>
        /// <returns>ImageFormat.</returns>
        private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, ImageFormat outputFormat)
        {
            switch (outputFormat)
            {
            case ImageFormat.Bmp:
                return(System.Drawing.Imaging.ImageFormat.Bmp);

            case ImageFormat.Gif:
                return(System.Drawing.Imaging.ImageFormat.Gif);

            case ImageFormat.Jpg:
                return(System.Drawing.Imaging.ImageFormat.Jpeg);

            case ImageFormat.Png:
                return(System.Drawing.Imaging.ImageFormat.Png);

            default:
                return(image.RawFormat);
            }
        }
示例#2
0
        public void EncodeImage(string inputPath, string cacheFilePath, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
        {
            var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed > 0;

            using (var originalImage = Image.FromFile(inputPath))
            {
                var newWidth = Convert.ToInt32(width);
                var newHeight = Convert.ToInt32(height);

                // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
                // Also, Webp only supports Format32bppArgb and Format32bppRgb
                var pixelFormat = selectedOutputFormat == ImageFormat.Webp
                    ? PixelFormat.Format32bppArgb
                    : PixelFormat.Format32bppPArgb;

                using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat))
                {
                    // Mono throw an exeception if assign 0 to SetResolution
                    if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
                    {
                        // Preserve the original resolution
                        thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
                    }

                    using (var thumbnailGraph = Graphics.FromImage(thumbnail))
                    {
                        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                        thumbnailGraph.SmoothingMode = SmoothingMode.HighQuality;
                        thumbnailGraph.InterpolationMode = InterpolationMode.HighQualityBicubic;
                        thumbnailGraph.PixelOffsetMode = PixelOffsetMode.HighQuality;
                        thumbnailGraph.CompositingMode = !hasPostProcessing ?
                            CompositingMode.SourceCopy :
                            CompositingMode.SourceOver;

                        SetBackgroundColor(thumbnailGraph, options);

                        thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);

                        DrawIndicator(thumbnailGraph, newWidth, newHeight, options);

                        var outputFormat = GetOutputFormat(originalImage, selectedOutputFormat);

                        _fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFilePath));

                        // Save to the cache location
                        using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
                        {
                            // Save to the memory stream
                            thumbnail.Save(outputFormat, cacheFileStream, quality);
                        }
                    }
                }

            }
        }
示例#3
0
 /// <summary>
 /// Gets the output format.
 /// </summary>
 /// <param name="image">The image.</param>
 /// <param name="outputFormat">The output format.</param>
 /// <returns>ImageFormat.</returns>
 private System.Drawing.Imaging.ImageFormat GetOutputFormat(Image image, ImageFormat outputFormat)
 {
     switch (outputFormat)
     {
         case ImageFormat.Bmp:
             return System.Drawing.Imaging.ImageFormat.Bmp;
         case ImageFormat.Gif:
             return System.Drawing.Imaging.ImageFormat.Gif;
         case ImageFormat.Jpg:
             return System.Drawing.Imaging.ImageFormat.Jpeg;
         case ImageFormat.Png:
             return System.Drawing.Imaging.ImageFormat.Png;
         default:
             return image.RawFormat;
     }
 }
        public void EncodeImage(string inputPath, string cacheFilePath, bool autoOrient, int width, int height, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
        {
            var hasPostProcessing = !string.IsNullOrEmpty(options.BackgroundColor) || options.UnplayedCount.HasValue || options.AddPlayedIndicator || options.PercentPlayed > 0;

            using (var originalImage = Image.FromFile(inputPath))
            {
                var newWidth  = Convert.ToInt32(width);
                var newHeight = Convert.ToInt32(height);

                // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
                // Also, Webp only supports Format32bppArgb and Format32bppRgb
                var pixelFormat = selectedOutputFormat == ImageFormat.Webp
                    ? PixelFormat.Format32bppArgb
                    : PixelFormat.Format32bppPArgb;

                using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat))
                {
                    // Mono throw an exeception if assign 0 to SetResolution
                    if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
                    {
                        // Preserve the original resolution
                        thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
                    }

                    using (var thumbnailGraph = Graphics.FromImage(thumbnail))
                    {
                        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                        thumbnailGraph.SmoothingMode      = SmoothingMode.HighQuality;
                        thumbnailGraph.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        thumbnailGraph.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                        // SourceCopy causes the image to be blank in OSX
                        //thumbnailGraph.CompositingMode = !hasPostProcessing ?
                        //    CompositingMode.SourceCopy :
                        //    CompositingMode.SourceOver;

                        SetBackgroundColor(thumbnailGraph, options);

                        thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);

                        DrawIndicator(thumbnailGraph, newWidth, newHeight, options);

                        var outputFormat = GetOutputFormat(originalImage, selectedOutputFormat);

                        _fileSystem.CreateDirectory(Path.GetDirectoryName(cacheFilePath));

                        // Save to the cache location
                        using (var cacheFileStream = _fileSystem.GetFileStream(cacheFilePath, FileMode.Create, FileAccess.Write, FileShare.Read, false))
                        {
                            // Save to the memory stream
                            thumbnail.Save(outputFormat, cacheFileStream, quality);
                        }
                    }
                }
            }
        }
示例#5
0
        public void EncodeImage(string inputPath, ImageSize?originalImageSize, string outputPath, bool autoOrient, int quality, ImageProcessingOptions options, ImageFormat selectedOutputFormat)
        {
            using (var originalImage = GetImage(inputPath, options.CropWhiteSpace))
            {
                if (options.CropWhiteSpace || !originalImageSize.HasValue)
                {
                    originalImageSize = new ImageSize(originalImage.Width, originalImage.Height);
                }

                var newImageSize = ImageHelper.GetNewImageSize(options, originalImageSize);

                var newWidth  = Convert.ToInt32(Math.Round(newImageSize.Width));
                var newHeight = Convert.ToInt32(Math.Round(newImageSize.Height));

                // Graphics.FromImage will throw an exception if the PixelFormat is Indexed, so we need to handle that here
                // Also, Webp only supports Format32bppArgb and Format32bppRgb
                var pixelFormat = selectedOutputFormat == ImageFormat.Webp
                    ? PixelFormat.Format32bppArgb
                    : PixelFormat.Format32bppPArgb;

                using (var thumbnail = new Bitmap(newWidth, newHeight, pixelFormat))
                {
                    // Mono throw an exeception if assign 0 to SetResolution
                    if (originalImage.HorizontalResolution > 0 && originalImage.VerticalResolution > 0)
                    {
                        // Preserve the original resolution
                        thumbnail.SetResolution(originalImage.HorizontalResolution, originalImage.VerticalResolution);
                    }

                    using (var thumbnailGraph = Graphics.FromImage(thumbnail))
                    {
                        thumbnailGraph.CompositingQuality = CompositingQuality.HighQuality;
                        thumbnailGraph.SmoothingMode      = SmoothingMode.HighQuality;
                        thumbnailGraph.InterpolationMode  = InterpolationMode.HighQualityBicubic;
                        thumbnailGraph.PixelOffsetMode    = PixelOffsetMode.HighQuality;

                        // SourceCopy causes the image to be blank in OSX
                        //thumbnailGraph.CompositingMode = !hasPostProcessing ?
                        //    CompositingMode.SourceCopy :
                        //    CompositingMode.SourceOver;

                        SetBackgroundColor(thumbnailGraph, options);

                        thumbnailGraph.DrawImage(originalImage, 0, 0, newWidth, newHeight);

                        DrawIndicator(thumbnailGraph, newWidth, newHeight, options);

                        var outputFormat = GetOutputFormat(originalImage, selectedOutputFormat);

                        // Save to the cache location
                        using (var cacheFileStream = _fileSystem.GetFileStream(outputPath, FileOpenMode.Create, FileAccessMode.Write, FileShareMode.Read, false))
                        {
                            // Save to the memory stream
                            thumbnail.Save(outputFormat, cacheFileStream, quality);
                        }
                    }
                }
            }
        }