Beispiel #1
0
        private static CompressionResult[] OptimizeImages(Repository repo, string localPath, string[] imagePaths)
        {
            var optimizedImages = new List <CompressionResult>();

            ImageOptimizer imageOptimizer = new ImageOptimizer
            {
                OptimalCompression = true
            };

            Parallel.ForEach(imagePaths, image =>
            {
                try
                {
                    Console.WriteLine(image);
                    FileInfo file = new FileInfo(image);
                    double before = file.Length;
                    if (imageOptimizer.LosslessCompress(file))
                    {
                        optimizedImages.Add(new CompressionResult
                        {
                            Title      = image.Substring(localPath.Length),
                            SizeBefore = before / 1024d,
                            SizeAfter  = file.Length / 1024d,
                        });

                        Commands.Stage(repo, image);
                    }
                }
                catch
                {
                }
            });

            return(optimizedImages.ToArray());
        }
        public async Task <Uri> UploadReducedQualityImage(string folderPath, Uri fullsizePhotoUrl, int maxWidthPx, int maxHeightPx, string suffix)
        {
            var stream = await this.ToStreamAsync(fullsizePhotoUrl.ToString());

            var imageHelper          = new ImageUtilities();
            var extension            = fullsizePhotoUrl.ToString().GetFileExtension();
            var resizedImage         = imageHelper.ScaleImage(Image.FromStream(stream), maxWidthPx, maxHeightPx);
            var lowerQualityImageUrl = fullsizePhotoUrl.ToString().Replace(string.Format(".{0}", extension), string.Format("{0}.{1}", suffix, extension));
            var streamRotated        = this.ToAStream(resizedImage, this.SetImageFormat(lowerQualityImageUrl));

            if (OptimizeImage)
            {
                var optimizer = new ImageOptimizer
                {
                    OptimalCompression = true
                };
                optimizer.LosslessCompress(streamRotated);
            }

            await this.siteFilesRepository.UploadAsync(
                streamRotated,
                lowerQualityImageUrl.GetFileNameFromUrl(),
                folderPath);

            stream.Dispose();
            resizedImage.Dispose();

            return(new Uri(lowerQualityImageUrl));
        }
Beispiel #3
0
        protected override void ExecuteWork(ImageFileInfo sourceImage, string fullTargetName)
        {
            string ext = Path.GetExtension(fullTargetName);

            if (!IsTargetExtSupported(ext))
            {
                return; //TODO:Buraya bir error uydurmak gerekir mi?
            }
            if (File.Exists(fullTargetName) == false)
            {
                if (sourceImage.Extension.Equals(ext, StringComparison.InvariantCultureIgnoreCase))
                {
                    File.Copy(sourceImage.FullName, fullTargetName);
                }
                else
                {
                    using (var image = new MagickImage(sourceImage.FullName))
                    {
                        image.Strip();
                        image.Write(fullTargetName);
                    }
                }
            }

            var imageOptimizer = new ImageOptimizer
            {
                OptimalCompression = true
            };

            imageOptimizer.LosslessCompress(fullTargetName);
        }
Beispiel #4
0
        protected static void LosslessCompress(string fileName)
        {
            var optimizer = new ImageOptimizer();

            optimizer.OptimalCompression = true;
            optimizer.LosslessCompress(fileName);
        }
        static Tuple <bool, string> Interpolate(MagickImage imageA, MagickImage imageB, string destinationPath, double alpha)
        {
            if (alpha >= 1 || alpha <= 0)
            {
                return(new Tuple <bool, string>(false, "Alpha value must be less than 1.0 and more than 0.0"));
            }
            try
            {
                imageB.HasAlpha = true;
                imageB.Evaluate(Channels.Alpha, EvaluateOperator.Multiply, alpha);
                imageA.Composite(imageB, CompositeOperator.Over);

                using (MemoryStream memoryStream = new MemoryStream())
                {
                    imageA.Write(memoryStream, MagickFormat.Png32);
                    ImageOptimizer optimizer = new ImageOptimizer();
                    optimizer.OptimalCompression = true;
                    memoryStream.Position        = 0;
                    optimizer.LosslessCompress(memoryStream);
                    FileStream fileStream = new FileStream(destinationPath, FileMode.Create);
                    memoryStream.WriteTo(fileStream);
                }
                //imageA.Write(destinationPath, MagickFormat.Png24);
            }
            catch (Exception ex)
            {
                return(new Tuple <bool, string>(false, ex.Message));
            }
            return(new Tuple <bool, string>(true, "Success"));
        }
