void ProgressBox_FormClosing(object sender, FormClosingEventArgs e)
 {
     // closing the window implies cancellation
     if (worker.IsBusy)
     {
         targetBitmap = null;
         worker.CancelAsync();
         GC.Collect();
     }
 }
        public ImporterOptimised(Bitmap bitmap)
        {
            worker.WorkerReportsProgress = true;
            worker.DoWork                    += Worker_DoWork;
            worker.ProgressChanged           += Worker_ProgressChanged;
            worker.RunWorkerCompleted        += Worker_Completed;
            worker.WorkerSupportsCancellation = true;

            sourceBitmap = bitmap;
            targetBitmap = new CharBitmap(bitmap.Width, bitmap.Height);

            progressBox.Text         = "Importing bitmap...";
            progressBox.Value        = 0;
            progressBox.FormClosing += ProgressBox_FormClosing;
        }