Click here to Skip to main content
15,885,943 members
Articles / Programming Languages / C#
Article

Batch Image

Rate me:
Please Sign up or sign in to vote.
4.34/5 (21 votes)
4 Apr 2007CPOL3 min read 84.3K   4K   86   16
An application to make several images processing elaboration in batch for creating thumbnails in a few minutes
Sample screenshot

Introduction

This application is useful to generate thumbnails from images in a few minutes with some interesting effects and features.

You can also insert text over images in semitransparence, defining its position, size, shadow, and optionally with a background shape like a rectangle or an ellipse and a 3D effect. Moreover you can impress a watermark image on the target image defining the transparency key, the size in percent respect of the target image, the transparency level, and the position. You can convert images between formats. Finally, you can make an interesting shadow effect like a picture over a sheet. All the elaborations could be done in batch.

Several formats are supported: JPG (with customizable compression level), GIF, PNG, WMF, TIFF, EXIT and ICO.

The tool has a nice look and a user friendly interface.

Background

There are a lot of applications or code samples that make a resize of an image with GDI+.
The basic idea for resizing pictures is well explained in the article Resizing a Photographic image with GDI+ for .NET.

The basic idea for the watermark is well explained in the article Creating a Watermarked Photograph with GDI+ for.NET.

The implementation for resizing and impressing watermark is not much different from the quoted articles, but with some small differences.

TreeView

Image 2

The user can navigate files and folders for choosing desired images using the look appeal treeview on the right side of the application window. With a simple double click on a file, an image can be added to panel, otherwise with a left click a context menu appears and makes possible to the user to add or remove a single image or an entire directory containing several images. The presence of the same image on the work panel is checked. When a file or a directory is selected on the treeview, its properties like size, name, etc. are shown on the property grid. If the selected file is an image, a preview is visible just below the navigation tree.

Using the Code

The project is composed of several classes. The most important ones are:

  • UImageContainer - Contains the treeview, the workpanel and the preview for files and images
  • UDirTree - Provides the folder and files navigation
  • Panello - Is the workpanel that contains several UImage controls
  • Uimage - The user control that displays an image on the work panel
  • CImage - Provides the main image processing like resize, watermark, shadow effect, JPG saving, etc.

The follow code lines create the shadow effect:

C#
public void makeshadow(ref Bitmap img, Color backcolor) 
{
 int imgWidth = img.Width;
 int imgHeight = img.Height;
 // Create a new bitmap with the same size of the original image 
Bitmap workingimg = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb); 
workingimg.SetResolution(img.HorizontalResolution, img.VerticalResolution); 
// Create a new graphic object from the new bitmap  
Graphics imgGraphic = Graphics.FromImage(workingimg); 
// Set the interpolation mode imgGraphic.InterpolationMode = intmode; 
// Calculate the new size for drawing the originalimage 
// that is reduced to maintain the total target image size 
int width = (int)((float)img.Width * 94 / 100); 
int height = (int)((float)img.Height * 94 / 100); 
// Create a new brush semitransparent 
SolidBrush BrushShadow = new SolidBrush(Color.FromArgb(10, 0, 0, 0)); 
// Set the background color based on the user preference 
imgGraphic.Clear(backcolor); 
// Create the shadow shifting semi-transparent rectangle over the empty bitmap 
float steps = ((float)imgWidth - width) / 20;
float sottr = 0f; for (float i = 0; i < 10; i++) 
{ 
     imgGraphic.FillRectangle(BrushShadow, new Rectangle((int)(sottr), 
		(int)sottr, (int)((float)width+sottr), (int)((float)height+sottr))); 
     sottr += steps; 
} 
// Finally draw the original image but shrink 
imgGraphic.DrawImage(img, new Rectangle(0, 0, width, height), 0, 0, 
	imgWidth, imgHeight, GraphicsUnit.Pixel); 
BrushShadow.Dispose(); 

img.Dispose(); 
img = workingimg; 
}

Points of Interest

Multithreading Capabilities

To enhance the speed of the images elaboration, I made the user free to choose the number of threads that will compute the work from 1 to 10 from a numericalupdown control in the settings panel. The set of images are subdivided into intervals based on the number of maximum usable threads.

The work can be suspended, resumed and aborted by the user.

