Загрузка данных
using ImageMagick;
using FFMpegCore;
using Spire.Doc;
using Spire.Presentation;
namespace UltraConvert
{
public partial class Form1 : Form
{
// ТЕ САМЫЕ СПИСКИ, про которые ты говорил
private readonly List<string> imageExtensions = new List<string> { ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".heic", ".avif" };
private readonly List<string> docExtensions = new List<string> { ".docx", ".doc", ".pptx", ".pdf" };
private readonly List<string> videoExtensions = new List<string> { ".mp4", ".avi", ".mkv", ".mov", ".flv" };
public Form1()
{
InitializeComponent();
SetupTools();
// Привязываем кнопки (на случай, если в дизайнере слетели события)
this.btnSelectFiles.Click += btnSelectFiles_Click;
this.btnConvert.Click += btnConvert_Click;
this.btnClear.Click += btnClear_Click;
}
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) return;
btnConvert.Enabled = false;
for (int i = 0; i < listBoxQueue.Items.Count; i++)
{
string filePath = listBoxQueue.Items[i].ToString()!;
lblStatus.Text = $"Обработка: {Path.GetFileName(filePath)}";
try
{
await Task.Run(() => ProcessFile(filePath));
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка в {Path.GetFileName(filePath)}: {ex.Message}");
}
}
lblStatus.Text = "Готово!";
btnConvert.Enabled = true;
}
private void ProcessFile(string input)
{
string ext = Path.GetExtension(input).ToLower();
string folder = Path.GetDirectoryName(input)!;
string name = Path.GetFileNameWithoutExtension(input);
int selectedTarget = 0;
this.Invoke(new Action(() => { selectedTarget = comboBoxFormats.SelectedIndex; }));
// ПРОВЕРКА ПО ТВОИМ СПИСКАМ
if (imageExtensions.Contains(ext))
{
using (var img = new MagickImage(input))
{
img.Write(Path.Combine(folder, name + ".jpg"));
}
}
else if (docExtensions.Contains(ext))
{
if (ext == ".docx" || ext == ".doc")
{
var doc = new Document();
doc.LoadFromFile(input);
doc.SaveToFile(Path.Combine(folder, name + ".pdf"), Spire.Doc.FileFormat.PDF);
}
else if (ext == ".pptx")
{
var ppt = new Presentation();
ppt.LoadFromFile(input);
ppt.SaveToFile(Path.Combine(folder, name + ".pdf"), Spire.Presentation.FileFormat.PDF);
}
}
else if (videoExtensions.Contains(ext))
{
// Если выбрано "В MP3"
if (selectedTarget == 2)
{
FFMpeg.ExtractAudio(input, Path.Combine(folder, name + ".mp3"));
}
else
{
// Тут можно добавить конвертацию видео в видео, если нужно
}
}
}
}
}