Beispiel #6
0
        private void Form1_Shown(object sender, EventArgs e)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            DialogResult   result;

            do
            {
                result = ofd.ShowDialog();
            } while (result != DialogResult.OK || ofd.FileName == null);
            string fileName = ofd.FileName;

            FileInfo snakewareLogo = new FileInfo(fileName);

            MessageBox.Show("Bytes before: " + snakewareLogo.Length);

            ImageOptimizer optimizer = new ImageOptimizer();

            optimizer.LosslessCompress(snakewareLogo);

            snakewareLogo.Refresh();
            MessageBox.Show("Bytes after:  " + snakewareLogo.Length);

            //Compressor
            return;

            AntlrFileStream inputStream = new AntlrFileStream(fileName);

            IParseTree  tree  = null;
            IVocabulary names = null;
            string      typeS = fileName.Substring(fileName.LastIndexOf('.') + 1);

            switch (typeS.ToUpper())
            {
            case "CSS":
                tree  = new CssParser(new CommonTokenStream(new CssLexer(inputStream))).stylesheet();
                names = CssParser.DefaultVocabulary;
                break;

            case "HTML":
                tree  = new HtmlParser(new CommonTokenStream(new HtmlLexer(inputStream))).htmlDocument();
                names = HtmlParser.DefaultVocabulary;
                break;

            case "JS":
                tree  = new JsParser(new CommonTokenStream(new JsLexer(inputStream))).program();
                names = JsParser.DefaultVocabulary;
                break;
            }
            var node = treeView.Nodes.Add(ToText(tree, names));

            RecursFn(tree, node, (i) => ToText(i, names));

            /*var d = new CommonTokenStream(new HtmlLexer(inputStream));
             * names = HtmlParser.DefaultVocabulary;
             * d.Fill();
             * foreach(var item in d.GetTokens()) {
             *  treeView.Nodes.Add(names.GetDisplayName(item.Type) + " --- " + item.ToString());
             * }*/
        }
        public void Test_InvalidArguments()
        {
            ImageOptimizer optimizer = new ImageOptimizer();

            Assert.IsNotNull(optimizer);

            ExceptionAssert.Throws <ArgumentNullException>(delegate()
            {
                optimizer.Compress((FileInfo)null);
            });

            ExceptionAssert.Throws <ArgumentNullException>(delegate()
            {
                optimizer.Compress((string)null);
            });

            ExceptionAssert.Throws <ArgumentException>(delegate()
            {
                optimizer.Compress("");
            });

            ExceptionAssert.Throws <ArgumentException>(delegate()
            {
                optimizer.Compress(Files.Missing);
            });

            ExceptionAssert.Throws <ArgumentNullException>(delegate()
            {
                optimizer.LosslessCompress((FileInfo)null);
            });

            ExceptionAssert.Throws <ArgumentNullException>(delegate()
            {
                optimizer.LosslessCompress((string)null);
            });

            ExceptionAssert.Throws <ArgumentException>(delegate()
            {
                optimizer.LosslessCompress("");
            });

            ExceptionAssert.Throws <ArgumentException>(delegate()
            {
                optimizer.LosslessCompress(Files.Missing);
            });
        }
