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
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 imagesUDirTree
- Provides the folder and files navigationPanello
- Is the workpanel
that contains several UImage
controlsUimage
- The user control that displays an image on the work panelCImage
- Provides the main image processing like resize, watermark, shadow effect, JPG saving, etc.
The follow code lines create the shadow effect:
public void makeshadow(ref Bitmap img, Color backcolor)
{
int imgWidth = img.Width;
int imgHeight = img.Height;
Bitmap workingimg = new Bitmap(imgWidth, imgHeight, PixelFormat.Format24bppRgb);
workingimg.SetResolution(img.HorizontalResolution, img.VerticalResolution);
Graphics imgGraphic = Graphics.FromImage(workingimg);
int width = (int)((float)img.Width * 94 / 100);
int height = (int)((float)img.Height * 94 / 100);
SolidBrush BrushShadow = new SolidBrush(Color.FromArgb(10, 0, 0, 0));
imgGraphic.Clear(backcolor);
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;
}
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.
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.
Conclusion
With this tool, you can make thumbnails like this:
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.
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