/// <summary> /// Arguments for a successful file process /// </summary> /// <param name="originalFilePath">Path to the original file</param> /// <param name="newFilePath">Path to the newly (over)written file</param> /// <param name="filePathIndex">Index in the set of files given to the batch</param> /// <param name="compressor">Compressor that produced the smallest file</param> public FileProcessSuccessEventArgs(string originalFilePath, string newFilePath, int filePathIndex, Compressor.PNGCompressor compressor) : base() { this.originalFilePath = originalFilePath; this.newFilePath = newFilePath; this.filePathIndex = filePathIndex; this.compressor = compressor; }
/// <summary> /// Begin compressing files /// </summary> public void Start() { if (FilePaths == null || OutputDirectory == String.Empty) { throw new Exception("FilePaths / OutputDirectory not set"); } // loop through our files for (int i = 0; i < FilePaths.Length; i++) { string filePath = FilePaths[i]; try { Compressor.PNGCompressor pngCompressor = compress(filePath); // this stores the compressor that produced the smallest file Compressor.PNGCompressor winningCompressor = pngCompressor; byte[] fileToWrite = pngCompressor.CompressedFile; string outputDirectory = OutputDirectory; // we may be getting a jpg as input, make sure we output png string fileName = Path.GetFileNameWithoutExtension(filePath) + ".png"; // if the compressed file is larger than the original, keep the original (unless told otherwise) if (!OutputIfLarger && Compressor.PNGCompressor.IsPng(pngCompressor.OriginalFile) && pngCompressor.CompressedFile.Length >= pngCompressor.OriginalFile.Length) { fileToWrite = pngCompressor.OriginalFile; // there was no winning compressor winningCompressor = null; } // we're going to output to the same directory, overwriting files if needed if (outputDirectory == null) { outputDirectory = Path.GetDirectoryName(filePath); } // build the file path string outputFilePath = System.IO.Path.Combine(outputDirectory, fileName); // output the file File.WriteAllBytes(outputFilePath, fileToWrite); // fire the success event FileProcessSuccessEventArgs e = new FileProcessSuccessEventArgs(filePath, outputFilePath, i, winningCompressor); OnFileProcessSuccess(e); } catch (Exception e) { // fire the fail event FileProcessFailEventArgs eventArgs = new FileProcessFailEventArgs(filePath, i, e); OnFileProcessFail(eventArgs); } } }
private void ThreadRun() { string filePath = ""; int i; ++ThreadsRunning; while (true) { i = NextIndex(); try { filePath = FilePaths[i]; } catch (IndexOutOfRangeException) { ThreadsRunning--; break; } /* * Ugh don't do this: * try * {*/ Compressor.PNGCompressor pngCompressor = compress(filePath); // this stores the compressor that produced the smallest file Compressor.PNGCompressor winningCompressor = pngCompressor; byte[] fileToWrite = pngCompressor.CompressedFile; string outputDirectory = OutputDirectory; // we may be getting a jpg as input, make sure we output png string fileName = Path.GetFileNameWithoutExtension(filePath) + ".png"; // if the compressed file is larger than the original, keep the original (unless told otherwise) if (!OutputIfLarger && Compressor.PNGCompressor.IsPng(pngCompressor.OriginalFile) && pngCompressor.CompressedFile.Length >= pngCompressor.OriginalFile.Length) { fileToWrite = pngCompressor.OriginalFile; // there was no winning compressor winningCompressor = null; } // we're going to output to the same directory, overwriting files if needed if (outputDirectory == null) { outputDirectory = Path.GetDirectoryName(filePath); } // build the file path string outputFilePath = System.IO.Path.Combine(outputDirectory, fileName); // output the file File.WriteAllBytes(outputFilePath, fileToWrite); // fire the success event FileProcessSuccessEventArgs e = new FileProcessSuccessEventArgs(filePath, outputFilePath, i, winningCompressor); OnFileProcessSuccess(e); /* } * catch (Exception e) * { * // fire the fail event * FileProcessFailEventArgs eventArgs = new FileProcessFailEventArgs(filePath, i, e); * OnFileProcessFail(eventArgs); * } */ } }