Загрузка данных
using ImageMagick;
using FFMpegCore;
using Spire.Doc;
using Spire.Presentation;
using System.IO;
namespace UltraConvert
{
public partial class Form1 : Form
{
// Списки расширений из первой версии
private List<string> imageExt = new List<string> { ".jpg", ".jpeg", ".png", ".bmp", ".webp", ".heic", ".avif" };
private List<string> docExt = new List<string> { ".docx", ".doc" };
private List<string> videoExt = new List<string> { ".mp4", ".avi", ".mkv", ".mov" };
private List<string> presExt = new List<string> { ".pptx" };
public Form1()
{
InitializeComponent();
SetupTools();
}
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;
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
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, desktopPath));
}
catch (Exception ex)
{
MessageBox.Show($"Ошибка в {Path.GetFileName(filePath)}: {ex.Message}");
}
}
lblStatus.Text = "Готово! Файлы на рабочем столе.";
btnConvert.Enabled = true;
}
private void ProcessFile(string input, string outputDir)
{
string ext = Path.GetExtension(input).ToLower();
string name = Path.GetFileNameWithoutExtension(input);
int mode = 0;
this.Invoke(new Action(() => { mode = comboBoxFormats.SelectedIndex; }));
if (imageExt.Contains(ext))
{
using (var img = new MagickImage(input))
{
img.Write(Path.Combine(outputDir, name + ".jpg"));
}
}
else if (docExt.Contains(ext))
{
var doc = new Document();
doc.LoadFromFile(input);
doc.SaveToFile(Path.Combine(outputDir, name + ".pdf"), Spire.Doc.FileFormat.PDF);
}
else if (videoExt.Contains(ext))
{
FFMpeg.ExtractAudio(input, Path.Combine(outputDir, name + ".mp3"));
}
else if (presExt.Contains(ext))
{
if (mode == 3) // Режим: Извлечь фото из PPTX
{
Presentation ppt = new Presentation();
ppt.LoadFromFile(input);
for (int i = 0; i < ppt.Images.Count; i++)
{
File.WriteAllBytes(Path.Combine(outputDir, $"{name}_img_{i}.png"), ppt.Images[i].Data);
}
}
else // Режим: Презентацию в PDF
{
Presentation ppt = new Presentation();
ppt.LoadFromFile(input);
ppt.SaveToFile(Path.Combine(outputDir, name + ".pdf"), Spire.Presentation.FileFormat.PDF);
}
}
}
}
}