Click here to Skip to main content
15,881,709 members
Articles / Multimedia / GDI+
Article

Quick Snip - Lightweight image resizer

Rate me:
Please Sign up or sign in to vote.
3.78/5 (11 votes)
8 May 20052 min read 64.1K   1.1K   38   6
Quick Snip is an incredibly lightweight application to make "web sized" pictures out of large images

Sample Image - screenshot.png

Introduction

Quick Snip is an incredibly lightweight application to make "web sized" pictures out of large images. I often want to take pictures with a digital camera and just want a 600 max width picture that I can easily email to my friends. Common image software like GIMP and Photoshop have too much overhead - opening, picking settings and choosing directories over and over again is annoying. Quick Snip is accessed via the "Send to" menu and drops a JPG in the same directory with the name + "_resized.jpg". One step, no fuss, no annoyance.

Background

This program gives a very quick introduction on how to take an image and resize it. I use a more robust version of this embedded into my website to automatically resize images for gallery collections. It also touches on how to do basic console program operations like read/write to console and pass in parameters. Reading and understanding this article should hopefully be very easy, and greatly expand your programming options.

Using the code

Important Notes

If you want to create a console application from scratch, you need to manually add a “.NET reference” to System.Drawing. (References -> Add Reference -> System.drawing | OK).

In order to register a program to be used in “Send to”, you need to add a shortcut to the Send to directory. On my computer (Windows 2003), the directory is located at \Documents and Settings\User\SendTo.

Now on to the code!

When a file is selected and the “send to” command is used, each filename is passed in as a subsequent command line parameter. We can easily iterate through each of the files.

C#
foreach (string FileName in args)
{
    // quickly slap "_resized.jpg" at the end.
    string NewFileName = FileName.Substring(0, 
                         (FileName.Length -4)) + "_resized.jpg"
            
    ResizeImage(600,600, FileName, NewFileName);                
}

Next we need a piece of code to handle the image resizing:

C#
public static void ResizeImage(int MaxWidth, 
          int MaxHeight, string FileName, string NewFileName)
{
    // load up the image, figure out a "best fit" resize,
    // and then save that new image
    Bitmap OriginalBmp = 
           (System.Drawing.Bitmap)Image.FromFile(FileName).Clone();
    Size ResizedDimensions = 
         GetDimensions(MaxWidth, MaxHeight, ref OriginalBmp);
    Bitmap NewBmp = new Bitmap(OriginalBmp, ResizedDimensions);
        
    NewBmp.Save(NewFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
}

The actual algorithm to figure out the optimal size is handled by a function called GetDimensions. GetDimensions is a very simple function to return a reasonable set of dimensions for the resized image.

C#
public static Size GetDimensions(int MaxWidth, int MaxHeight, ref Bitmap Bmp)
{
    int Width;
    int Height;
    float Multiplier;

    Height = Bmp.Height;
    Width = Bmp.Width;

    // this means you want to shrink an image that is already shrunken!
    if (Height <= MaxHeight && Width <= MaxWidth)
        return new Size(Width, Height);

    // check to see if we can shrink it width first
    Multiplier = (float)((float)MaxWidth / (float)Width);
    if ((Height * Multiplier)<= MaxHeight)
    {
        Height = (int)(Height * Multiplier);
        return new Size(MaxWidth, Height);
    }

    // if we can't get our max width, then use the max height
    Multiplier = (float)MaxHeight / (float)Height;
    Width = (int)(Width * Multiplier);
    return new Size(Width, MaxHeight);
}

That's it!

Complete Code

C#
using System;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace QuickSnip
{
    class Class1
    {
        
        [STAThread]
        static void Main(string[] args)
        {
            // when you use send to it will pass each file
            // as it's own string in args
            foreach (string FileName in args)
            {
                try
                {
                    // quickly slap "_resized.jpg" at the end.
                    string NewFileName = FileName.Substring(0, 
                           (FileName.Length -4)) + "_resized.jpg";

                    // nice when you have multiple images
                    Console.WriteLine(FileName);
                    Console.WriteLine(NewFileName);

                    // call our thumbnail function
                    ResizeImage(600,600, FileName, NewFileName);
                }
                catch (System.Exception ex)
                {
                    // UH OH!!!
                    Console.Write(ex.Message);
                    Console.ReadLine();
                }
            }
        }
    
        /// <SUMMARY>
        /// This function takes a max width/height
        /// and makes a new file based on it
        /// </SUMMARY>
        /// <PARAM name="MaxWidth">Max width of the new image</PARAM>
        /// <PARAM name="MaxHeight">Max Height of the new image</PARAM>
        /// <PARAM name="FileName">Original file name</PARAM>
        /// <PARAM name="NewFileName">new file name</PARAM>
        public static void ResizeImage(int MaxWidth, int MaxHeight, 
                               string FileName, string NewFileName)
        {
            // load up the image, figure out a "best fit"
            // resize, and then save that new image
            Bitmap OriginalBmp = 
               (System.Drawing.Bitmap)Image.FromFile(FileName).Clone();
            Size ResizedDimensions = 
               GetDimensions(MaxWidth, MaxHeight, ref OriginalBmp);
            Bitmap NewBmp = new Bitmap(OriginalBmp, ResizedDimensions);
            
            NewBmp.Save(NewFileName, System.Drawing.Imaging.ImageFormat.Jpeg);
        }

        /// <SUMMARY>
        /// this function aims to give you a best fit
        /// for a resize. It assumes width is more important 
        /// then height. If an image is already smaller
        /// then max dimensions it will not resize it.
        /// </SUMMARY>
        /// <PARAM name="MaxWidth">max width of the new image</PARAM>
        /// <PARAM name="MaxHeight">max height of the new image</PARAM>
        /// <PARAM name="Bmp">BMP of the current image,
        ///              passing by ref so fast</PARAM>
        /// <RETURNS></RETURNS>
        public static Size GetDimensions(int MaxWidth, 
                           int MaxHeight, ref Bitmap Bmp)
        {
            int Width;
            int Height;
            float Multiplier;

            Height = Bmp.Height;
            Width = Bmp.Width;

            // this means you want to shrink
            // an image that is already shrunken!
            if (Height <= MaxHeight && Width <= MaxWidth)
                return new Size(Width, Height);

            // check to see if we can shrink it width first
            Multiplier = (float)((float)MaxWidth / (float)Width);
            if ((Height * Multiplier)<= MaxHeight)
            {
                Height = (int)(Height * Multiplier);
                return new Size(MaxWidth, Height);
            }

            // if we can't get our max width, then use the max height
            Multiplier = (float)MaxHeight / (float)Height;
            Width = (int)(Width * Multiplier);
            return new Size(Width, MaxHeight);
        }
    }
}

Points of Interest

I have a number of programs I have written that end up doing image resizing and minor image manipulation. My personal website uses a simple directory scanner to look for new collections of images. When it finds them it automatically creates thumbnails, and web sized versions. I have another program that finds images on my computer and on a timer randomly creates a copy, re-sizes and re-sets my wallpaper. Overall I have found minor image manipulation easy to write with powerful results.

I hope people find my first article useful, and I will begin to post some of my more interesting code.

History

No history yet ;)

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
United States United States
I am a witty computer programmer that lives in Cambridge, Ma. I am a Microsoft fan, and interned there summer of 2002 for search.msn.com. There is an Aaron J Sherman in the area that is a well known and respected unix programmer, we are not the same person; I freaking hate perl. I have a masters in CS from RPI. I did my graduate research on the effects of file sharing. In my personal time I brew beer, play ice hockey, and shoot darts.

You can view my website which has no goal other then to display my antics.
http://boston.sleezysherm.com

Comments and Discussions

 
QuestionNo metadata Pin
beevasya17-May-16 9:23
beevasya17-May-16 9:23 
QuestionHow to resize image Pin
ranu_hai10-Nov-06 21:10
ranu_hai10-Nov-06 21:10 
AnswerRe: How to resize image Pin
Aaron Sherman12-Nov-06 17:12
Aaron Sherman12-Nov-06 17:12 
GeneralRe: How to resize image [modified] Pin
ranu_hai12-Nov-06 18:09
ranu_hai12-Nov-06 18:09 
GeneralI made the asp.net version Pin
Britney S. Morales8-Sep-06 5:36
Britney S. Morales8-Sep-06 5:36 
GeneralPerfect Pin
Intellex8-Nov-05 11:50
Intellex8-Nov-05 11:50 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.