/*
* Software Developed by Filip Ekberg ( Filip@SmartIT.se )
*
* For Questions regarding this software, please send me an E-mail
*
* (C) Copyright 2006
*/
#region Usings
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Text;
using System.Windows.Forms;
using System.IO;
#endregion
namespace ImageResizer
{
public partial class ImageShrinker : Form
{
#region Delegates
///
/// This is my standard delegate that i use for the Buttons
///
delegate void myDelegate();
///
/// This is the label delegate that i use to change the text
///
/// The New text that is going to be displayed
delegate void myLabelDelegate(string Text);
///
/// This delegate is used for the ProcessBar to set the Maximum value
///
/// The Maximum Value of the ProcessBar
delegate void myProcessDelegate(int lengt);
///
/// This delegate is used for updating the processbar value
///
/// The amount of points to add to the value
delegate void myProcessUpdate(int value);
#endregion
#region Members
///
/// This Worker will handle all the image processing
///
private BackgroundWorker m_worker = new BackgroundWorker();
///
/// This Bitmap is used to store the Original picture in memmory
///
private Bitmap m_currentImage;
///
/// Gives us directory info about the current processing directory
///
private DirectoryInfo m_directoryInfo;
///
/// The list of files from the DirectoruInfo
///
private FileInfo[] m_fileList;
///
/// The current Processing image
///
private int m_currentProcessedImage;
///
/// This is the final image
///
private Image m_finalProcessedImage;
///
/// The Image Processor
///
private ImageProcessor m_processor = new ImageProcessor();
///
/// Standard size to use
///
private double m_size = ImageSize.Normal;
#endregion
#region Constructor
///
/// Initialize all components
///
public ImageShrinker()
{
InitializeComponent();
panel1.Visible = false;
m_worker.DoWork += new DoWorkEventHandler(do_work);
sizeBox.SelectedItem = "Medium";
}
#endregion
#region Delegate Methods
///
/// This will Enable / Disable all the buttons / listbox when the program is working
///
private void delegate_btnControl()
{
if (this.btnCreate.InvokeRequired)
{
myDelegate md = new myDelegate(delegate_btnControl);
this.Invoke(md);
}
else
{
btnCreate.Enabled = !btnCreate.Enabled;
btnInput.Enabled = !btnInput.Enabled;
btnOutput.Enabled = !btnOutput.Enabled;
sizeBox.Enabled = !sizeBox.Enabled;
panel1.Visible = true;
}
}
///
/// This will update the Label with new Text
///
/// The New Text
private void delegate_lblControl(string Text)
{
if (this.label1.InvokeRequired)
{
myLabelDelegate md = new myLabelDelegate(delegate_lblControl);
this.Invoke(md, new object[] { Text });
}
else
label1.Text = "Current: " + Text;
}
///
/// This will set a Maximum value to the ProcessBar and reset the bars value to 0 and also set the minimum value to 0
///
/// The Lenght of the Maximum value
private void delegate_imageProcessMaximum(int Length)
{
if (this.imageProcessBar.InvokeRequired)
{
myProcessDelegate md = new myProcessDelegate(delegate_imageProcessMaximum);
this.Invoke(md, new object[] { Length });
}
else
{
imageProcessBar.Maximum = Length;
imageProcessBar.Minimum = 0;
imageProcessBar.Value = 0;
}
}
///
/// This will update the value on the ProcessBar
///
/// How many points to add to the Value property
private void delegate_imageProcessValue(int Value)
{
if (this.imageProcessBar.InvokeRequired)
{
myProcessUpdate md = new myProcessUpdate(delegate_imageProcessValue);
this.Invoke(md, new object[] { Value });
}
else
{
imageProcessBar.Value += Value;
}
}
#endregion
#region Process Image
///
/// This method processes the image and meanwhile it disables the buttons and gives you a preview of the image that is currently being processed.
///
///
///
private void do_work(Object sender, DoWorkEventArgs e)
{
// Disables the Butons
delegate_btnControl();
// Gives us information about the selected Input directory
m_directoryInfo = new DirectoryInfo(fldInput.SelectedPath);
// Select the files from the directory
m_fileList = m_directoryInfo.GetFiles("*.jpg");
// Set the ProcessBars Max value
delegate_imageProcessMaximum(m_fileList.Length);
// Set the CurrentProcessedImage to 0
m_currentProcessedImage = 0;
// As long as we have Files in our FileList, keep on Processing
foreach (FileInfo currentFile in m_fileList)
{
m_currentProcessedImage += 1;
// Create a Bitmap of the Current file in the File List
m_currentImage = new Bitmap(currentFile.FullName);
// Display a Thumbnail of it
pictureBox1.Image = m_currentImage.GetThumbnailImage(pictureBox1.Width, pictureBox1.Height, null, IntPtr.Zero);
// Update the Label with Filename and what file is currently processed
delegate_lblControl(currentFile.Name + "\n" + m_currentProcessedImage + " / " + m_fileList.Length);
// Increase the value on the ProcessBar
delegate_imageProcessValue(1);
// Process the Image
m_processor.ProcessImage = m_currentImage;
m_processor.Size = m_size;
m_finalProcessedImage = m_processor.BeginProcess();
// Save the Image to the Output folder
m_finalProcessedImage.Save(fldOutput.SelectedPath + "\\" + currentFile.Name);
// Dispose the Images
m_finalProcessedImage.Dispose();
m_currentImage.Dispose();
}
// Enable the Buttons again
delegate_btnControl();
MessageBox.Show("Congratulations!\nAll Images are now Converted", "Done");
}
#endregion
#region Event Handlers ( Clicked )
///
/// Show the Input folder Selector
///
///
///
private void btnInput_Click(object sender, EventArgs e)
{
DialogResult result = fldInput.ShowDialog();
if (result == DialogResult.OK)
inputFolder.Text = "Process Folder: " + fldInput.SelectedPath;
}
///
/// Show the Output folder Selector
///
///
///
private void btnOutput_Click(object sender, EventArgs e)
{
DialogResult result = fldOutput.ShowDialog();
if (result == DialogResult.OK)
outputFolder.Text = "Output Folder: " + fldOutput.SelectedPath;
}
///
/// Start processing the images
///
///
///
private void btnCreate_Click(object sender, EventArgs e)
{
// If we have not choosen a directory for input and output then display an error
if (fldOutput.SelectedPath != "" && fldInput.SelectedPath != "" && sizeBox.SelectedIndices.Count != 0)
{
switch (sizeBox.SelectedItem.ToString())
{
case "Big ( 45 % )": m_size = ImageSize.Big;
break;
case "Normal ( 25% )": m_size = ImageSize.Normal;
break;
case "Thumbnail ( 5 % )": m_size = ImageSize.ThumbNail;
break;
}
m_worker.RunWorkerAsync();
}
else
MessageBox.Show("Please select Input, Output Folder and a Size", "Warning");
}
///
/// Show a little help window
///
///
///
private void howToToolStripMenuItem_Click(object sender, EventArgs e)
{
MessageBox.Show("Select the Input folder where the Images you want to resize are located.\nThen select the Output folder where you want the new Resized pictures to be stored.\n\n\nSize Explenation\nThe program is used for processing images that is 2304x1728. If you want a Thumbnail it will be 100x75, if you want Medium it will be 640x480.\n\nPlease send me an email about how the images was processed on your computer with your images! Help me develope this software!\n\n\n\nIf you have any probles using this application, please contact me at Filip@SmartIT.se", "How To");
}
///
/// Show the about window
///
///
///
private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
{
AboutImageResizer air = new AboutImageResizer();
air.Show();
}
#endregion
}
}