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.Text;
- using System.IO;
- using System.Drawing;
- using System.Drawing.Imaging;
- using System.Drawing.Text;
- namespace ThumbnailDirectory
- {
- class Program
- {
- static void Main(string[] args)
- {
- if (args != null)
- {
- List<string> extensions = new List<string> { ".jpg", ".gif" };
- string path = Path.GetFullPath(args[0]).ToString();
- List<string> files = Directory.GetFiles(path).ToList().Where(x => extensions.Contains(Path.GetExtension(x))).Select(x => x).ToList();
- Image myImg;
- string fn;
- string output;
- foreach (string f in files)
- {
- fn = Path.GetFileNameWithoutExtension(f);
- Console.Write("\r{0} out of {1} processed.", (files.IndexOf(f) + 1).ToString("00000"), files.Count().ToString("00000"));
- //Console.WriteLine("{0} -> {1}", f, fn + "-thumbnail" + ".jpg");
- myImg = Image.FromFile(Path.Combine(path, f));
- Size thumbnailSize = GetThumbnailSize(myImg);
- Image thumbnail = myImg.GetThumbnailImage(thumbnailSize.Width, thumbnailSize.Height, null, IntPtr.Zero);
- Directory.CreateDirectory(Path.Combine(path, "OutputTesting").ToString());
- output = Path.Combine(Path.Combine(path, "OutputTesting", fn + "-thumbnail" + ".jpg")).ToString();
- thumbnail.Save(output);
- }
- }
- }
- static Size GetThumbnailSize(Image original)
- {
- // Maximum size of any dimension.
- const int maxPixels = 128;
- // Width and height.
- int originalWidth = original.Width;
- int originalHeight = original.Height;
- // Compute best factor to scale entire image based on larger dimension.
- double factor;
- if (originalWidth > originalHeight)
- {
- factor = (double)maxPixels / originalWidth;
- }
- else
- {
- factor = (double)maxPixels / originalHeight;
- }
- // Return thumbnail size.
- return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
- }
- private ImageCodecInfo GetEncoder(ImageFormat format)
- {
- ImageCodecInfo[] codecs = ImageCodecInfo.GetImageDecoders();
- foreach (ImageCodecInfo codec in codecs)
- {
- if (codec.FormatID == format.Guid)
- {
- return codec;
- }
- }
- return null;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement