Esempio n. 1
0
        public void Start(RecompressorParameter parameter)
        {
            OnProgress("Starting", 1);
            var files  = Directory.EnumerateFiles(parameter.Source, "*", SearchOption.AllDirectories).ToList();
            var list   = new ConcurrentStack <string>();
            var length = files.Count;

            Task.Factory.StartNew(() =>
            {
                QParallel.ForEach(files, parameter.ThreadCount, (file) =>
                {
                    list.Push(file);

                    OnProgress(file, list.Count * 100 / length);

                    var bitmap = JpegToBitmap(file);

                    if (parameter.Width.HasValue && parameter.Height.HasValue)
                    {
                        bitmap = Resize(bitmap, parameter.Width.Value, parameter.Height.Value);
                    }

                    var nFile = file.Replace(parameter.Source, "");
                    nFile     = string.Concat(parameter.Destination, nFile);

                    using (var ms1 = MakeJpeg(bitmap, parameter.Quality))
                        using (var fileStream = new FileStream(nFile, FileMode.Create))
                        {
                            ms1.CopyTo(fileStream);
                        }
                });

                OnProgress("Finish", 100);
            });
        }
Esempio n. 2
0
        private void btnStart_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txbSource.Text) || string.IsNullOrEmpty(txbDestination.Text))
            {
                MessageBox.Show("You should select source and destination folder for start");
                return;
            }

            var parameter = new RecompressorParameter
            {
                Source      = txbSource.Text,
                Destination = txbDestination.Text,
                Quality     = tbQuality.Value * 10,
                ThreadCount = tbThread.Value
            };

            if (chkChangeResolution.Checked)
            {
                parameter.Width  = float.Parse(txbWidth.Text);
                parameter.Height = float.Parse(txbHeight.Text);
            }


            Recompressor recompressor = new Recompressor();

            recompressor.progressEvent += (info, progress) =>
            {
                if (info == "Finish")
                {
                    MessageBox.Show("Finish");
                }

                BeginInvoke((Action)(() => { progressBar1.Value = progress; }), null);
            };

            recompressor.Start(parameter);
        }