Beispiel #1
0
        /// <summary>
        /// Gets the output filename for an image file.
        /// </summary>
        /// <param name="filename">Full path of the input file.</param>
        /// <param name="outType">Output file type.</param>
        /// <returns></returns>
        private static string GetOutputFilename(string filename, outTypeOptions outType, string suffix)
        {
            // Gets an output filename
            string extension = Path.GetExtension(filename);

            switch (outType)
            {
            case outTypeOptions.BMP:
                extension = ".bmp";
                break;

            case outTypeOptions.GIF:
                extension = ".gif";
                break;

            case outTypeOptions.JPG:
                extension = ".jpg";
                break;

            case outTypeOptions.PNG:
                extension = ".png";
                break;

            case outTypeOptions.TIF:
                extension = ".tif";
                break;
            }
            return(_CreateOutputFilename(filename, extension, suffix));
        }
Beispiel #2
0
        private void cbxOutputType_SelectedIndexChanged(object sender, EventArgs e)
        {
            outTypeOptions currentOption = (outTypeOptions)this.cbxOutputType.SelectedIndex;

            if (currentOption == outTypeOptions.match ||
                currentOption == outTypeOptions.JPG)
            {
                this.lblQuality.Visible = true;
                this.nudQuality.Visible = true;
            }
            else
            {
                this.lblQuality.Visible = false;
                this.nudQuality.Visible = false;
            }
        }
Beispiel #3
0
 public MediaProcessorOptions(comboOptions imageResizeType,
                              comboOptions videoResizeType,
                              outTypeOptions imageOutType,
                              videoOutTypeOptions videoOutType,
                              int imageResizeValue,
                              int videoResizeValue,
                              int jpegQuality,
                              int videoQuality,
                              float?defaultDropRatio)
 {
     this.imageResizeType  = imageResizeType;
     this.videoResizeType  = videoResizeType;
     this.imageOutType     = imageOutType;
     this.videoOutType     = videoOutType;
     this.imageResizeValue = imageResizeValue;
     this.videoResizeValue = videoResizeValue;
     this.jpegQuality      = jpegQuality;
     this.videoQuality     = videoQuality;
     this.defaultCropRatio = defaultDropRatio;
 }
Beispiel #4
0
        /// <summary>
        /// Resize and save an image file.
        /// </summary>
        /// <param name="filename">Full path to the image to resize.</param>
        /// <param name="outFile">Full path to where the output file should be saved.</param>
        /// <param name="resizeValue">The height, width, or precentage to resize to, as specified by the resizeOption parameter.</param>
        /// <param name="resizeOption">The type of resize which resizeValue is referring to.</param>
        /// <param name="outTypeOption">The output image file type.</param>
        /// <param name="jpegQuality">Only used if outTypeOption is JPEG, the quality parameter of the output JPEG image</param>
        /// <returns>Void.</returns>
        public static void ProcessImageFile(string filename, string outFile, int resizeValue, comboOptions resizeOption,
                                            outTypeOptions outTypeOption,
                                            Dictionary <string, Rectangle> definedCropBoundaries,
                                            float?defaultCropRatio, int jpegQuality = 98)
        {
            // Read file - will throw exception if file doesn't exist
            using (Image img = Image.FromFile(filename))
            {
                Rectangle cropBoundary = GetCropBoundaryForImage(filename, img, definedCropBoundaries, defaultCropRatio);

                // Get new dimensions
                Tuple <int, int> newSize = GetNewSize(resizeOption, cropBoundary.Width, cropBoundary.Height, resizeValue);
                int newWidth             = newSize.Item1;
                int newHeight            = newSize.Item2;

                // Do actual resize
                using (Bitmap resizedBmp = ResizeImage(img, newWidth, newHeight, cropBoundary))
                {
                    // Save result
                    if (outTypeOption == outTypeOptions.match)
                    {
                        switch (Path.GetExtension(filename).ToLower())
                        {
                        case ".jpg":
                        case ".jpeg":
                            outTypeOption = outTypeOptions.JPG;
                            break;

                        case ".bmp":
                            outTypeOption = outTypeOptions.BMP;
                            break;

                        case ".gif":
                            outTypeOption = outTypeOptions.GIF;
                            break;

                        case ".png":
                            outTypeOption = outTypeOptions.PNG;
                            break;

                        case ".tif":
                        case ".tiff":
                            outTypeOption = outTypeOptions.TIF;
                            break;
                        }
                    }
                    EncoderParameters eps = null;
                    ImageCodecInfo    ici = GetEncoderInfo("image/bmp");
                    switch (outTypeOption)
                    {
                    case outTypeOptions.JPG:
                        eps          = new EncoderParameters(1);
                        eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)jpegQuality);
                        ici          = GetEncoderInfo("image/jpeg");
                        break;

                    case outTypeOptions.BMP:
                        // Nothing to do - defaults are fine
                        break;

                    case outTypeOptions.GIF:
                        ici = GetEncoderInfo("image/gif");
                        break;

                    case outTypeOptions.PNG:
                        ici = GetEncoderInfo("image/png");
                        break;

                    case outTypeOptions.TIF:
                        ici = GetEncoderInfo("image/tiff");
                        break;
                    }

                    foreach (PropertyItem propItem in img.PropertyItems)
                    {
                        resizedBmp.SetPropertyItem(propItem);
                    }

                    resizedBmp.Save(outFile, ici, eps);
                }
            }
        }
