Click here to Skip to main content
15,881,600 members
Articles / Desktop Programming / Windows Forms

Converting Icons to Images with a Background Worker

Rate me:
Please Sign up or sign in to vote.
4.00/5 (5 votes)
25 Oct 2010CPOL2 min read 30.5K   463   14   6
Using a background worker thread to execute a time consuming task

Introduction

This article is an example of using a background worker thread to execute a time consuming task.

Background

I like creating software and for this I need some images for buttons, etc. There are many images you can find for downloading from the internet, but unfortunately, many are in icon format (.ico, .icl, etc) and must be converted before use. You can use many image editors such as PhotoShop, Gimp, Axialis, etc., but you must convert the images one by one. Being kind of lazy, of course I don't like that, so I created an application to automate the process. The function for converting is time consuming, however, so it makes user interface (UI) unresponsive. To resolve that problem, the function must execute in a background thread.

Using the Code

To convert icons, I use the iconlib library from this article. [^]

The following struct is used for passing an argument to the background worker. You need two of these, one for passing to background worker and one for updating the UI.

C#
public struct PassedObject //for passing to background worker
{
    public List<string> fileListing;
    public string initDirectory;         
    public PassedObject(List<string> listFilenya, string initDirnya)
    {
        fileListing = listFilenya;
        initDirectory = initDirnya;
     }
}
public struct ItemToReport //for passing from background worker
{
    public string strToReport ;
    public int id;
    public ItemToReport(string reportString, int ID)
    {
        strToReport = reportString;
        id = ID;
    }
}

The following function executes in background, converting the icons. Be careful, because any exception that occurs in this code is hard to debug. (I'm using the Express Edition.)

C#
private void convertIcon(BackgroundWorker worker, DoWorkEventArgs e)
{
    PassedObject objectToPass = (PassedObject)e.Argument; //perform unboxing
    //initial folder to create new folders of converted image
    string initDirectory = objectToPass.initDirectory ; 
    ///hold list of files
    List<string> fList = objectToPass.fileListing ;
    ...
    foreach (string item in fList )
    {
        FileInfo iconFile = new FileInfo(item);
        ItemToReport pesan = new ItemToReport("processing " + iconFile.Name + "...\n", 0);
        curIconProg = 0; //reset icon counter, prevent overflow at progressbar        
        worker.ReportProgress(0, pesan); //report : processing----        
	...
    }
}

The function that calls the background function and begins execution of the background thread is

C#
private void bgw_DoWork(object sender, DoWorkEventArgs e)
{            
     BackgroundWorker worker = sender as BackgroundWorker;
     convertIcon(worker, e);
}

This code is called when the background function wants to update the UI on the main thread. In my application, it is used for updating the ProgressBar. To allow the UI to be updated, you need to set workerReportProgress to true.

C#
private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    ItemToReport report = (ItemToReport)e.UserState; //perform unboxing message
    switch (report.id )
    {
         case 0:
            rtbReport.AppendText(report.strToReport);
             break;
         ...            
    }
}

The following function is called when the background worker has complete its work.

C#
private void bgw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
    btnConvert.Enabled = true; 
}

That's all there is to it. It should be easy to implement.

Points of Interest

It took me a long time to finally understand this process. Actually, most of the information comes from help, but some points are missing so it's not as easy to implement as it should be.

License

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


Written By
Engineer Donatus Inc.
Indonesia Indonesia
I'm currently a student of Brawijaya University. I work on programming just for hobby. I learn programming since 2nd year of my college. That's when I first bought my PC on year 2004.

Comments and Discussions

 
QuestionConverting? Pin
RedDk16-Apr-15 7:55
RedDk16-Apr-15 7:55 
I don't see how there's a conversion going on. Might be missing the point. Am I?

AnswerRe: Converting? Pin
asugix22-Apr-15 22:33
asugix22-Apr-15 22:33 
GeneralRe: Converting? Pin
RedDk23-Apr-15 8:58
RedDk23-Apr-15 8:58 
GeneralRe: Converting? Pin
asugix29-Apr-15 19:27
asugix29-Apr-15 19:27 
GeneralA broken link Pin
Qing Jiang3-Nov-10 7:47
Qing Jiang3-Nov-10 7:47 
GeneralRe: A broken link Pin
asugix5-Nov-10 1:36
asugix5-Nov-10 1:36 

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.