No error handling.  ;-)

You pass the file to the resizeFile() method, however that method never
checks if it is a valid image file.  What if there is one of those thumbs.db
files in the directory that I see all over image directories?

Anyway, I think you are trying to skin a cat that has been skinned a million
times.

Go to http://www.imagemagick.org/.  This app has been around for ages and is
really great.  You can script it or just use a command prompt or use it in a
program.

Read the section at the link above named "Command-line Tools".  It lists all
the command line tools that come with it and all the options.  I put this in
my path on windows and can just open a command prompt and do all kinds of
good stuff.  It runs on Linux, Mac, *nix and Windows.  It is Open and Free.

The program you should read up first is "convert" or convert.exe on
Windows.  I have used it tons of times to just process all the jpg files I
get off my camera.

convert takes a -resize option and will preserve the aspect ratio.

So you can just do:

convert image1.jpg -resize '1024' image1.jpg

This will take your image and make the width 1024 and the height will be
scaled properly.  You can also use -resize '1024x110' or something if you
want a weird image.

Jim

On Fri, Sep 11, 2009 at 6:57 PM, Processor Devil
<[email protected]>wrote:

> 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