Click here to Skip to main content
15,886,788 members
Articles / Desktop Programming / Windows Forms
Article

A Simple Photo Publisher Program

Rate me:
Please Sign up or sign in to vote.
2.42/5 (6 votes)
20 Nov 20042 min read 44.6K   1.4K   26   4
This program shows an example of how to manipulate lots of files to convert their size, quality and get a thumbnail image.

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.

C#
private int quality;
private string FullSizePath;
private string PreviewPathOut;
private string FullSizePathOut;
private bool AllocatedL;
private System.Drawing.Size FS; //full size
private System.Drawing.Size PS; //preview

When the associated image needs to be processed or shown, the image is allocated in memory with the following code:

C#
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:

C#
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...

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
Italy Italy
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralMissing reference Pin
Marc Clifton21-Nov-04 2:17
mvaMarc Clifton21-Nov-04 2:17 
GeneralRe: Missing reference Pin
GMachiavelli21-Nov-04 3:16
GMachiavelli21-Nov-04 3:16 
GeneralRe: Missing reference Pin
Le_MuLoT8-Dec-05 8:33
Le_MuLoT8-Dec-05 8:33 
AnswerRe: Missing reference Pin
z00z07-Jun-08 0:57
z00z07-Jun-08 0:57 

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.