Hi man, thanks for the feedback :).Here is new version, you can specify just
the height and width will be calculated automatically :)

using System;
using System.IO;
using System.Drawing;

namespace ThumbNail
{
    class Program
    {
        static void Main(string[] args)
        {
            if(args.Length == 3)
            {
                try
                {
                    Resizer rsz = new Resizer(args[0], int.Parse(args[2]),
int.Parse(args[1]));
                    rsz.ResizeImages();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else if (args.Length == 2)
            {
                try
                {
                    Resizer rsz = new Resizer(args[0], int.Parse(args[1]));
                    rsz.ResizeImages();
                }
                catch (Exception e)
                {
                    Console.WriteLine(e.Message);
                }
            }
            else
                Console.WriteLine("Usage:\n ThumbNail <directory>
[target_width] <target_height>");

        }
    }
    class Resizer
    {
        public Resizer(string directory, int height, int width)
        {
            this.directory = directory;
            this.height = height;
            this.width = width;
            this.proportions = false;
        }
        public Resizer(string directory, int height)
        {
            this.directory = directory;
            this.height = height;
            this.proportions = true;
        }
        public void ResizeImages()
        {
            if (Directory.Exists(this.directory))
            {
                this.newDir = Path.Combine(this.directory, "thumbnails");
                string[] files = Directory.GetFiles(this.directory);
                if (!Directory.Exists(newDir))
                    Directory.CreateDirectory(newDir);
                foreach (string file in files)
                {
                    this.resizeFile(file);
                }
            }
            else
            {
                Console.WriteLine("Specified directory doesn't exist");
            }
        }
        void calculateWidth(int oldHeight, int oldWidth, int newHeight)
        {
            int percentage = oldHeight / newHeight;
            int newWidth = oldWidth / percentage;
            this.width = newWidth;
        }
        bool problem()
        {
            Console.WriteLine("Problem with creating a thumbnail");
            return false;
        }
        void resizeFile(string file)
        {
            Image img = Image.FromFile(file);
            if (this.proportions == true)
                this.calculateWidth(img.Height, img.Width, this.height);
            Image resized = img.GetThumbnailImage(this.width, this.height,
                new Image.GetThumbnailImageAbort(problem), IntPtr.Zero);
            string newName = string.Format("{0}_t{1}",
                Path.GetFileNameWithoutExtension(file),
                Path.GetExtension(file));
            resized.Save(Path.Combine(this.newDir, newName));
            Console.WriteLine("{0} --> {1}", Path.GetFileName(file),
newName);

        }

        string directory;
        int height;
        int width;
        bool proportions;
        string newDir;
    }
}


2009/9/11 Vitaly Maslevskiy <[email protected]>

>
> Problem of this prog,
>
> 1. it doesn't recognize portrait / landscape
> 2. if params height and width not proportional to original image
> thumbnails will look ugly :)
>
> vitaly
>
>
> 2009/9/10 Processor-Dev1l <[email protected]>:
> >
> > Hi guys. I am just doing a business web page for my friends. Problem
> > is they want about 200 pictures into galery (web page is html only)
> > and one pic has about 5 MB.
> > Because I didn't want to resize each picture I made this simple
> > console application, I hope you will find it useful :)
> >
> > using System;
> > using System.IO;
> > using System.Drawing;
> >
> > namespace ThumbNail
> > {
> >    class Program
> >    {
> >        static void Main(string[] args)
> >        {
> >            if (args.Length != 3)
> >                Console.WriteLine("Usage:\n ThumbNail <directory>
> > <target_width> <target_height>");
> >            else
> >            {
> >                try
> >                {
> >                    Resizer rsz = new Resizer(args[0], int.Parse(args
> > [2]), int.Parse(args[1]));
> >                    rsz.ResizeImages();
> >                }
> >                catch (Exception e)
> >                {
> >                    Console.WriteLine(e.Message);
> >                }
> >            }
> >        }
> >    }
> >    class Resizer
> >    {
> >        public Resizer(string directory, int height, int width)
> >        {
> >            this.directory = directory;
> >            this.height = height;
> >            this.width = width;
> >        }
> >        public void ResizeImages()
> >        {
> >            string[] files = Directory.GetFiles(this.directory);
> >            string newDir = Path.Combine(this.directory,
> > "thumbnails");
> >            if (!Directory.Exists(newDir))
> >                Directory.CreateDirectory(newDir);
> >            foreach (string file in files)
> >            {
> >                Image img = Image.FromFile(file);
> >                Image resized = img.GetThumbnailImage(this.width,
> > this.height,
> >                    new Image.GetThumbnailImageAbort(problem),
> > IntPtr.Zero);
> >                string newName = string.Format("{0}_t{1}",
> >                    Path.GetFileNameWithoutExtension(file),
> >                    Path.GetExtension(file));
> >                resized.Save(Path.Combine(newDir, newName));
> >                Console.WriteLine("{0} --> {1}", Path.GetFileName
> > (file), newName);
> >            }
> >        }
> >        bool problem()
> >        {
> >            Console.WriteLine("Problem with creating a thumbnail");
> >            return false;
> >        }
> >
> >        string directory;
> >        int height;
> >        int width;
> >    }
> > }
>

Reply via email to