Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Drawing.Drawing2D;
- using System.Drawing.Imaging;
- using System.Drawing;
- using System.IO;
- using System.Windows.Forms;
- // Credits to: csaki
- // Exe download: https://hostr.co/eMXIFkX16sdR
- namespace imageProcessor
- {
- class Program
- {
- static string folder;
- static string[] filenames;
- static string path;
- static DateTime starttime;
- static DateTime endtime;
- static DateTime creationTime;
- static string filename;
- [STAThread()]
- static void Main()
- {
- Start:
- OpenFileDialog dialog = new OpenFileDialog();
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Welcome to my simple image resizer!");
- Console.WriteLine("Select all the .jpg/.png images you want to resize.");
- Console.WriteLine("(...and let the magic happen!)\n");
- Console.WriteLine("Currently jpg/png generating quality is set to 30 percent (optimal)");
- Console.WriteLine("Resized pictures are kept in a folder named \"Resized\".\n");
- Console.ForegroundColor = ConsoleColor.White;
- dialog.Multiselect = true;
- dialog.Title = "Select images";
- dialog.InitialDirectory = @"C:\";
- dialog.Filter = "Images (*.jpg, *.jpeg *.png) | *.JPG; *.jpg; *.JPEG; *.jpeg; *.PNG; *.png";
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- starttime = DateTime.Now;
- folder = Path.GetDirectoryName(dialog.FileNames[0]);
- Log("Selected folder: " + folder);
- filenames = dialog.FileNames;
- Log("Got " + filenames.Length + " files!");
- Log("Starting image processing...");
- for (int i = 0; i < filenames.Length; i++)
- {
- try
- {
- using (Bitmap img = new Bitmap(filenames[i]))
- {
- creationTime = File.GetCreationTime(filenames[i]);
- filename = Path.GetFileName(filenames[i]);
- FolderExists("Resized\\" + String.Format("{0:yyyy_MM}", creationTime));
- path = Path.Combine(folder, "Resized", String.Format("{0:yyyy_MM}",
- creationTime), filename);
- SaveImage(path, img, 30);
- Log("Resized: " + filename + " (" + (filenames.Length - i - 1) + " left)");
- }
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Log("Ooops! Something happened!");
- if (MessageBox.Show("Exception message:\n\n" + ex.Message + "\n\nDo you want to restart from the beginning?", "Error!", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
- {
- goto Start;
- }
- else Environment.Exit(-1);
- }
- }
- Log("All of the images were successfully processed!");
- }
- else
- {
- if (MessageBox.Show("You haven't selected any files! Do you want to do so again?\n"
- + "Press yes for restart, or no for exit.", "Error", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
- {
- goto Start;
- }
- else Environment.Exit(0);
- }
- endtime = DateTime.Now;
- Log("Passed time: " + String.Format("{0} seconds\n", Math.Round((endtime - starttime).TotalSeconds, 2)));
- Log("Perhaps you want to resize more images? Y/N");
- Retry:
- ConsoleKeyInfo decision = Console.ReadKey();
- switch (decision.Key)
- {
- case ConsoleKey.Y:
- goto Start;
- case ConsoleKey.N:
- Environment.Exit(0);
- break;
- default:
- Log("Press Y or N!");
- goto Retry;
- }
- }
- // Main ends
- //static void RenameFolders()
- //{
- // string[] folders = Directory.GetDirectories(Path.Combine(folder, "Resized"));
- // List<string> foldersForModify = new List<string>();
- // for (int i = 0; i < folders.Length; i++)
- // {
- // if (folders[i].Substring(folders[i].LastIndexOf('\\') + 1).Contains(' '))
- // {
- // foldersForModify.Add(folders[i]);
- // }
- // }
- // if (foldersForModify.Count == 0) return;
- // string[] renamed = new string[foldersForModify.Count];
- // string[] actualFoldernames = new string[renamed.Length];
- // char[] chars = null;
- // for (int i = 0; i < actualFoldernames.Length; i++)
- // {
- // actualFoldernames[i] = folders[i].Substring(folders[i].LastIndexOf('\\') + 1);
- // chars = actualFoldernames[i].ToCharArray();
- // for (int j = 0; j < chars.Length; j++)
- // {
- // if (chars[j] == ' ') chars[j] = '_';
- // }
- // actualFoldernames[i] = new string(chars);
- // renamed[i] = folders[i].Substring(0, folders[i].LastIndexOf('\\') + 1) + actualFoldernames[i];
- // }
- // for (int i = 0; i < renamed.Length; i++)
- // {
- // if (folders[i] != renamed[i]) Directory.Move(folders[i], renamed[i]);
- // }
- //}
- static void Log(string text)
- {
- Console.WriteLine("[{0:HH:MM:ss.ff}] {1}", DateTime.Now, text);
- }
- static void FolderExists(string f)
- {
- string path = Path.Combine(folder, f);
- if (Directory.Exists(path))
- {
- return;
- }
- else
- {
- Log("Creating path: " + path);
- Directory.CreateDirectory(path);
- }
- }
- static void SaveImage(string path, Image image, int quality)
- {
- if ((quality < 0) || (quality > 100))
- throw new ArgumentOutOfRangeException("Value must be between 0 and 100.");
- EncoderParameter qualityParam = new EncoderParameter(Encoder.Quality, quality);
- ImageCodecInfo imgCodec;
- if (path.Substring(path.LastIndexOf('.') + 1).ToLower() == "jpg" ||
- path.Substring(path.LastIndexOf('.') + 1).ToLower() == "jpeg")
- imgCodec = GetEncoderInfo("image/jpeg");
- else imgCodec = GetEncoderInfo("image/png");
- EncoderParameters encoderParams = new EncoderParameters(1);
- encoderParams.Param[0] = qualityParam;
- image.Save(path, imgCodec, encoderParams);
- }
- static ImageCodecInfo GetEncoderInfo(string mimeType)
- {
- string lookupKey = mimeType.ToLower();
- ImageCodecInfo foundCodec = null;
- if (Encoders.ContainsKey(lookupKey))
- {
- foundCodec = Encoders[lookupKey];
- }
- return foundCodec;
- }
- static Dictionary<string, ImageCodecInfo> encoders = null;
- static Dictionary<string, ImageCodecInfo> Encoders
- {
- get
- {
- if (encoders == null)
- {
- encoders = new Dictionary<string, ImageCodecInfo>();
- }
- if (encoders.Count == 0)
- {
- foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
- {
- encoders.Add(codec.MimeType.ToLower(), codec);
- }
- }
- return encoders;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment