Advertisement
csaki

Image Resizer (HUN, console) [Final]

Oct 29th, 2013
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.77 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Drawing.Drawing2D;
  5. using System.Drawing.Imaging;
  6. using System.Drawing;
  7. using System.IO;
  8. using System.Windows.Forms;
  9.  
  10. // Credits csaki-nak
  11. // Exe letöltés: https://hostr.co/vF7obWIM7RRm
  12. namespace imageProcesser
  13. {
  14.     class Program
  15.     {
  16.         static string mappa;
  17.         static string[] fajlnevek;
  18.         static string utvonal;
  19.         static DateTime startido;
  20.         static DateTime endido;
  21.         static DateTime keltdatum;
  22.         static string fajlnev;
  23.  
  24.         [STAThread()]
  25.         static void Main()
  26.         {
  27.         Start:
  28.             OpenFileDialog dialog = new OpenFileDialog();
  29.             Console.Clear();
  30.             Console.ForegroundColor = ConsoleColor.Yellow;
  31.             Console.WriteLine("Üdv az én kis egyszerű képkicsinyítőmben!");
  32.             Console.WriteLine("Válaszd ki az összes .jpg/.png képet, amit le akarsz kicsinyíteni.");
  33.             Console.WriteLine("(...és figyeld ahogy a varázslat történik!)\n");
  34.             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.");
  35.             Console.ForegroundColor = ConsoleColor.White;
  36.  
  37.             dialog.Multiselect = true;
  38.             dialog.Title = "Válassz képeket!";
  39.             dialog.InitialDirectory = @"C:\";
  40.             dialog.Filter = "Képfájlok (*.jpg, *.jpeg, *.png) | *.JPG; *.jpg; *.JPEG; *.jpeg; *.PNG; *.png";
  41.             if (dialog.ShowDialog() == DialogResult.OK)
  42.             {
  43.                 startido = DateTime.Now;
  44.                 mappa = Path.GetDirectoryName(dialog.FileNames[0]);
  45.                 Log("Választott mappa: " + mappa);
  46.                 fajlnevek = dialog.FileNames;
  47.                 Log(fajlnevek.Length + " fájl kiválasztva!");
  48.                 Log("A feldolgozás elkezdődött...");
  49.                 for (int i = 0; i < fajlnevek.Length; i++)
  50.                 {
  51.                     try
  52.                     {
  53.                         using (Bitmap img = new Bitmap(fajlnevek[i]))
  54.                         {
  55.                             keltdatum = File.GetCreationTime(fajlnevek[i]);
  56.                             fajlnev = Path.GetFileName(fajlnevek[i]);
  57.                             MappaLetezik("Kicsi\\" + String.Format("{0:yyyy_MM}", keltdatum));
  58.                             utvonal = Path.Combine(mappa, "Kicsi", String.Format("{0:yyyy_MM}",
  59.                                                                                               keltdatum), fajlnev);
  60.                            
  61.                             KepMentes(utvonal, img, 30);
  62.                             Log("Átméretezve: " + fajlnev + " (" + (fajlnevek.Length - i) + " van hátra)");
  63.                         }
  64.                     }
  65.                     catch (Exception ex)
  66.                     {
  67.                         Console.ForegroundColor = ConsoleColor.Red;
  68.                         Log("Hoppá! Valami történt!");
  69.                         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)
  70.                         {
  71.                             goto Start;
  72.                         }
  73.                         else Environment.Exit(-1);
  74.                     }
  75.                 }
  76.                 Log("Minden kép sikeresen fel van dolgozva!");
  77.             }
  78.             else
  79.             {
  80.                 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)
  81.                 {
  82.                     goto Start;
  83.                 }
  84.                 else Environment.Exit(0);
  85.             }
  86.  
  87.             endido = DateTime.Now;
  88.             Log("Ennyi idő telt el a feldolgozás óta: " + String.Format("{0} seconds\n", (endido - startido).TotalSeconds));
  89.             Log("Esetleg szeretnél még képeket feldolgozni? Y/N (Y - igen, N - nem)");
  90.         Retry:
  91.             ConsoleKeyInfo decision = Console.ReadKey();
  92.             switch (decision.Key)
  93.             {
  94.                 case ConsoleKey.Y:
  95.                     goto Start;
  96.  
  97.                 case ConsoleKey.N:
  98.                     Environment.Exit(0);
  99.                     break;
  100.                 default:
  101.                     Log("Y-t vagy N-t nyomj!!");
  102.                     goto Retry;
  103.  
  104.             }
  105.         }
  106.         // Main ends
  107.  
  108.  
  109.         static void Log(string text)
  110.         {
  111.             Console.WriteLine("[{0:HH:MM:ss.ff}] {1}", DateTime.Now, text);
  112.         }
  113.  
  114.         static void MappaLetezik(string f)
  115.         {
  116.             string path = Path.Combine(mappa, f);
  117.             if (Directory.Exists(path))
  118.             {
  119.                 return;
  120.             }
  121.             else
  122.             {
  123.                 Log("Útvonal létrehozása: " + path);
  124.                 Directory.CreateDirectory(path);
  125.             }
  126.         }
  127.  
  128.         static void KepMentes(string utvonal, Image kep, int minoseg)
  129.         {
  130.             if ((minoseg < 0) || (minoseg > 100))
  131.             {
  132.                 throw new ArgumentOutOfRangeException("0 és 100 között kell az értéknek lennie!");
  133.             }
  134.  
  135.             EncoderParameter minosegParam = new EncoderParameter(Encoder.Quality, minoseg);
  136.             ImageCodecInfo imgCodec;
  137.             if (utvonal.Substring(utvonal.LastIndexOf('.') + 1).ToLower() == "jpg" ||
  138.                 utvonal.Substring(utvonal.LastIndexOf('.') + 1).ToLower() == "jpeg")
  139.             imgCodec = EncoderInfoLekerdezes("image/jpeg");
  140.             else imgCodec = EncoderInfoLekerdezes("image/png");
  141.  
  142.             EncoderParameters encoderParams = new EncoderParameters(1);
  143.             encoderParams.Param[0] = minosegParam;
  144.  
  145.             kep.Save(utvonal, imgCodec, encoderParams);
  146.         }
  147.  
  148.         static ImageCodecInfo EncoderInfoLekerdezes(string mimeTipus)
  149.         {
  150.             mimeTipus = mimeTipus.ToLower();
  151.             ImageCodecInfo codec = null;
  152.             if (Encoders.ContainsKey(mimeTipus))
  153.             {
  154.                 codec = Encoders[mimeTipus];
  155.             }
  156.             return codec;
  157.         }
  158.  
  159.         static Dictionary<string, ImageCodecInfo> encoders = null;
  160.  
  161.         static Dictionary<string, ImageCodecInfo> Encoders
  162.         {
  163.             get
  164.             {
  165.                 if (encoders == null)
  166.                 {
  167.                     encoders = new Dictionary<string, ImageCodecInfo>();
  168.                 }
  169.                 if (encoders.Count == 0)
  170.                 {
  171.                     foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
  172.                     {
  173.                         encoders.Add(codec.MimeType.ToLower(), codec);
  174.                     }
  175.                 }
  176.                 return encoders;
  177.             }
  178.         }
  179.     }
  180. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement