コード例 #1
0
ファイル: PngQuantOptimizer.cs プロジェクト: jammykam/Dianoga
        public override IOptimizerResult Optimize(MediaStream stream)
        {
            var quantizer = new WuQuantizer();

            var memoryStream = new MemoryStream();

            using (var bitmap = new Bitmap(stream.Stream))
            {
                var bitDepth = Image.GetPixelFormatSize(bitmap.PixelFormat);
                if (bitDepth != 32)
                {
                    return OptimizerFailureResult("the image you are attempting to quantize does not contain a 32 bit ARGB palette. This image has a bit depth of {0} with {1} colors".FormatWith(bitDepth, bitmap.Palette.Entries.Length));
                }

                using (var quantized = quantizer.QuantizeImage(bitmap))
                {
                    quantized.Save(memoryStream, ImageFormat.Png);
                }

                // rewind the stream
                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = new PngQuantOptimizerResult();
                result.Success = true;
                result.SizeBefore = (int)stream.Length;
                result.SizeAfter = (int)memoryStream.Length;
                result.ResultStream = memoryStream;

                return OptimizationSuccessful(result);
            }
        }
コード例 #2
0
        public override IOptimizerResult Optimize(MediaStream stream)
        {
            var quantizer = new WuQuantizer();

            var memoryStream = new MemoryStream();

            using (var bitmap = new Bitmap(stream.Stream))
            {
                var bitDepth = Image.GetPixelFormatSize(bitmap.PixelFormat);
                if (bitDepth != 32)
                {
                    return(OptimizerFailureResult("the image you are attempting to quantize does not contain a 32 bit ARGB palette. This image has a bit depth of {0} with {1} colors".FormatWith(bitDepth, bitmap.Palette.Entries.Length)));
                }

                using (var quantized = quantizer.QuantizeImage(bitmap))
                {
                    quantized.Save(memoryStream, ImageFormat.Png);
                }

                // rewind the stream
                memoryStream.Seek(0, SeekOrigin.Begin);

                var result = new PngQuantOptimizerResult();
                result.Success      = true;
                result.SizeBefore   = (int)stream.Length;
                result.SizeAfter    = (int)memoryStream.Length;
                result.ResultStream = memoryStream;

                return(OptimizationSuccessful(result));
            }
        }
コード例 #3
0
ファイル: PngQuantOptimizer.cs プロジェクト: jammykam/Dianoga
 private IOptimizerResult OptimizerFailureResult(string message)
 {
     var result = new PngQuantOptimizerResult()
     {
         Success = false,
         ErrorMessage = message
     };
     return result;
 }
コード例 #4
0
        private IOptimizerResult OptimizerFailureResult(string message)
        {
            var result = new PngQuantOptimizerResult()
            {
                Success      = false,
                ErrorMessage = message
            };

            return(result);
        }