Beispiel #5
0
        private void btnProcess_Click(object sender, EventArgs e)
        {
            if (this.isProcessing)
            {
                this.cancelSource.Cancel();
                return;
            }
            if (this.fileList.Count == 0)
            {
                MessageBox.Show("No files selected.", "No files selected", MessageBoxButtons.OK);
                return;
            }
            int resizeValue = TryReadResizeText(this.txtResize, (comboOptions)this.cbxResizeType.SelectedIndex);

            if (resizeValue < 0)
            {
                MessageBox.Show("Invalid value entered for resizing images to.", "Invalid entry", MessageBoxButtons.OK);
                return;
            }
            int videoResizeValue = TryReadResizeText(this.txtVideoResize, (comboOptions)this.cbxVideoResizeType.SelectedIndex);

            if (videoResizeValue < 0)
            {
                MessageBox.Show("Invalid value entered for resizing videos to.", "Invalid entry", MessageBoxButtons.OK);
                return;
            }

            this.OnProcessingStart();



            comboOptions        comboOption        = (comboOptions)this.cbxResizeType.SelectedIndex;
            comboOptions        videoComboOption   = (comboOptions)this.cbxVideoResizeType.SelectedIndex;
            outTypeOptions      outTypeOption      = (outTypeOptions)this.cbxOutputType.SelectedIndex;
            videoOutTypeOptions videoOutTypeOption = (videoOutTypeOptions)this.cbxVideoOutputType.SelectedIndex;
            int jpegQuality         = (int)nudQuality.Value;
            int mpegQuality         = (int)nudVideoQuality.Value;
            CancellationToken token = this.cancelSource.Token;

            Task processingTask = Task.Factory.StartNew(() =>
            {
                List <string> failedList = new List <string>();
                for (int ii = 0; ii < this.fileList.Count; ii++)
                {
                    if (token.IsCancellationRequested)
                    {
                        break;
                    }
                    try
                    {
                        if (!IsVideoFile(this.fileList[ii]))
                        {
                            ProcessImageFile(this.fileList[ii], resizeValue, comboOption, outTypeOption, jpegQuality);
                        }
                        else
                        {
                            Tuple <double, double> trimRange = null;
                            int idx = this.trimFiles.FindIndex(a => a == this.fileList[ii]);
                            if (idx >= 0)
                            {
                                trimRange = this.trimRanges[idx];
                            }
                            ProcessVideoFile(this.fileList[ii], videoResizeValue, videoComboOption, videoOutTypeOption, mpegQuality, trimRange);
                        }
                        if (token.IsCancellationRequested)
                        {
                            break;
                        }
                        SetProgress(ii + 1);
                    }
                    catch (Exception exp)
                    {
                        Console.WriteLine(String.Format("{0} - {1}", exp.Message, exp.StackTrace));
                        failedList.Add(this.fileList[ii]);
                    }
                }
                if (token.IsCancellationRequested)
                {
                    this.OnProcessingCancelled();
                }
                else
                {
                    this.OnProcessingComplete(failedList.Count);
                }
            }, token);
        }
Beispiel #6
0
        private void ProcessImageFile(string filename, int resizeValue, comboOptions resizeOption,
                                      outTypeOptions outTypeOption, int jpegQuality)
        {
            SetCurrentProgress(0, filename);

            // Read file - will throw exception if file doesn't exist
            Image img = Image.FromFile(filename);

            string outFile = GetOutputFilename(filename, outTypeOption);

            EnsureDir(outFile); // Will throw exception if can't create folder
            DialogResult result = WarnIfExists(outFile);

            switch (result)
            {
            case DialogResult.Yes:
                break;

            case DialogResult.No:
                return;

            case DialogResult.Cancel:
                this.cancelSource.Cancel();
                return;
            }

            // Get new dimensions
            Tuple <int, int> newSize = GetNewSize(resizeOption, img.Width, img.Height, resizeValue);
            int newWidth             = newSize.Item1;
            int newHeight            = newSize.Item2;

            // Do actual resize
            Bitmap resizedBmp = ResizeImage(img, newWidth, newHeight);

            // Save result
            if (outTypeOption == outTypeOptions.match)
            {
                switch (Path.GetExtension(filename).ToLower())
                {
                case ".jpg":
                case ".jpeg":
                    outTypeOption = outTypeOptions.JPG;
                    break;

                case ".bmp":
                    outTypeOption = outTypeOptions.BMP;
                    break;

                case ".gif":
                    outTypeOption = outTypeOptions.GIF;
                    break;

                case ".png":
                    outTypeOption = outTypeOptions.PNG;
                    break;

                case ".tif":
                case ".tiff":
                    outTypeOption = outTypeOptions.TIF;
                    break;
                }
            }
            EncoderParameters eps = null;
            ImageCodecInfo    ici = GetEncoderInfo("image/bmp");

            switch (outTypeOption)
            {
            case outTypeOptions.JPG:
                eps          = new EncoderParameters(1);
                eps.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)jpegQuality);
                ici          = GetEncoderInfo("image/jpeg");
                break;

            case outTypeOptions.BMP:
                // Nothing to do - defaults are fine
                break;

            case outTypeOptions.GIF:
                ici = GetEncoderInfo("image/gif");
                break;

            case outTypeOptions.PNG:
                ici = GetEncoderInfo("image/png");
                break;

            case outTypeOptions.TIF:
                ici = GetEncoderInfo("image/tiff");
                break;
            }

            foreach (PropertyItem propItem in img.PropertyItems)
            {
                resizedBmp.SetPropertyItem(propItem);
            }

            resizedBmp.Save(outFile, ici, eps);
        }