using System; using System.Collections.Generic; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CarManagerV2 { internal class ImageManager { public static void initializeImageFolder() { string path = "images"; if (!System.IO.Directory.Exists(path)) { System.IO.Directory.CreateDirectory(path); } } public static string getImagePath(Car car) { string basePath = "images/"; string fileName = $"{car.Make}_{car.Model}_{car.Year}_{car.Color}.png"; return basePath + fileName; } public static Image GetImage(Car car) { initializeImageFolder(); fetchImage(car); string path = getImagePath(car); // does image exist? if (System.IO.File.Exists(path)) { return Image.FromFile(path); } else { return Image.FromFile("images/no_image_available.png"); } } public static void fetchImage(Car car) { // Fetch the image from https://cdn.imagin.studio/getimage and save it to images/Make_Model_Year.webp // use params like this: ?customer=hrjavascript-mastery&zoomType=fullscreen&make={make}&modelFamily={model}&modelYear={year}&angle=front&paintDescription={color}&fileType=png // check if the image already exists string path = getImagePath(car); if (System.IO.File.Exists(path)) { return; } string url = $"https://cdn.imagin.studio/getimage?customer=hrjavascript-mastery&zoomType=fullscreen&make={car.Make}&modelFamily={car.Model}&modelYear={car.Year}&angle=front&paintDescription={car.Color}&fileType=png"; //add Referer header using (var client = new System.Net.WebClient()) { client.Headers.Add("Referer", "http://localhost"); try { client.DownloadFile(url, path); } catch (Exception ex) { // if error, use no_image_available.png System.IO.File.Copy("images/no_image_available.png", path); } } } } }