I'm semi-new to programming and want to improve my skills by understanding how people might optimize their code and how the code should be structured since I've been told it needs to do more than just work properly. It gets the brightness of each pixel and the darker the pixel, the denser the ASCII character it writes will be.
using System.Drawing; using System.Net; class Generator { static void Main() { try { while (true) { string imagePath = ""; Bitmap image = null; while (true) { Console.Clear(); Console.Write("Image Path/URL: "); imagePath = Console.ReadLine().Trim(); Console.Clear(); if (Path.Exists(imagePath)) { image = new(imagePath); break; } else if (imagePath.StartsWith("http")) { Console.WriteLine("Preparing image..."); using WebClient client = new(); byte[] imageData = client.DownloadData(imagePath); using MemoryStream stream = new(imageData); image = new Bitmap(stream); Console.Clear(); break; } Console.WriteLine("Invalid path/URL. Press Enter."); Console.ReadLine(); } bool rerun = true; while (rerun) { rerun = false; Console.WriteLine("Scaling Mode:"); Console.WriteLine("[1] Default"); Console.WriteLine("[2] High Quality (Stretches Image)"); Console.WriteLine("[3] Specific Character Count"); Console.WriteLine("[4] Specific Character Count + High Quality (Stretches Image)"); ConsoleKeyInfo keyInfo = Console.ReadKey(); Console.Clear(); switch (keyInfo.Key) { case ConsoleKey.D1: ConvertToASCII(image, 2); break; case ConsoleKey.D2: ConvertToASCII(image, 1); break; case ConsoleKey.D3: ConvertToCharacterCountASCII(image, 2); break; case ConsoleKey.D4: ConvertToCharacterCountASCII(image, 1); break; default: Console.WriteLine("Invalid Input. Press Enter."); Console.ReadLine(); rerun = true; Console.Clear(); break; } } Console.ReadLine(); Console.Clear(); } } catch (Exception ex) { Console.Clear(); Console.WriteLine($"Error: {ex}"); Console.ReadLine(); } } static void ConvertToASCII(Bitmap image, int iterator) { Console.Clear(); for (int y = 0; y < image.Height; y += iterator) { for (int x = 0; x < image.Width; x++) { Color pixelColor = image.GetPixel(x, y); float brightnessUnrounded = pixelColor.GetBrightness(); double brightness = Math.Floor(brightnessUnrounded * 100); // convert 0.984... to 98 if (brightness > 80) Console.Write("█"); else if (brightness > 70) Console.Write("▌"); else if (brightness > 60) Console.Write("▀"); else if (brightness > 50) Console.Write("#"); else if (brightness > 40) Console.Write("&"); else if (brightness > 30) Console.Write("o"); else if (brightness > 20) Console.Write("+"); else if (brightness > 10) Console.Write(";"); else Console.Write("."); if (x == 0) Console.WriteLine(); } } image.Dispose(); } static void ConvertToCharacterCountASCII(Bitmap image, int iterator) { double characterCount; while (true) { Console.Clear(); Console.Write("Character Count: "); try { characterCount = Convert.ToDouble(Console.ReadLine().Trim()); break; } catch { Console.Clear(); Console.WriteLine("Invalid Number. Press Enter"); Console.ReadLine(); } } // Calculate scaling to fit X characters + aspect ratio double scaleFactor = Math.Sqrt(characterCount / (image.Width * image.Height)); int newWidth = (int)(image.Width * scaleFactor); int newHeight = (int)(image.Height * scaleFactor); ConvertToASCII(new Bitmap(image, newWidth, newHeight), iterator); image.Dispose(); } }