Beispiel #8
0
        public void LosslessCompress_IgnoreUnsupportedFile_DoesNotThrowException()
        {
            var optimizer = new ImageOptimizer {
                IgnoreUnsupportedFormats = true
            };

            Assert.IsFalse(optimizer.LosslessCompress(Files.InvitationTif));
        }
                public void ShouldNotMakeFileSmallerWhenFileNameIsUncompressibleIcoFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.ImageMagickICO, false, (string file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
                public void ShouldThrowExceptionWhenFileNameIsNull()
                {
                    var optimizer = new ImageOptimizer();

                    ExceptionAssert.ThrowsArgumentNullException("fileName", () =>
                    {
                        optimizer.LosslessCompress((string)null);
                    });
                }
                public void ShouldMakeFileSmallerWhenFileNameIsCompressibleGifFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.FujiFilmFinePixS1ProGIF, true, (string file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
                public void ShouldMakeFileSmallerWhenFileIsCompressibleIcoFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.WandICO, true, (FileInfo file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
                public void ShouldThrowExceptionWhenFileIsUnsupportedFormat()
                {
                    var optimizer = new ImageOptimizer();

                    ExceptionAssert.Throws <MagickCorruptImageErrorException>(() =>
                    {
                        optimizer.LosslessCompress(new FileInfo(Files.InvitationTIF));
                    }, "Invalid format");
                }
                public void ShouldNotMakeFileSmallerWhenStreamIsCompressibleIcoStream()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.ImageMagickICO, false, (Stream stream) =>
                    {
                        return(optimizer.LosslessCompress(stream));
                    });
                }
                public void ShouldMakeFileSmallerWhenStreamIsCompressibleIcoStream()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.WandICO, true, (Stream stream) =>
                    {
                        return(optimizer.LosslessCompress(stream));
                    });
                }
                public void ShouldNotMakeFileSmallerWhenFileNameIsUncompressibleGifFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.RoseSparkleGIF, false, (string file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
Beispiel #17
0
        private bool OptimizeImageStream(Stream imageStream)
        {
            var optimizer = new ImageOptimizer();

            imageStream.Position = 0;
            bool success = optimizer.LosslessCompress(imageStream);

            return(success);
        }
                public void ShouldThrowExceptionWhenStreamIsNull()
                {
                    var optimizer = new ImageOptimizer();

                    ExceptionAssert.Throws <ArgumentNullException>("stream", () =>
                    {
                        optimizer.LosslessCompress((Stream)null);
                    });
                }
                public void ShouldNotMakeFileSmallerWhenStreamIsCompressibleJpgFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.LetterJPG, false, (Stream file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
                public void ShouldMakeFileSmallerWhenStreamIsCompressibleJpgStrea()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.ImageMagickJPG, true, (Stream stream) =>
                    {
                        return(optimizer.LosslessCompress(stream));
                    });
                }
                public void ShouldNotMakeFileSmallerWhenFileIsUnCompressiblePngFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.MagickNETIconPNG, false, (FileInfo file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
                public void ShouldMakeFileSmallerWhenStreamIsCompressibleGifStream()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.FujiFilmFinePixS1ProGIF, true, (Stream stream) =>
                    {
                        return(optimizer.LosslessCompress(stream));
                    });
                }
                public void ShouldThrowExceptionWhenFileNameIsEmpty()
                {
                    var optimizer = new ImageOptimizer();

                    ExceptionAssert.Throws <ArgumentException>("fileName", () =>
                    {
                        optimizer.LosslessCompress(string.Empty);
                    });
                }
                public void ShouldNotMakeFileSmallerWhenStreamIsCompressibleGifStream()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.RoseSparkleGIF, false, (Stream stream) =>
                    {
                        return(optimizer.LosslessCompress(stream));
                    });
                }
                public void ShouldThrowExceptionWhenFileNameIsInvalid()
                {
                    var optimizer = new ImageOptimizer();

                    ExceptionAssert.Throws <MagickBlobErrorException>(() =>
                    {
                        optimizer.LosslessCompress(Files.Missing);
                    }, "error/blob.c/OpenBlob");
                }
                public void ShouldNotThrowExceptionWhenIgnoringUnsupportedFileName()
                {
                    var optimizer = new ImageOptimizer {
                        IgnoreUnsupportedFormats = true
                    };
                    var compressionSuccess = optimizer.LosslessCompress(new FileInfo(Files.InvitationTIF));

                    Assert.IsFalse(compressionSuccess);
                }
                public void ShouldMakeFileSmallerWhenFileNameIsCompressiblePngFile()
                {
                    var optimizer = new ImageOptimizer();

                    AssertCompress(Files.SnakewarePNG, true, (string file) =>
                    {
                        return(optimizer.LosslessCompress(file));
                    });
                }
                public void ShouldThrowExceptionWhenFileIsNull()
                {
                    var optimizer = new ImageOptimizer();

                    ExceptionAssert.Throws <ArgumentNullException>("file", () =>
                    {
                        optimizer.LosslessCompress((FileInfo)null);
                    });
                }
Beispiel #29
0
        private static void Compress(string file)
        {
            ImageOptimizer optimizer = new ImageOptimizer()
            {
                OptimalCompression       = true,
                IgnoreUnsupportedFormats = true
            };

            optimizer.LosslessCompress(file);
        }
                public void ShouldReturnFalseWhenFileIsEmpty()
                {
                    var optimizer = new ImageOptimizer();

                    using (TemporaryFile file = new TemporaryFile("empty"))
                    {
                        var result = optimizer.LosslessCompress(file);
                        Assert.IsFalse(result);
                    }
                }