Introduction
A Picture Publisher converts size, names, compresses pictures (.bmp, .gif, .jpg) and generates preview images (like web albums). Simple use of threads is shown here. You can find the updates in Guidomarche.NET. This is a very simple program used for converting many graphics files at the same time. SinglePhoto.cs contains a class that is used to gather information about each photo to process, such as the original path, size, compression, the new path, size and compression. It only stores information about the new picture properties. New pictures are created only when they are saved.
private int quality;
private string FullSizePath;
private string PreviewPathOut;
private string FullSizePathOut;
private bool AllocatedL;
private System.Drawing.Size FS;
private System.Drawing.Size PS;
When the associated image needs to be processed or shown, the image is allocated in memory with the following code:
public bool Allocate()
{
System.Drawing.Image.GetThumbnailImageAbort myCallBack =
new System.Drawing.Image.GetThumbnailImageAbort(PreviewFail);
try
{
this.FullSize=new Bitmap(FullSizePath);
this.Preview=this.FullSize.GetThumbnailImage(PS.Width,
PS.Height,myCallBack,IntPtr.Zero);
this.AllocatedL=true;
}
catch
{
return false;
}
return true;
}
Now the SinglePhoto
object contains a full size image (the original) and a preview image. myCallBack
is something like a delegate, which is required to handle failures in GetThumbnailImage
method. Using SinglePhoto
's FHeight
, FWidth
, PHeight
, PWidth
or FSSize
,PSSize
proprieties, or using ResizeFS(int alt, int larg)
, ResizePS(int alt, int larg)
you can set the size of the picture, and get a resized picture using FsImg
, PrvImg
properties.
The automated task zone, contains a few ways of generating a sequence of names for photos. I would like to add a few words on StartLoad()
and StartSave()
: I decided to load and save pictures in a separate thread, to avoid "freezing" of the graphic interface. A good idea is to convert and save each picture using a different thread, in order to speed up the operations. But I am not sure whether it will be really faster. Running a thread is very simple, this shows how to run a thread:
private void StartLoad()
{
ThreadStart LoadingThreadDelegate=new ThreadStart(this.LoadTasks);
Thread LoadingThread= new Thread(LoadingThreadDelegate);
LoadingThread.Name="LoadingThread";
LoadingThread.Start();
}
All you have to do is, create a ThreadStart
object, linked to the method you want to start with, when the thread starts. In this example, I have created LoadingThreadDelegate=new ThreadStart(this.LoadTasks)
, a ThreadStart
object linked to this.LoadTasks()
. Then you can use the ThreadStart
object (named LoadingThreadDelegate
) to create a new Thread
object, and then start it. After LoadingThread.Start()
is executed, the method LoadTasks()
and the main thread (user interface) will run concurrently.
That's all...