Skip to content

Commit

Permalink
optimize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
tinohager committed Aug 9, 2023
1 parent ba3a769 commit 1183396
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/WebpCompression/Form1.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,24 @@ namespace WebpCompression
{
public partial class Form1 : Form
{
private string _processPath;

public Form1()
{
this.InitializeComponent();

var binaryDirectory = "bin";

if (!Directory.Exists(binaryDirectory))
{
Directory.CreateDirectory(binaryDirectory);
}

this._processPath = Path.Combine(binaryDirectory, "cwebp.exe");
if (!File.Exists(this._processPath))
{
MessageBox.Show("Cannot found cwebp.exe, please download and copy into bin directory", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}

private void buttonSelectFiles_Click(object sender, EventArgs e)
Expand All @@ -21,6 +36,11 @@ private void buttonSelectFiles_Click(object sender, EventArgs e)
private void buttonOptimize_Click(object sender, EventArgs e)
{
var imageInfos = this.dataGridView1.DataSource as ImageInfo[];
if (imageInfos == null)
{
return;
}

foreach (var imageInfo in imageInfos)
{
var fileInfo = new FileInfo(imageInfo.Name);
Expand All @@ -29,8 +49,8 @@ private void buttonOptimize_Click(object sender, EventArgs e)

var processStartInfo = new ProcessStartInfo
{
FileName = @"bin\cwebp.exe",
Arguments = $"\"{imageInfo.Name}\" -o {newFileName}",
FileName = this._processPath,
Arguments = $"\"{imageInfo.Name}\" -o \"{newFileName}\"",
RedirectStandardOutput = true,
RedirectStandardError = true,
CreateNoWindow = true
Expand Down

0 comments on commit 1183396

Please sign in to comment.