using System; using System.Diagnostics.CodeAnalysis; using System.Drawing; using System.Drawing.Imaging; using System.IO; namespace MaddoShared.Benchmarks.Helpers; /// /// Helper class to generate test images for benchmarking /// [SuppressMessage("Interoperability", "CA1416:Validate platform compatibility")] public static class TestImageGenerator { /// /// Generates a set of test JPEG images in the specified directory /// /// Directory where images will be created /// Number of images to generate /// Width of each image /// Height of each image /// Whether to create images in subfolders public static void GenerateTestImages( string outputDirectory, int imageCount, int width = 4000, int height = 3000, bool includeSubfolders = false) { Directory.CreateDirectory(outputDirectory); var random = new Random(42); // Fixed seed for reproducibility for (int i = 0; i < imageCount; i++) { var targetDir = outputDirectory; if (includeSubfolders && i % 10 == 0) { targetDir = Path.Combine(outputDirectory, $"Subfolder_{i / 10}"); Directory.CreateDirectory(targetDir); } var filePath = Path.Combine(targetDir, $"test_image_{i:D5}.jpg"); // Skip if already exists if (File.Exists(filePath)) continue; using var bitmap = new Bitmap(width, height); using var graphics = Graphics.FromImage(bitmap); // Fill with a random color background var bgColor = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256)); graphics.Clear(bgColor); // Draw some random shapes to make it more realistic for (int j = 0; j < 20; j++) { var color = Color.FromArgb(random.Next(256), random.Next(256), random.Next(256)); var brush = new SolidBrush(color); var x = random.Next(width); var y = random.Next(height); var w = random.Next(200, 800); var h = random.Next(200, 800); graphics.FillEllipse(brush, x, y, w, h); } // Add some text using var font = new Font("Arial", 48, FontStyle.Bold); var text = $"Test Image {i}"; var textBrush = new SolidBrush(Color.White); graphics.DrawString(text, font, textBrush, new PointF(100, 100)); // Save as JPEG with standard quality var encoder = GetEncoder(ImageFormat.Jpeg); var encoderParameters = new EncoderParameters(1); encoderParameters.Param[0] = new EncoderParameter(Encoder.Quality, 85L); bitmap.Save(filePath, encoder, encoderParameters); } } /// /// Cleans up generated test images /// public static void CleanupTestImages(string directory) { if (Directory.Exists(directory)) { Directory.Delete(directory, recursive: true); } } private static ImageCodecInfo GetEncoder(ImageFormat format) { var codecs = ImageCodecInfo.GetImageEncoders(); foreach (var codec in codecs) { if (codec.FormatID == format.Guid) { return codec; } } return null; } }