示例#1
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);
            }
        }
示例#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
 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);
        }