/// <summary> /// Use reflection to determine all IRecipe objects in the project, execute each of them, /// and save the output using the recipe ID as its base filename. /// </summary> public void CreateCookbookImages(string outputPath) { outputPath = Path.GetFullPath(outputPath); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } var recipes = Locate.GetRecipes(); Console.WriteLine($"Cooking {recipes.Length} recipes in: {outputPath}"); Parallel.ForEach(recipes, recipe => { Debug.WriteLine($"Executing {recipe.ID}"); var plt = new Plot(Width, Height); recipe.ExecuteRecipe(plt); // save full size image Bitmap bmp = plt.Render(); string fileName = (recipe.ID + Ext).ToLower(); string filePath = Path.Combine(outputPath, fileName); bmp.Save(filePath, System.Drawing.Imaging.ImageFormat.Png); // thumbnail int thumbHeight = 180; int thumbWidth = thumbHeight * bmp.Width / bmp.Height; Bitmap thumb = Drawing.GDI.Resize(bmp, thumbWidth, thumbHeight); string thumbFileName = (recipe.ID + ExtThumb).ToLower(); string thumbFilePath = Path.Combine(outputPath, thumbFileName); thumb.Save(thumbFilePath, System.Drawing.Imaging.ImageFormat.Jpeg); }); }
public static void Generate(string outputPath, int width = 600, int height = 400, int thumbJpegQuality = 95) { outputPath = Path.GetFullPath(outputPath); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } IRecipe[] recipes = Locate.GetRecipes(); EncoderParameters thumbJpegEncoderParameters = new(1); thumbJpegEncoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, thumbJpegQuality); ImageCodecInfo thumbJpegEncoder = ImageCodecInfo.GetImageEncoders().Where(x => x.MimeType == "image/jpeg").First(); Parallel.ForEach(recipes, recipe => { Console.WriteLine($"Generating: {recipe.Category.Name} - {recipe.Title}"); var plt = new Plot(width, height); recipe.ExecuteRecipe(plt); // save full size image Bitmap bmp = plt.Render(); string filePath = Path.Combine(outputPath, recipe.ID.ToLower() + ".png"); bmp.Save(filePath, ImageFormat.Png); // thumbnail int thumbHeight = 180; int thumbWidth = thumbHeight * bmp.Width / bmp.Height; Bitmap thumb = Drawing.GDI.Resize(bmp, thumbWidth, thumbHeight); string thumbFilePath = Path.Combine(outputPath, recipe.ID.ToLower() + "_thumb.jpg"); thumb.Save(thumbFilePath, thumbJpegEncoder, thumbJpegEncoderParameters); }); }
/// <summary> /// Use REFLECTION to locate all recipes, execute them and save the output images. /// </summary> /// <returns>array of recipes found using reflection</returns> public static IRecipe[] Generate(string outputPath, int width = 600, int height = 400, int thumbJpegQuality = 95) { outputPath = Path.GetFullPath(outputPath); if (!Directory.Exists(outputPath)) { Directory.CreateDirectory(outputPath); } IRecipe[] recipes = Locate.GetRecipes(); EncoderParameters thumbJpegEncoderParameters = new(1); thumbJpegEncoderParameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, thumbJpegQuality); ImageCodecInfo thumbJpegEncoder = ImageCodecInfo.GetImageEncoders().Where(x => x.MimeType == "image/jpeg").First(); Parallel.ForEach(recipes, recipe => { var sw = System.Diagnostics.Stopwatch.StartNew(); var plt = new Plot(width, height); recipe.ExecuteRecipe(plt); // save full size image Bitmap bmp = plt.Render(); string filePath = Path.Combine(outputPath, recipe.ID.ToLower() + ".png"); bmp.Save(filePath, ImageFormat.Png); // calculate image hash Console.WriteLine($"{recipe.ID},{Tools.BitmapHash(bmp)},{sw.Elapsed.TotalMilliseconds}"); // thumbnail int thumbHeight = 180; int thumbWidth = thumbHeight * bmp.Width / bmp.Height; Bitmap thumb = Drawing.GDI.Resize(bmp, thumbWidth, thumbHeight); string thumbFilePath = Path.Combine(outputPath, recipe.ID.ToLower() + "_thumb.jpg"); thumb.Save(thumbFilePath, thumbJpegEncoder, thumbJpegEncoderParameters); }); return(recipes); }