One thing I didn’t consider when using my Sony DSC-W800 20.1mp digital camera, is when I run them through Photolemur 3 for colour and shading enhancement, my images go from around 8mb to around 24mb each. Each time for posting here, on social media, or even sharing via WhatsApp I’ve struggled with the sizes a little. After the schlep of doing it manually several times, I figured why not just make it into a single ‘button click‘ for myself.
Hence, today, just to share, in less than a minute with Copilot, I tossed together ImageMinifier (v1; 69kb). A simple windows forms application using C# 12 and .Net 8.0 which goes through all files in the directory and if it’s a PNG, my preferred image output format, it makes it 25% the size and saves it in a new folder.
I guessed this might be useful for others, so figured I would share it here as well. This makes all my large, adjusted, images into small enough images I could easily share in all the spots for the public.
using (var folderDialog = new FolderBrowserDialog())
{
DialogResult result = folderDialog.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(folderDialog.SelectedPath))
{
MessageBox.Show("Please wait a moment while images are processed...");
string selectedFolder = folderDialog.SelectedPath;
// Create a folder with current date and time numerics only
string folderName = DateTime.Now.ToString("yyyyMMddHHmmss");
string folderPath = Path.Combine(selectedFolder, folderName);
Directory.CreateDirectory(folderPath);
// Get the path of the created folder
string folderPathString = folderPath;
// Get all PNG files in the selected folder
string[] pngFiles = Directory.GetFiles(selectedFolder, "*.png");
foreach (string pngFile in pngFiles)
{
// Load the image into memory
using (Image image = Image.FromFile(pngFile))
{
// Calculate the new width and height
int newWidth = (int)(image.Width * 0.25);
int newHeight = (int)(image.Height * 0.25);
// Create a new bitmap with the resized dimensions
using (Bitmap resizedImage = new Bitmap(newWidth, newHeight))
{
// Set the resolution to the same as the original image
resizedImage.SetResolution(image.HorizontalResolution, image.VerticalResolution);
// Create a graphics object from the resized image
using (Graphics graphics = Graphics.FromImage(resizedImage))
{
// Set the interpolation mode to high quality
graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
// Draw the original image onto the resized image
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
// Save the resized image to the folderPathString
string resizedImagePath = Path.Combine(folderPathString, Path.GetFileName(pngFile));
resizedImage.Save(resizedImagePath, ImageFormat.Png);
}
}
}
MessageBox.Show("Done, enjoy the minified images!");
}
}