C#
for (int i = 0 ; i < 11; i++) 
{
    threads[i] = new Thread(StarkWork);
    threads[i].Priority = ThreadPriority.Lowest;
    threads[i].IsBackground = false; 
} 
intSimultaneous = CONCURRENT_THREADS; 
int interval = (int)Math.Floor((double)((double)total / (double)Simultaneous)); 
int accumulator = 0; 
for (int i = 0; i < Simultaneous-1; i++) 
{ 
    ThreadInterval Bounds = new ThreadInterval();
    Bounds.LowerBound = accumulator;
    Bounds.UpperBound = accumulator+interval;
    threads[i].Start(Bounds);    
    accumulator += interval; 
} 
ThreadInterval BoundsRemained = new ThreadInterval(); 
BoundsRemained.LowerBound = accumulator;
BoundsRemained.UpperBound = total;

threads[Simultaneous].Start(BoundsRemained);

Some Cool Features

The user can choose the transparency key color simply by clicking on the watermark image.

Image 3

Conclusion

With this tool, you can make thumbnails like this:

Image 4

Future Developments and Updates

I plan to modify this tool for creating HTML pages.
I have made the modification in another article: An Application to Create Interesting and Fully Customizable Web Photo Gallery.

I chose to keep this article unchanged and attached the code because the target is different: this article is about thumbnails generator in batch, the new article ("Extended Web Gallery") is about HTML pages generation (with plugins).

External Link

For more information like user documentation, visit this page.

Note

English is not my first language, so please excuse any mistakes. This is my first article for The Code Project, so any suggestions and/or feedback will be appreciated.

License

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


Written By
Web Developer
Italy Italy
I'm an electronic engeneer form Genoa.
I like very much dotnet and C#. I've developed some works like an html gallery generator, a graphic formula viewer (user defined), a webcam grabber and html gallery generator, a little framework for image processing, an image resizer, elaboration in batch and some other application tool.
Actually I'm working in VC++ 6 MFC, but I hope that my future is .net !!!
Some of my works are downloadable at:
http://www.solchiere.it
Lorenzo Banderali

Comments and Discussions

 
GeneralMy vote of 5 Pin
Manoj Kumar Choubey26-Feb-12 20:55
professionalManoj Kumar Choubey26-Feb-12 20:55 
Nice
GeneralAll images set to some display Pin
nighttrain_21-Feb-11 0:42
nighttrain_21-Feb-11 0:42 
GeneralMy vote of 1 Pin
Wertugo5558-Dec-08 22:15
Wertugo5558-Dec-08 22:15 
QuestionImage Similarity Pin
3amoor20-Sep-06 7:48
3amoor20-Sep-06 7:48 
AnswerRe: Image Similarity Pin
Lorenzo1981Ge20-Sep-06 21:17
Lorenzo1981Ge20-Sep-06 21:17 
GeneralRe: Image Similarity Pin
3amoor21-Sep-06 2:57
3amoor21-Sep-06 2:57 
GeneralRe: Image Similarity Pin
Lorenzo1981Ge21-Sep-06 7:47
Lorenzo1981Ge21-Sep-06 7:47 
QuestionRe: Image Similarity Pin
3amoor21-Sep-06 8:01
3amoor21-Sep-06 8:01 
QuestionImage Reconstruction Pin
3amoor19-Sep-06 4:26
3amoor19-Sep-06 4:26 
AnswerRe: Image Reconstruction Pin
Lorenzo1981Ge19-Sep-06 7:38
Lorenzo1981Ge19-Sep-06 7:38 
GeneralRe: Image Reconstruction Pin
3amoor19-Sep-06 9:28
3amoor19-Sep-06 9:28 
GeneralRe: Image Reconstruction Pin
Lorenzo1981Ge19-Sep-06 21:06
Lorenzo1981Ge19-Sep-06 21:06 
GeneralImage Crop and Splitting Pin
3amoor18-Sep-06 13:24
3amoor18-Sep-06 13:24 
GeneralRe: Image Crop and Splitting Pin
Lorenzo1981Ge18-Sep-06 21:47
Lorenzo1981Ge18-Sep-06 21:47 
GeneralBad url link Pin
T4Top13-Sep-06 20:37
T4Top13-Sep-06 20:37 
GeneralRe: Bad url link Pin
Lorenzo1981Ge13-Sep-06 21:43
Lorenzo1981Ge13-Sep-06 21:43 

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.