Click here to Skip to main content
13,451,731 members (41,470 online)
Click here to Skip to main content
Add your own
alternative version


13 bookmarked
Posted 21 Jan 2009

Converting Icons to Images with a Background Worker

, 25 Oct 2010
Rate this:
Please Sign up or sign in to vote.
Using a background worker thread to execute a time consuming task


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


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.

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.)

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

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.

private void bgw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    ItemToReport report = (ItemToReport)e.UserState; //perform unboxing message
    switch ( )
         case 0:

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

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.


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


About the Author

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.

You may also be interested in...

Comments and Discussions

QuestionConverting? Pin
RedDk16-Apr-15 7:55
memberRedDk16-Apr-15 7:55 
AnswerRe: Converting? Pin
asugix22-Apr-15 22:33
memberasugix22-Apr-15 22:33 
GeneralRe: Converting? Pin
RedDk23-Apr-15 8:58
memberRedDk23-Apr-15 8:58 
GeneralRe: Converting? Pin
asugix29-Apr-15 19:27
memberasugix29-Apr-15 19:27 
GeneralA broken link Pin
Qing Jiang3-Nov-10 7:47
memberQing Jiang3-Nov-10 7:47 
GeneralRe: A broken link Pin
asugix5-Nov-10 1:36
memberasugix5-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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180318.3 | Last Updated 25 Oct 2010
Article Copyright 2009 by asugix
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid