Mk0.Software.ImageSorter/Mk0.Software.ImageSorter/Konverter.cs
2024-08-19 09:51:28 +02:00

71 lines
2.3 KiB
C#

using ImageMagick;
using System;
using System.IO;
using System.Windows.Forms;
namespace Mk0.Software.ImageSorter
{
public partial class Konverter : Form
{
private string imagePath;
public string newFilePath { get; set; }
public Konverter(string imagePath, bool auto = false, MagickFormat autoFormat = MagickFormat.Png)
{
this.imagePath = imagePath;
if (auto)
{
using (MagickImage image = new MagickImage(imagePath))
{
image.Format = autoFormat;
newFilePath = Path.Combine(Path.GetDirectoryName(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "." + autoFormat.ToString().ToLower());
image.Write(newFilePath);
}
}
else
{
InitializeComponent();
labelBildname.Text = imagePath;
comboBoxFormat.SelectedIndex = 0;
}
}
private void ButtonKonvert_Click(object sender, EventArgs e)
{
using (MagickImage image = new MagickImage(imagePath))
{
if (comboBoxFormat.SelectedItem == "PNG")
{
image.Format = MagickFormat.Png;
}
else if (comboBoxFormat.SelectedItem == "JPG")
{
image.Format = MagickFormat.Jpeg;
}
else if (comboBoxFormat.SelectedItem == "BMP")
{
image.Format = MagickFormat.Bmp;
}
// Save frame as jpg
newFilePath = Path.Combine(Path.GetDirectoryName(imagePath), Path.GetFileNameWithoutExtension(imagePath) + "." + comboBoxFormat.SelectedItem.ToString().ToLower());
image.Write(newFilePath);
}
this.DialogResult = DialogResult.OK;
this.Close();
}
private void ComboBoxFormat_SelectedIndexChanged(object sender, EventArgs e)
{
if (File.Exists(imagePath) && comboBoxFormat.SelectedIndex != -1)
{
buttonKonvert.Enabled = true;
}
else
{
buttonKonvert.Enabled = false;
}
}
}
}