コード例 #1
0
ファイル: ImageConvertor.cs プロジェクト: idozahavy/Convertor
 /// <summary>
 /// converts the image by class attributes
 /// </summary>
 /// <param name="imageConvertor">attributes in class are taken to the function</param>
 public static void ConvertImage(ImageConvertor imageConvertor)
 {
     ConvertImage(imageConvertor._filePath, imageConvertor._savePath, imageConvertor._imageFormat);
 }
コード例 #2
0
        private void CovertButton_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrWhiteSpace(textBox1.Text))
            {
                MessageBox.Show("Choose Folder Path", "Error");
                return;
            }
            if (!Directory.Exists(this.textBox1.Text))
            {
                MessageBox.Show("Incorrect Folder Path", "Error");
                return;
            }

            string sourceFolderPath    = textBox1.Text;
            string convertedFolderPath = sourceFolderPath + "\\converted";

            string[] sourceFiles = Directory.GetFiles(sourceFolderPath);
            if (SaveRadioConvertedFolder.Checked)
            {
                Directory.CreateDirectory(convertedFolderPath);
            }

            statusLabel.Visible = true;
            progressBar.Visible = true;
            progressBar.Maximum = sourceFiles.Length;
            progressBar.Value   = 0;

            ConvertPanel.Enabled = false;

            string      toFormatName  = (string)ToFormatComboBox.SelectedItem;
            ImageFormat toFormat      = ImageConvertor.GetImageFormatByString(toFormatName);
            string      fromExtension = FromExtensionComboBox.SelectedItem.ToString().ToLower();

            LinkedList <Thread> threadList = new LinkedList <Thread>();

            DateTime lastIntervalTime = DateTime.Now;

            statusLabel.Text = "Converting";

            int count          = 0;
            int convertedCount = 0;

            for (int index = 0; index < sourceFiles.Length; index++)
            {
                string sourceFilePath = sourceFiles[index];
                string resultFilePath;

                // Form status and progressBar update
                void FormUpdate()
                {
                    progressBar.Value = index;
                    if (DateTime.Now.Subtract(lastIntervalTime).Milliseconds > STATUS_UPDATE_INTERVAL)
                    {
                        statusLabel.Text = "Converting" + String.Concat(Enumerable.Repeat(".", (++count % 5)));
                        lastIntervalTime = DateTime.Now;
                    }
                    Application.DoEvents();
                }

                FormUpdate();

                // continue if the source file extension is not the one I'm looking for (.jpg != .bmp)
                if (!Path.GetExtension(sourceFilePath).ToLower().Equals("." + fromExtension))
                {
                    continue;
                }
                convertedCount++;

                resultFilePath  = SaveRadioConvertedFolder.Checked ? convertedFolderPath : sourceFolderPath;
                resultFilePath += "\\" + Path.GetFileNameWithoutExtension(sourceFilePath) + "." + toFormatName.ToLower();

                ImageConvertor imageConvertor = new ImageConvertor(sourceFilePath, resultFilePath, toFormat);

                // using sequential io     = 430 bmp size 5.93MB to jpg, 42 secs on SSD , 242 secs on HDD
                // using multi-threaded io = 430 bmp size 5.93MB to jpg, 14 secs on SSD , 122 secs on HDD
                threadList.AddLast(new Thread(new ThreadStart(imageConvertor.ConvertImage)));
                threadList.Last.Value.Start();

                while (threadList.Count >= THREAD_QUEUE) // Thread Queue, the longest operation is IO so IO Queue
                {
                    Thread.Sleep(THREAD_QUEUE_SLEEP);
                    FormUpdate();
                    RemoveNonLivingThreads(threadList);
                }
            }
            foreach (Thread th in threadList)
            {
                th.Join(); // wait for the last threads to finish
            }

            MessageBox.Show("Converted " + convertedCount + " Files", "Conversion Finished");

            statusLabel.Visible = false;
            progressBar.Visible = false;

            ConvertPanel.Enabled = true;
        }