Advertisement
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 csaki-nak
- // Exe letöltés: https://hostr.co/vF7obWIM7RRm
- namespace imageProcesser
- {
- class Program
- {
- static string mappa;
- static string[] fajlnevek;
- static string utvonal;
- static DateTime startido;
- static DateTime endido;
- static DateTime keltdatum;
- static string fajlnev;
- [STAThread()]
- static void Main()
- {
- Start:
- OpenFileDialog dialog = new OpenFileDialog();
- Console.Clear();
- Console.ForegroundColor = ConsoleColor.Yellow;
- Console.WriteLine("Üdv az én kis egyszerű képkicsinyítőmben!");
- Console.WriteLine("Válaszd ki az összes .jpg/.png képet, amit le akarsz kicsinyíteni.");
- Console.WriteLine("(...és figyeld ahogy a varázslat történik!)\n");
- Console.WriteLine("Jelenlegi .jpg/.png minőség 30 százalékra van állítva (optimális)\nA képek a \"Kicsi\" mappában tárolódnak.");
- Console.ForegroundColor = ConsoleColor.White;
- dialog.Multiselect = true;
- dialog.Title = "Válassz képeket!";
- dialog.InitialDirectory = @"C:\";
- dialog.Filter = "Képfájlok (*.jpg, *.jpeg, *.png) | *.JPG; *.jpg; *.JPEG; *.jpeg; *.PNG; *.png";
- if (dialog.ShowDialog() == DialogResult.OK)
- {
- startido = DateTime.Now;
- mappa = Path.GetDirectoryName(dialog.FileNames[0]);
- Log("Választott mappa: " + mappa);
- fajlnevek = dialog.FileNames;
- Log(fajlnevek.Length + " fájl kiválasztva!");
- Log("A feldolgozás elkezdődött...");
- for (int i = 0; i < fajlnevek.Length; i++)
- {
- try
- {
- using (Bitmap img = new Bitmap(fajlnevek[i]))
- {
- keltdatum = File.GetCreationTime(fajlnevek[i]);
- fajlnev = Path.GetFileName(fajlnevek[i]);
- MappaLetezik("Kicsi\\" + String.Format("{0:yyyy_MM}", keltdatum));
- utvonal = Path.Combine(mappa, "Kicsi", String.Format("{0:yyyy_MM}",
- keltdatum), fajlnev);
- KepMentes(utvonal, img, 30);
- Log("Átméretezve: " + fajlnev + " (" + (fajlnevek.Length - i) + " van hátra)");
- }
- }
- catch (Exception ex)
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Log("Hoppá! Valami történt!");
- if (MessageBox.Show("Exception üzenet:\n\n" + ex.Message + "\n\nSzeretnéd előről kezdeni?", "Hiba!", MessageBoxButtons.YesNo, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
- {
- goto Start;
- }
- else Environment.Exit(-1);
- }
- }
- Log("Minden kép sikeresen fel van dolgozva!");
- }
- else
- {
- if (MessageBox.Show("Nem választottál egy fájlt sem! Szeretnéd újrakezdeni? Ha nem, a program kilép.","Hiba", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1) == DialogResult.Yes)
- {
- goto Start;
- }
- else Environment.Exit(0);
- }
- endido = DateTime.Now;
- Log("Ennyi idő telt el a feldolgozás óta: " + String.Format("{0} seconds\n", (endido - startido).TotalSeconds));
- Log("Esetleg szeretnél még képeket feldolgozni? Y/N (Y - igen, N - nem)");
- Retry:
- ConsoleKeyInfo decision = Console.ReadKey();
- switch (decision.Key)
- {
- case ConsoleKey.Y:
- goto Start;
- case ConsoleKey.N:
- Environment.Exit(0);
- break;
- default:
- Log("Y-t vagy N-t nyomj!!");
- goto Retry;
- }
- }
- // Main ends
- static void Log(string text)
- {
- Console.WriteLine("[{0:HH:MM:ss.ff}] {1}", DateTime.Now, text);
- }
- static void MappaLetezik(string f)
- {
- string path = Path.Combine(mappa, f);
- if (Directory.Exists(path))
- {
- return;
- }
- else
- {
- Log("Útvonal létrehozása: " + path);
- Directory.CreateDirectory(path);
- }
- }
- static void KepMentes(string utvonal, Image kep, int minoseg)
- {
- if ((minoseg < 0) || (minoseg > 100))
- {
- throw new ArgumentOutOfRangeException("0 és 100 között kell az értéknek lennie!");
- }
- EncoderParameter minosegParam = new EncoderParameter(Encoder.Quality, minoseg);
- ImageCodecInfo imgCodec;
- if (utvonal.Substring(utvonal.LastIndexOf('.') + 1).ToLower() == "jpg" ||
- utvonal.Substring(utvonal.LastIndexOf('.') + 1).ToLower() == "jpeg")
- imgCodec = EncoderInfoLekerdezes("image/jpeg");
- else imgCodec = EncoderInfoLekerdezes("image/png");
- EncoderParameters encoderParams = new EncoderParameters(1);
- encoderParams.Param[0] = minosegParam;
- kep.Save(utvonal, imgCodec, encoderParams);
- }
- static ImageCodecInfo EncoderInfoLekerdezes(string mimeTipus)
- {
- mimeTipus = mimeTipus.ToLower();
- ImageCodecInfo codec = null;
- if (Encoders.ContainsKey(mimeTipus))
- {
- codec = Encoders[mimeTipus];
- }
- return codec;
- }
- 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
Advertisement