private void DoCompress(FileInfo file, bool lossless, int quality) { using (TemporaryFile tempFile = new TemporaryFile()) { if (!DoCompress(file, tempFile, Progressive, lossless, quality)) { return; } if (OptimalCompression) { using (TemporaryFile tempFileOptimal = new TemporaryFile()) { if (!DoCompress(file, tempFileOptimal, Progressive, lossless, quality)) { return; } if (tempFileOptimal.Length < file.Length && tempFileOptimal.Length < tempFile.Length) { tempFileOptimal.CopyTo(file); return; } } } if (tempFile.Length < file.Length) { tempFile.CopyTo(file); } } }
private bool DoCompress(FileInfo file, bool lossless, int quality) { using (TemporaryFile tempFile = new TemporaryFile()) { DoNativeCompress(file, tempFile, Progressive, lossless, quality); if (OptimalCompression) { using (TemporaryFile tempFileOptimal = new TemporaryFile()) { DoNativeCompress(file, tempFileOptimal, !Progressive, lossless, quality); if (tempFileOptimal.Length < tempFile.Length) { tempFileOptimal.CopyTo(tempFile); } } } if (tempFile.Length >= file.Length) { return(false); } tempFile.CopyTo(file); return(true); } }
private bool DoLosslessCompress(FileInfo file) { bool isCompressed = false; using (MagickImage image = new MagickImage(file)) { ImageOptimizerHelper.CheckFormat(image, MagickFormat.Png); image.Strip(); image.Settings.SetDefine(MagickFormat.Png, "exclude-chunks", "all"); image.Settings.SetDefine(MagickFormat.Png, "include-chunks", "tRNS,gAMA"); CheckTransparency(image); Collection <TemporaryFile> tempFiles = new Collection <TemporaryFile>(); try { TemporaryFile bestFile = null; foreach (int quality in GetQualityList()) { TemporaryFile tempFile = new TemporaryFile(); tempFiles.Add(tempFile); image.Quality = quality; image.Write(tempFile); if (bestFile == null || bestFile.Length > tempFile.Length) { bestFile = tempFile; } } if (bestFile.Length < file.Length) { isCompressed = true; bestFile.CopyTo(file); file.Refresh(); } } finally { foreach (TemporaryFile tempFile in tempFiles) { tempFile.Dispose(); } } } return(isCompressed); }
private bool DoCompress(FileInfo file, bool lossless) { bool isCompressed = false; var settings = new MagickReadSettings() { Format = MagickFormat.Ico }; using (var images = new MagickImageCollection(file, settings)) { foreach (var image in images) { if (image.Width > 255) { image.Format = MagickFormat.Png; var pngHelper = new PngHelper(this); var memoryStream = pngHelper.FindBestStreamQuality(image, out var bestQuality); image.Format = MagickFormat.Ico; if (memoryStream != null) { memoryStream.Dispose(); image.Quality = bestQuality; } } else { if (CanUseColormap(image, lossless)) { image.ClassType = ClassType.Pseudo; } } } using (var tempFile = new TemporaryFile()) { images.Write(tempFile); if (tempFile.Length < file.Length) { isCompressed = true; tempFile.CopyTo(file); file.Refresh(); } } } return(isCompressed); }
private bool DoCompress(FileInfo file, bool lossless) { bool isCompressed = false; using (MagickImage image = new MagickImage(file)) { if (image.GetAttribute("png:acTL") != null) { return(false); } StartCompression(image, lossless); Collection <TemporaryFile> tempFiles = new Collection <TemporaryFile>(); try { TemporaryFile bestFile = null; foreach (int quality in GetQualityList()) { TemporaryFile tempFile = new TemporaryFile(); tempFiles.Add(tempFile); image.Quality = quality; image.Write(tempFile); if (bestFile == null || bestFile.Length > tempFile.Length) { bestFile = tempFile; } } if (bestFile.Length < file.Length) { isCompressed = true; bestFile.CopyTo(file); file.Refresh(); } } finally { foreach (TemporaryFile tempFile in tempFiles) { tempFile.Dispose(); } } } return(isCompressed); }
private static void DoLosslessCompress(FileInfo file, IMagickImage image) { CheckFormat(image); image.Strip(); using (TemporaryFile tempFile = new TemporaryFile()) { image.Settings.Interlace = Interlace.NoInterlace; image.Write(tempFile); if (tempFile.Length < file.Length) { tempFile.CopyTo(file); } } }
public void Can_copy_to_stream() { ////Arrange Stream result = new MemoryStream(); ////Act using (var file = new TemporaryFile()) { using (var stream = file.Info.OpenWrite()) stream.WriteByte(1); file.CopyTo(result); } ////Assert Assert.That(result.Length, Is.EqualTo(1)); }
private static bool DoLosslessCompress(FileInfo file, IMagickImage <QuantumType> image) { ImageOptimizerHelper.CheckFormat(image, MagickFormat.Gif); bool isCompressed = false; using (TemporaryFile tempFile = new TemporaryFile()) { LosslessCompress(image); image.Write(tempFile); if (tempFile.Length < file.Length) { isCompressed = true; tempFile.CopyTo(file); file.Refresh(); } } return(isCompressed); }
private bool DoCompress(FileInfo file, bool lossless) { bool isCompressed = false; using (var image = new MagickImage(file)) { if (image.GetAttribute("png:acTL") != null) { return(false); } StartCompression(image, lossless); TemporaryFile bestFile = null; try { var pngHelper = new PngHelper(this); bestFile = pngHelper.FindBestFileQuality(image, out _); if (bestFile != null && bestFile.Length < file.Length) { isCompressed = true; bestFile.CopyTo(file); file.Refresh(); } } finally { if (bestFile != null) { bestFile.Dispose(); } } } return(isCompressed); }
private static bool DoLosslessCompress(FileInfo file, IMagickImage image) { CheckFormat(image); bool isCompressed = false; image.Strip(); using (TemporaryFile tempFile = new TemporaryFile()) { image.Settings.Interlace = Interlace.NoInterlace; image.Write(tempFile); if (tempFile.Length < file.Length) { isCompressed = true; tempFile.CopyTo(file); file.Refresh(); } } return(isCompressed); }