using System.Diagnostics; namespace SCI.AsciiArt; public class Generator { public static async Task FromImage (string imagePath, int width) { // Ensure image exists if (!File.Exists (imagePath)) { throw new FileNotFoundException ($"The specified file '{imagePath}' does not exist"); } var process = new Process (); process.StartInfo = new ProcessStartInfo () { FileName = "jp2a", Arguments = $"--width={width} \"{imagePath}\"", RedirectStandardInput = true, RedirectStandardOutput = true, RedirectStandardError = true, CreateNoWindow = true, UseShellExecute = false }; process.Start (); // Wait up to one second for the task to finish try { var cancellationTokenSource = new CancellationTokenSource (1000); await process.WaitForExitAsync (cancellationTokenSource.Token); } catch (TaskCanceledException) { throw new TimeoutException ("jp2a did not finish after 1000ms, process was killed"); } if (process.ExitCode != 0) { throw new Exception ($"jp2a returned with exit code {process.ExitCode}.\n{process.StandardError.ReadToEnd ()}"); } return await process.StandardOutput.ReadToEndAsync (); } }