public ImageCompressionResult CompressFile(string fileName, bool lossy) { string targetFile = Path.ChangeExtension(Path.GetTempFileName(), Path.GetExtension(fileName)); var start = new ProcessStartInfo("cmd") { WindowStyle = ProcessWindowStyle.Hidden, WorkingDirectory = _cwd, Arguments = GetArguments(fileName, targetFile, lossy), UseShellExecute = false, CreateNoWindow = true, }; var stopwatch = Stopwatch.StartNew(); using (var process = Process.Start(start)) { process.WaitForExit(); } stopwatch.Stop(); var result = new ImageCompressionResult(fileName, targetFile, stopwatch.Elapsed); HandleResult(result); return(result); }
void HandleResult(ImageCompressionResult result) { string name = Path.GetFileName(result.OriginalFileName); if (result.Saving > 0 && result.ResultFileSize > 0 && File.Exists(result.ResultFileName)) { File.Copy(result.ResultFileName, result.OriginalFileName, true); File.Delete(result.ResultFileName); } }