Example #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="format"></param>
        /// <param name="img"></param>
        /// <param name="target"></param>
        /// <param name="colors"></param>
        /// <param name="dither"></param>
        /// <param name="fourPass"></param>
        /// <param name="ditherPercent"></param>
        /// <param name="useGdiQuantization"></param>
        public void SaveIndexed(System.Drawing.Imaging.ImageFormat format, Image img, Stream target, byte colors, bool dither, bool fourPass, int ditherPercent)
        {
            if (PreservePalette && originalPalette != null)
            {
                img.Palette = originalPalette;
                //If we are encoding in PNG, and writing to a non-seekable stream,
                //we have to buffer it all in memory or we'll get an exception
                if (!target.CanSeek && ImageFormat.Png.Equals(format))
                {
                    using (MemoryStream ms = new MemoryStream(4096)) {
                        img.Save(ms, format); //Recursive call
                        ms.WriteTo(target);
                    }
                }
                else
                {
                    //Everything else
                    img.Save(target, format);
                }
                return;
            }


            OctreeQuantizer quantizer = new OctreeQuantizer(colors, GetBitsNeededForColorDepth(colors));

            if (query.Get <bool>("fulltrust", HasFullTrust))
            {
                quantizer.Dither        = dither;
                quantizer.FourPass      = fourPass;
                quantizer.DitherPercent = (float)ditherPercent / 100;
            }
            else
            {
                quantizer.FullTrust          = false;
                quantizer.OmitFinalStage     = true;
                quantizer.ResizeForFirstPass = true;

                quantizer.FirstPassPixelCount     = (long)Math.Pow(query.Get <int>("pixelCount", (int)Math.Sqrt(quantizer.FirstPassPixelCount)), 2);
                quantizer.FirstPassPixelThreshold = (long)Math.Pow(query.Get <int>("pixelThreshold", (int)Math.Sqrt(quantizer.FirstPassPixelThreshold)), 2);
            }
            using (Bitmap quantized = quantizer.Quantize(img)) {
                //If we are encoding in PNG, and writing to a non-seekable stream,
                //we have to buffer it all in memory or we'll get an exception
                if (!target.CanSeek && ImageFormat.Png.Equals(format))
                {
                    using (MemoryStream ms = new MemoryStream(4096)) {
                        quantized.Save(ms, format); //Recursive call
                        ms.WriteTo(target);
                    }
                }
                else
                {
                    //Everything else
                    quantized.Save(target, format);
                }
            }
        }
Example #2
0
        /// <summary>
        /// 
        /// </summary>
        /// <param name="format"></param>
        /// <param name="img"></param>
        /// <param name="target"></param>
        /// <param name="colors"></param>
        /// <param name="dither"></param>
        /// <param name="fourPass"></param>
        /// <param name="ditherPercent"></param>
        /// <param name="useGdiQuantization"></param>
        public void SaveIndexed(System.Drawing.Imaging.ImageFormat format, Image img, Stream target, byte colors, bool dither, bool fourPass, int ditherPercent)
        {
            if (PreservePalette && originalPalette != null) {
                img.Palette = originalPalette;
                //If we are encoding in PNG, and writing to a non-seekable stream,
                //we have to buffer it all in memory or we'll get an exception
                if (!target.CanSeek && ImageFormat.Png.Equals(format)) {
                    using (MemoryStream ms = new MemoryStream(4096)) {
                        img.Save(ms,format); //Recursive call
                        ms.WriteTo(target);
                    }
                } else {
                    //Everything else
                    img.Save(target, format);
                }
                return;
            }

            OctreeQuantizer quantizer = new OctreeQuantizer(colors, GetBitsNeededForColorDepth(colors));

            if (query.Get<bool>("fulltrust",HasFullTrust)) {
                quantizer.Dither = dither;
                quantizer.FourPass = fourPass;
                quantizer.DitherPercent = (float)ditherPercent / 100;
            } else {
                quantizer.FullTrust = false;
                quantizer.OmitFinalStage = true;
                quantizer.ResizeForFirstPass = true;

                quantizer.FirstPassPixelCount = (long)Math.Pow(query.Get<int>( "pixelCount", (int)Math.Sqrt(quantizer.FirstPassPixelCount)),2);
                quantizer.FirstPassPixelThreshold = (long)Math.Pow(query.Get<int>( "pixelThreshold", (int)Math.Sqrt(quantizer.FirstPassPixelThreshold)), 2);

            }
            using (Bitmap quantized = quantizer.Quantize(img)) {
                //If we are encoding in PNG, and writing to a non-seekable stream,
                //we have to buffer it all in memory or we'll get an exception
                if (!target.CanSeek && ImageFormat.Png.Equals(format)) {
                    using (MemoryStream ms = new MemoryStream(4096)) {
                        quantized.Save(ms,format); //Recursive call
                        ms.WriteTo(target);
                    }
                } else {
                    //Everything else
                    quantized.Save(target, format);
                }
            }
        }