Click here to Skip to main content
15,867,771 members
Articles / Multimedia / GDI+
Article

C# Batch Photo Resize

Rate me:
Please Sign up or sign in to vote.
4.18/5 (13 votes)
18 Feb 2008CPOL3 min read 80.1K   5.4K   68   11
One small example teaching how to create a software for resizing and editing pictures

Introduction

In this article, I will try to provide some help in creating a photo resize software using C# and its basic namespaces. The software itself will try to fit all types and sizes of images to a ‘web’ version giving the user the option to choose the folder, destination path and also the option to insert subtitles and a transparent banner. By default, the .NET Framework is essential.

photo_resize.JPG

Background

This software requires attention to multi-language support: we declare variables for all the labels and buttons titles as public strings and we change their container when the user changes the software's language. To resize the pictures, I got information from MSDN forums and C# documentation; using System.Drawing it's possible to create 'big thumbnails' from the raw images without losing the original resolution. To insert the subtitles and manage the transparent banners on the pictures, I used as my source the article Creating a Watermarked Photograph with GDI+ for .NET. The transparent banner will be inserted setting two color manipulations. On the first, we remove the back color with one that is transparent and then we change the opacity of this by applying a 5x5 matrix that contains the coordinates for the RGBA space. The pictures will be resized using a scale scanning which means that different scales need different sizes (pictures can be 640x480 or 480x640).

Using the Code

Is the file a valid picture-file? What is the main scale to use? Starting with the ReziseImage() that basically checks the size, container and scale, we set the new size and throw a ‘new-picture-size’ to the memory. We will use this new image ‘variable’ to add the banner or text (if the user wants).

This function will also call at its end the InsertBannerAndText() that will manage the image attributes to add or not to add the banner and text to the bottom of the file. We'll also check the size of the picture to know which font-size we will use. A good thing to pay attention to is that when we treat these images with Image objects, we also have to copy the camera details manually. This means that, if you don't want to lose the EXIF data, you need to use the above code to save all the picture information files like camera type, picture date, camera functions used, etc.

C#
foreach (PropertyItem e in imagem.PropertyItems) 
{ 
    imgPhoto.SetPropertyItem(e); 
}

For more information, read this.

The function Draw() will create the image to show on the Windows forms. It also does the scale check and generation of the picture. Then, throw it on the Picture Box on the main form. At the end of the code, we have buttons that search for pictures in specified folders and skip pictures that we don't want to resize.

Points of Interest

The point of interest of this software is to try to make the job of resizing pictures easier every time before we send pictures to services like Orkut, Facebook, Picasa, etc. facing problems with the big size files or slow connections of cyber cafés that cannot manage anymore the new camera types with their huge picture quality.

History

  • 1.7.1 – Web Release (24-October-2007)

Thanks

Thanks Matthis Kiechle for the German translation, software tests and bugs searches.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Brazil Brazil
Check my Resumé at www.renanrodriguesduarte.org

Comments and Discussions

 
Questionphotoshop alternative can do that too Pin
sugar213428-May-13 23:33
sugar213428-May-13 23:33 
QuestionSimple solution is here - http://aspnethelper.com/tutorial/10 Pin
arijit0072-Apr-12 0:53
arijit0072-Apr-12 0:53 
GeneralMy vote of 5 Pin
jackjiazhiqiang6-Jul-11 19:43
jackjiazhiqiang6-Jul-11 19:43 
GeneralThank You very much Renan Pin
Paul Chu31-Aug-10 15:36
Paul Chu31-Aug-10 15:36 
GeneralMy vote of 5 Pin
Rodrigo Coimbra3-Aug-10 7:04
Rodrigo Coimbra3-Aug-10 7:04 
Generalresize photo Pin
antony0929-Dec-09 23:52
antony0929-Dec-09 23:52 
GeneralThank you so much for your code. It really helped me. Pin
sangram_ctp25-Nov-09 23:41
sangram_ctp25-Nov-09 23:41 
GeneralThis is what I demand. Pin
Reaksmey Rin11-Sep-08 17:13
Reaksmey Rin11-Sep-08 17:13 
Generalnice one. Pin
Gautam Sharma2-Jul-08 17:39
Gautam Sharma2-Jul-08 17:39 
Good one but this can also be done with the free tool, like one google has, like picasa.

But a good artical.

Happy coding

QuestionWhy RotateFlip ? Pin
taichitof3-Jun-08 21:25
taichitof3-Jun-08 21:25 
AnswerRe: Why RotateFlip ? Pin
PGumbo10-Dec-08 9:25
PGumbo10-Dec-08 9:25 

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.