Example #1
0
        private void reduceImagesAsync()
        {
            DisableUIForRun();
            List <ReduceOptions> optionsList = new List <ReduceOptions>();

            foreach (ListViewItem item in listView.Items)
            {
                // Save reduced file to a subdirectory called "Reduced" in the same location
                // as the input.
                // e.g. c:\users\public\Accusoft\Common\Images\akuma.jpg -> c:\users\public\Accusoft\Common\Images\Reduced\akuma.jpg
                //

                string inputPath       = item.SubItems[COLUMN_FILENAME].Text;
                string inputFileName   = Path.GetFileName(inputPath);
                string inputDirectory  = Path.GetDirectoryName(inputPath);
                string outputDirectory = Path.Combine(inputDirectory, "Reduced");

                if (IsFileExtensionJpeg(inputPath))
                {
                    JpegReduceOptions options = new JpegReduceOptions();
                    options.inputPath         = inputPath;
                    options.outputPath        = Path.Combine(outputDirectory, inputFileName);
                    options.jpegQualityFactor = _jpegQualityFactor;
                    options.transformMode     = _transformMode;

                    optionsList.Add(options);
                }
                else if (IsFileExtensionPng(inputPath))
                {
                    ReduceOptions options = new ReduceOptions();
                    options.inputPath  = inputPath;
                    options.outputPath = Path.Combine(outputDirectory, inputFileName);

                    optionsList.Add(options);
                }
            }
            backgroundWorker.RunWorkerAsync(optionsList);
        }
Example #2
0
        private void backgroundWorker_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e)
        {
            List <ReduceOptions> optionsList = (List <ReduceOptions>)e.Argument;
            Int32 remaining = optionsList.Count;

            foreach (ReduceOptions options in optionsList)
            {
                if (backgroundWorker.CancellationPending)
                {
                    break;
                }

                // Prepare to save reduced image:
                // - Delete the target file if it already exists
                // - Ensure the host directory exists.
                deleteFileWhenExists(options.outputPath);
                Directory.CreateDirectory(Path.GetDirectoryName(options.outputPath));

                JpegReduceOptions jpegReduceOptions = new JpegReduceOptions();
                JpegReduceResult  jpegLatestResult  = new JpegReduceResult();

                ReduceResult latestResult = new ReduceResult();

                // Perform reduction and prepare to report results
                latestResult.inputPath          = options.inputPath;
                latestResult.outputPath         = options.outputPath;
                latestResult.isErrorEncountered = false;
                latestResult.remarks            = String.Empty;

                bool areOptionsForJpeg = (options is JpegReduceOptions) ? true : false;

                if (areOptionsForJpeg)
                {
                    jpegReduceOptions = options as JpegReduceOptions;
                    jpegLatestResult  = new JpegReduceResult(latestResult);
                    jpegLatestResult.jpegQualityFactor = jpegReduceOptions.jpegQualityFactor;
                    jpegLatestResult.transformMode     = jpegReduceOptions.transformMode;
                }

                try
                {
                    if (areOptionsForJpeg)
                    {
                        Accusoft.ImagXpressSdk.ImageReduce.JpegReduce(imagXpress1, jpegReduceOptions.inputPath, jpegReduceOptions.outputPath, jpegReduceOptions.jpegQualityFactor);
                    }
                    else
                    {
                        Accusoft.ImagXpressSdk.ImageReduce.PngReduce(imagXpress1, options.inputPath, options.outputPath);
                    }
                }
                catch (ImagXpressException ex)
                {
                    latestResult.isErrorEncountered = true;
                    latestResult.remarks            = ex.Message;
                }
                catch (Exception ex)
                {
                    latestResult.isErrorEncountered = true;
                    latestResult.remarks            = ex.ToString();
                }

                // Notify UI of a progress update
                int percentComplete = (int)((float)(optionsList.Count - remaining) / optionsList.Count * 100);
                if (areOptionsForJpeg)
                {
                    backgroundWorker.ReportProgress(percentComplete, jpegLatestResult);
                }
                else
                {
                    backgroundWorker.ReportProgress(percentComplete, latestResult);
                }
                remaining--;
            }
        }