Exemplo n.º 1
0
        public byte[] OptimizePng(byte[] bytes, int compressionLevel, bool imageQuantizationDisabled)
        {
            var optimizedBytes = bytes;

            if (!imageQuantizationDisabled)
            {
                using (var unQuantized = new Bitmap(new MemoryStream(bytes)))
                {
                    using (var quantized = wuQuantizer.QuantizeImage(unQuantized, 10, 5))
                    {
                        var memStream = new MemoryStream();
                        quantized.Save(memStream, ImageFormat.Png);
                        optimizedBytes = memStream.GetBuffer();
                    }
                }
            }

            if (fileWrapper.FileExists(optiPngLocation))
            {
                var scratchFile = string.Format("{0}\\scratch-{1}.png", configuration.SpritePhysicalPath, Hasher.Hash(bytes));
                try
                {
                    fileWrapper.Save(optimizedBytes, scratchFile);
                    var arg = String.Format(@"-fix -o{1} ""{0}""", scratchFile, compressionLevel);
                    InvokeExecutable(arg, optiPngLocation);
                    optimizedBytes = fileWrapper.GetFileBytes(scratchFile);
                }
                finally
                {
                    fileWrapper.DeleteFile(scratchFile);
                }
            }

            return(optimizedBytes);
        }
Exemplo n.º 2
0
        public virtual void Save(byte[] content, string url, string originalUrls)
        {
            var file = GetFileNameFromConfig(url);
            var sig  = uriBuilder.ParseSignature(url);
            var guid = uriBuilder.ParseKey(url);

            FileWrapper.Save(content, file);
            if (!url.ToLower().EndsWith(".png") && ReductionRepository != null)
            {
                ReductionRepository.AddReduction(guid, url);
            }
            RRTracer.Trace("{0} saved to disk.", url);
            var expiredFile = file.Insert(file.IndexOf(sig, StringComparison.Ordinal), "Expired-");

            if (FileWrapper.FileExists(expiredFile))
            {
                FileWrapper.DeleteFile(expiredFile);
            }
        }