Загрузка данных


using ImageMagick;
using FFMpegCore;
using Spire.Doc;
using Spire.Presentation;

namespace UltraConvert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SetupTools();
            comboBoxFormats.SelectedIndex = 0; // Выбираем первый формат по умолчанию
        }

        private void SetupTools()
        {
            string baseDir = AppDomain.CurrentDomain.BaseDirectory;
            MagickNET.SetGhostscriptDirectory(baseDir);
            GlobalFFOptions.Configure(options => options.BinaryFolder = baseDir);
        }

        private void btnSelectFiles_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog ofd = new OpenFileDialog { Multiselect = true })
            {
                if (ofd.ShowDialog() == DialogResult.OK)
                {
                    foreach (string file in ofd.FileNames)
                    {
                        if (!listBoxQueue.Items.Contains(file))
                            listBoxQueue.Items.Add(file);
                    }
                }
            }
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            listBoxQueue.Items.Clear();
            lblStatus.Text = "Очередь очищена";
        }

        private async void btnConvert_Click(object sender, EventArgs e)
        {
            if (listBoxQueue.Items.Count == 0)
            {
                MessageBox.Show("Очередь пуста!");
                return;
            }

            btnConvert.Enabled = false;
            int count = 0;

            foreach (string item in listBoxQueue.Items)
            {
                count++;
                lblStatus.Text = $"Обработка {count} из {listBoxQueue.Items.Count}...";
                
                try
                {
                    await Task.Run(() => ProcessFile(item.ToString()));
                }
                catch (Exception ex)
                {
                    MessageBox.Show($"Ошибка в файле {Path.GetFileName(item.ToString())}: {ex.Message}");
                }
            }

            lblStatus.Text = "Всё готово!";
            btnConvert.Enabled = true;
            MessageBox.Show("Все задачи выполнены!");
        }

        private void ProcessFile(string input)
        {
            string folder = Path.GetDirectoryName(input);
            string fileName = Path.GetFileNameWithoutExtension(input);
            int mode = 0;

            // Узнаем, что выбрал пользователь в ComboBox (через Invoke, так как мы в другом потоке)
            this.Invoke(new Action(() => { mode = comboBoxFormats.SelectedIndex; }));

            if (mode == 0) // В JPEG
            {
                using (var img = new MagickImage(input))
                {
                    img.Write(Path.Combine(folder, fileName + ".jpg"));
                }
            }
            else if (mode == 1) // В PDF
            {
                string ext = Path.GetExtension(input).ToLower();
                if (ext == ".docx" || ext == ".doc")
                {
                    Document doc = new Document();
                    doc.LoadFromFile(input);
                    doc.SaveToFile(Path.Combine(folder, fileName + ".pdf"), Spire.Doc.FileFormat.PDF);
                }
                else if (ext == ".pptx")
                {
                    Presentation ppt = new Presentation();
                    ppt.LoadFromFile(input);
                    ppt.SaveToFile(Path.Combine(folder, fileName + ".pdf"), Spire.Presentation.FileFormat.PDF);
                }
            }
            else if (mode == 2) // В MP3
            {
                FFMpeg.ExtractAudio(input, Path.Combine(folder, fileName + ".mp3"));
            }
        }
    }
}