Ejemplo n.º 1
0
 /// <summary>
 /// Fire the 'FileProcessSuccess' event
 /// </summary>
 /// <param name="e">Event args</param>
 private void OnFileProcessSuccess(FileProcessSuccessEventArgs e)
 {
     FileProcessSuccess(this, e);
 }
Ejemplo n.º 2
0
        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);
                 * } */
            }
        }