/// <summary> /// Must be wrapped in a trycatch for catching cancellation exceptions. /// </summary> /// <param name="clearCache"></param> /// <param name="_worker"></param> /// <exception cref="OperationCanceledException"></exception> /// <exception cref="TaskCanceledException"></exception> public void PreRenderImage(bool clearCache, CancellationToken _worker) { if (clearCache) { this.PreRenderedImage.DisposeSafely(); this.PreRenderedImage = null; this.History.Clear(); } if (this.PreRenderedImage == null) { var srcImage = PngFormatter.ResizeAndFormatRawImage(this.LoadedImage); PngFormatter.QuantizeImage(_worker, ref srcImage); // Do this step BEFORE setting panzoom to null. Very important. this.PreRenderedImage = srcImage; // Resize based on new size if (this.LoadedImage.Width != this.PreRenderedImage.Width || this.LoadedImage.Height != this.PreRenderedImage.Height) { MainForm.PanZoomSettings = null; } this.InvokeEx((c) => { c.imagePanelMain.SetImage(c.PreRenderedImage); c.ShowImagePanel(); }); } }
static void Main(string[] args) { if (!args.Any()) { Application.ThreadException += MainForm.OnThreadException; AppDomain.CurrentDomain.UnhandledException += MainForm.OnUnhandledException; Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException); //SetLocaleByTextureSize(Constants.TextureSize); Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new MainForm()); return; } else { Bitmap inputImage = null; string outputPath = null; string format = null; foreach (var arg in args) { string lArg = arg.ToLowerInvariant(); if (lArg.StartsWith("--options=")) { Options.Import(arg.Substring("--options=".Length)); } else if (lArg.StartsWith("--input=")) { inputImage = (Bitmap)Bitmap.FromFile(arg.Substring("--input=".Length)); } else if (lArg.StartsWith("--output=")) { outputPath = arg.Substring("--output=".Length); string ext = arg.Split(new char[] { '.' }, StringSplitOptions.RemoveEmptyEntries).LastOrDefault(); if (ext != null && format == null) { switch (ext.ToUpperInvariant()) { case "SCHEMATIC": case "PNG": case "SCHEM": format = ext.ToUpperInvariant(); break; } } } else if (lArg.StartsWith("--format=")) { format = arg.Substring("--format=".Length).ToUpperInvariant(); } else { if (lArg.StartsWith("--help")) { Console.WriteLine(@" ------------------ PixelStacker Help ---------------------------------------- The following options are available for CLI usage. If no CLI params are provided, the standard program GUI will be used instead. --format Specify the output format to use. Available values are: SCHEM, PNG Example: '--format=PNG' --output Where to output the file. Bitmap.Save({your value}) Example: '--output=C:\git\my-output-file.png' --input Where to load the file. Bitmap.FromFile({your value}) Example: '--input=C:\git\myfile.png' --options Path to the options.json file to use. If not provided, your most recently used settings will be applied. Example: '--options=C:\git\myfile.json' "); return; } } } if (string.IsNullOrWhiteSpace(format)) { Console.Error.WriteLine($"No value given for the --format argument. Valid values: [PNG, SCHEM]"); return; } if (string.IsNullOrWhiteSpace(outputPath)) { Console.Error.WriteLine($"No value given for the --output argument. Must be a full file path."); return; } else if (File.Exists(outputPath)) { Console.Error.WriteLine($"Warning, the output file already exists. It will be erased."); File.Delete(outputPath); } if (inputImage == null) { Console.Error.WriteLine($"File path for --input was invalid. File not found, or file could not be loaded."); return; } TaskManager.Get.TryTaskCatchCancelSync(() => { switch (format) { case "PNG": PngFormatter.writePNG(CancellationToken.None, outputPath, inputImage).ConfigureAwait(true).GetAwaiter().GetResult(); Console.WriteLine("Finished!"); break; case "SCHEM": Schem2Formatter.writeSchemFromImage(CancellationToken.None, outputPath, inputImage).ConfigureAwait(true).GetAwaiter().GetResult(); Console.WriteLine("Finished!"); break; } }); } }