Advertisement
Atheuz

Untitled

May 5th, 2012
334
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Drawing;
  7. using System.Drawing.Imaging;
  8. using System.Drawing.Text;
  9.  
  10. namespace ThumbnailDirectory
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             if (args != null)
  17.             {
  18.                 List<string> extensions = new List<string> { ".jpg", ".gif" };
  19.                 string path = Path.GetFullPath(args[0]).ToString();
  20.                 List<string> files = Directory.GetFiles(path).ToList().Where(x => extensions.Contains(Path.GetExtension(x))).Select(x => x).ToList();
  21.  
  22.                 Image myImg;
  23.                 string fn;
  24.                 string output;
  25.                 foreach (string f in files)
  26.                 {
  27.                     fn = Path.GetFileNameWithoutExtension(f);
  28.                     Console.Write("\r{0} out of {1} processed.", (files.IndexOf(f) + 1).ToString("00000"), files.Count().ToString("00000"));
  29.                     //Console.WriteLine("{0} -> {1}", f, fn + "-thumbnail" + ".jpg");
  30.                     myImg = Image.FromFile(Path.Combine(path, f));
  31.                     Size thumbnailSize = GetThumbnailSize(myImg);
  32.                     Image thumbnail = myImg.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
  33.                     Directory.CreateDirectory(Path.Combine(path, "OutputTesting").ToString());
  34.                     output = Path.Combine(Path.Combine(path, "OutputTesting", fn + "-thumbnail" + ".jpg")).ToString();
  35.                     thumbnail.Save(output);
  36.                 }
  37.             }
  38.         }
  39.  
  40.         static Size GetThumbnailSize(Image original)
  41.         {
  42.             // Maximum size of any dimension.
  43.             const int maxPixels = 128;
  44.  
  45.             // Width and height.
  46.             int originalWidth = original.Width;
  47.             int originalHeight = original.Height;
  48.  
  49.             // Compute best factor to scale entire image based on larger dimension.
  50.             double factor;
  51.             if (originalWidth > originalHeight)
  52.             {
  53.                 factor = (double)maxPixels / originalWidth;
  54.             }
  55.             else
  56.             {
  57.                 factor = (double)maxPixels / originalHeight;
  58.             }
  59.  
  60.             // Return thumbnail size.
  61.             return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
  62.         }
  63.  
  64.         private ImageCodecInfo GetEncoder(ImageFormat format)
  65.         {
  66.  
  67.             ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
  68.  
  69.             foreach (ImageCodecInfo codec in codecs)
  70.             {
  71.                 if (codec.FormatID == format.Guid)
  72.                 {
  73.                     return codec;
  74.                 }
  75.             }
  76.             return null;
  77.         }
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement