Ejemplo n.º 1
0
        private void picsorBackgroundWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            _directory = openFileDialog
                         .FileName.Substring(0, openFileDialog.FileName.LastIndexOf('\\'))
                         + "\\PicsorConverted";

            if (!Directory.Exists(_directory))
            {
                Directory.CreateDirectory(_directory);
            }

            foreach (var file in openFileDialog.FileNames)
            {
                ImageCompressor imageCompressor = new ImageCompressor(new Bitmap(file));

                // Type of resizing
                Bitmap resized = null;
                if (resizingSelection.Current.Text == "Size")
                {
                    resized = imageCompressor.GetResizeUsingMaxDimensions(pbMaxWidth.Value, pbMaxHeight.Value);
                }
                else if (pbPercent.Value != 100)
                {
                    resized = imageCompressor.GetResizedUsingPercentage(pbPercent.Value / 100f);
                }

                // Define forced codec
                ImageCodecInfo codec = null;

                if (formatSelection.Current.Text == "JPG")
                {
                    codec = imageCompressor.GetCodec(ImageFormat.Jpeg);
                }
                else if (formatSelection.Current.Text == "PNG")
                {
                    codec = imageCompressor.GetCodec(ImageFormat.Png);
                }

                // Update output filename for forced codec
                string outputFilename = file.Substring(file.LastIndexOf('\\') + 1);
                if (codec != null)
                {
                    outputFilename += $".{formatSelection.Current.Text}";
                }
                // Or keep current codec
                else
                {
                    codec = imageCompressor.GetCurrentCodec();
                }

                // Encode and save
                imageCompressor.EncodeAndSave(
                    codec,
                    _directory + "\\" + outputFilename,
                    pbQuality.Value,
                    resized);
            }
        }