I created a little tool to help me take a folder of images and convert them into a properly formatted structure for a photo gallery I'm working on. This includes keeping the original high res photo, and making a smaller version for previewing.
A gallery with 260 images takes 11,631ms on my laptop and a gallery with 10 takes about 450ms. Is there anything I can do to make this code more efficient?
Note: All photos that are sent to me for the photo gallery are a JPEG.
MainForm.cs
using System;
using System.Diagnostics;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Windows.Forms;
namespace PhotoGalleryFormatter
{
public partial class MainForm : Form
{
private string _originalDirectory;
private string _outputDirectory;
private string _galleryName = "formatted-gallery";
private string[] _originalFiles;
private Stopwatch _timer;
private const double DownscaleRatio = .35;
/// <summary>
/// Initialize Form
/// </summary>
public MainForm()
{
InitializeComponent();
_timer = new Stopwatch();
}
/// <summary>
/// Opens FolderBrowserDialog to select original directory to work with
/// </summary>
private void OpenFolderBrowserDialog()
{
using (var dialog = new FolderBrowserDialog())
{
var result = dialog.ShowDialog() == DialogResult.OK;
if (result && !string.IsNullOrWhiteSpace(dialog.SelectedPath) )
{
_originalDirectory = dialog.SelectedPath;
_outputDirectory = _originalDirectory + @"\formatted-gallery\";
txtFileDir.Text = _originalDirectory;
txtOutputDir.Text = _outputDirectory;
_originalFiles = Directory.GetFiles(dialog.SelectedPath, "*.jpg");
}
btnFormat.Enabled = result;
}
}
/// <summary>
/// Auto-Generates a output directory based on gallery name
/// </summary>
private void CalculateOutputDirectory()
{
if ( !string.IsNullOrWhiteSpace(txtGalleryName.Text) )
{
_galleryName = txtGalleryName.Text;
_galleryName = _galleryName.Replace(' ', '-');
_outputDirectory = _originalDirectory + @"\" + _galleryName + @"\";
}
else
{
_galleryName = "formatted-gallery";
_outputDirectory = _originalDirectory + @"\formatted-gallery\";
}
txtOutputDir.Text = _outputDirectory;
}
/// <summary>
/// Formats gallery file names
/// </summary>
private void FormatGallery()
{
_timer.Start();
if( Directory.Exists(_outputDirectory) )
Directory.Delete(_outputDirectory);
if ( Directory.Exists(_outputDirectory + @"\preview") )
Directory.Delete(_outputDirectory + @"\preview");
Directory.CreateDirectory(_outputDirectory);
Directory.CreateDirectory(_outputDirectory + @"\preview");
for (var i = 0; i < _originalFiles.Length; i++)
{
if ( File.Exists(_originalFiles[i]) )
{
var path = _galleryName + "-" + (i + 1) + ".jpg";
File.Copy(_originalFiles[i], _outputDirectory + path);
CreatePreviewImage(path);
}
}
_timer.Stop();
MessageBox.Show("Gallery " + _galleryName + " has been formatted in " + _timer.ElapsedMilliseconds + "ms.",
"Gallery Format Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
_timer.Reset();
}
/// <summary>
/// Resizes image for preview folder
/// </summary>
/// <param name="path">Path of image</param>
private void CreatePreviewImage(string path)
{
using (var originalImage = Image.FromFile(_outputDirectory + path))
{
using (var resizedImage = DownsizeImage(originalImage))
{
resizedImage.Save(_outputDirectory + @"preview\" + path, ImageFormat.Jpeg);
}
}
}
/// <summary>
/// Downsizes image to preview size using _downscaleRatio
/// </summary>
/// <param name="image">Original un-scaled image</param>
/// <returns>New downsized image</returns>
private Image DownsizeImage(Image image)
{
var newWidth = (int) (image.Width * DownscaleRatio);
var newHeight = (int) (image.Height * DownscaleRatio);
var newImage = new Bitmap(newWidth, newHeight);
using (var graphics = Graphics.FromImage(newImage))
{
graphics.DrawImage(image, 0, 0, newWidth, newHeight);
}
return newImage;
}
private void btnLoadDir_Click(object sender, EventArgs e)
{
OpenFolderBrowserDialog();
}
private void txtGalleryName_TextChanged(object sender, EventArgs e)
{
CalculateOutputDirectory();
}
private void btnFormat_Click(object sender, EventArgs e)
{
FormatGallery();
}
}
}
MainForm.Designer.cs
namespace PhotoGalleryFormatter
{
partial class MainForm
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
this.btnLoadDir = new System.Windows.Forms.Button();
this.txtFileDir = new System.Windows.Forms.TextBox();
this.gbFileInfo = new System.Windows.Forms.GroupBox();
this.btnFormat = new System.Windows.Forms.Button();
this.lblOutputDir = new System.Windows.Forms.Label();
this.txtOutputDir = new System.Windows.Forms.TextBox();
this.lblOriginalDir = new System.Windows.Forms.Label();
this.txtGalleryName = new System.Windows.Forms.TextBox();
this.lblGalleryName = new System.Windows.Forms.Label();
this.gbFileInfo.SuspendLayout();
this.SuspendLayout();
//
// btnLoadDir
//
this.btnLoadDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnLoadDir.Location = new System.Drawing.Point(6, 188);
this.btnLoadDir.Name = "btnLoadDir";
this.btnLoadDir.Size = new System.Drawing.Size(196, 38);
this.btnLoadDir.TabIndex = 0;
this.btnLoadDir.Text = "Load Directory";
this.btnLoadDir.UseVisualStyleBackColor = true;
this.btnLoadDir.Click += new System.EventHandler(this.btnLoadDir_Click);
//
// txtFileDir
//
this.txtFileDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtFileDir.Location = new System.Drawing.Point(9, 48);
this.txtFileDir.Name = "txtFileDir";
this.txtFileDir.ReadOnly = true;
this.txtFileDir.Size = new System.Drawing.Size(409, 22);
this.txtFileDir.TabIndex = 1;
//
// gbFileInfo
//
this.gbFileInfo.Controls.Add(this.btnFormat);
this.gbFileInfo.Controls.Add(this.lblOutputDir);
this.gbFileInfo.Controls.Add(this.txtOutputDir);
this.gbFileInfo.Controls.Add(this.lblOriginalDir);
this.gbFileInfo.Controls.Add(this.txtGalleryName);
this.gbFileInfo.Controls.Add(this.lblGalleryName);
this.gbFileInfo.Controls.Add(this.txtFileDir);
this.gbFileInfo.Controls.Add(this.btnLoadDir);
this.gbFileInfo.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.gbFileInfo.Location = new System.Drawing.Point(12, 12);
this.gbFileInfo.Name = "gbFileInfo";
this.gbFileInfo.Size = new System.Drawing.Size(424, 238);
this.gbFileInfo.TabIndex = 2;
this.gbFileInfo.TabStop = false;
this.gbFileInfo.Text = "File Info";
//
// btnFormat
//
this.btnFormat.Enabled = false;
this.btnFormat.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnFormat.Location = new System.Drawing.Point(222, 188);
this.btnFormat.Name = "btnFormat";
this.btnFormat.Size = new System.Drawing.Size(196, 38);
this.btnFormat.TabIndex = 8;
this.btnFormat.Text = "Format Gallery";
this.btnFormat.UseVisualStyleBackColor = true;
this.btnFormat.Click += new System.EventHandler(this.btnFormat_Click);
//
// lblOutputDir
//
this.lblOutputDir.AutoSize = true;
this.lblOutputDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblOutputDir.Location = new System.Drawing.Point(6, 140);
this.lblOutputDir.Name = "lblOutputDir";
this.lblOutputDir.Size = new System.Drawing.Size(112, 17);
this.lblOutputDir.TabIndex = 7;
this.lblOutputDir.Text = "Output Directory";
//
// txtOutputDir
//
this.txtOutputDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtOutputDir.Location = new System.Drawing.Point(6, 160);
this.txtOutputDir.Name = "txtOutputDir";
this.txtOutputDir.ReadOnly = true;
this.txtOutputDir.Size = new System.Drawing.Size(412, 22);
this.txtOutputDir.TabIndex = 6;
//
// lblOriginalDir
//
this.lblOriginalDir.AutoSize = true;
this.lblOriginalDir.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblOriginalDir.Location = new System.Drawing.Point(6, 28);
this.lblOriginalDir.Name = "lblOriginalDir";
this.lblOriginalDir.Size = new System.Drawing.Size(118, 17);
this.lblOriginalDir.TabIndex = 4;
this.lblOriginalDir.Text = "Original Directory";
//
// txtGalleryName
//
this.txtGalleryName.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.txtGalleryName.Location = new System.Drawing.Point(9, 105);
this.txtGalleryName.Name = "txtGalleryName";
this.txtGalleryName.Size = new System.Drawing.Size(409, 22);
this.txtGalleryName.TabIndex = 3;
this.txtGalleryName.TextChanged += new System.EventHandler(this.txtGalleryName_TextChanged);
//
// lblGalleryName
//
this.lblGalleryName.AutoSize = true;
this.lblGalleryName.Font = new System.Drawing.Font("Microsoft Sans Serif", 7.8F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.lblGalleryName.Location = new System.Drawing.Point(6, 85);
this.lblGalleryName.Name = "lblGalleryName";
this.lblGalleryName.Size = new System.Drawing.Size(94, 17);
this.lblGalleryName.TabIndex = 2;
this.lblGalleryName.Text = "Gallery Name";
//
// MainForm
//
this.AutoScaleDimensions = new System.Drawing.SizeF(8F, 16F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(234)))), ((int)(((byte)(234)))), ((int)(((byte)(234)))));
this.ClientSize = new System.Drawing.Size(447, 262);
this.Controls.Add(this.gbFileInfo);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "MainForm";
this.Text = "Photo Gallery Formatter";
this.gbFileInfo.ResumeLayout(false);
this.gbFileInfo.PerformLayout();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.Button btnLoadDir;
private System.Windows.Forms.TextBox txtFileDir;
private System.Windows.Forms.GroupBox gbFileInfo;
private System.Windows.Forms.Button btnFormat;
private System.Windows.Forms.Label lblOutputDir;
private System.Windows.Forms.TextBox txtOutputDir;
private System.Windows.Forms.Label lblOriginalDir;
private System.Windows.Forms.TextBox txtGalleryName;
private System.Windows.Forms.Label lblGalleryName;
}
}