private void DoWork(object sender, DoWorkEventArgs e) { Microsoft.Win32.OpenFileDialog backOfd = (Microsoft.Win32.OpenFileDialog)e.Argument; ResultParameter rp = new ResultParameter(); int count = 0; double step = 100.0 / (double)backOfd.FileNames.Length; FileStream SourceStream = null; Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); foreach (string FileName in backOfd.FileNames) { try { paths.Add(FileName); count++; backgroundWorker.ReportProgress((int)(count * step), ofd.SafeFileNames[count - 1]); SourceStream = File.Open(FileName, FileMode.Open); if (ofd.SafeFileNames[count - 1].Split('.')[1] == "pcx") { rp.Images.Add(new BindingImage(++ImagesCount, backOfd.FileNames[count - 1], SourceStream)); } else { System.Drawing.Image img = System.Drawing.Image.FromStream(SourceStream, false, false); rp.Images.Add(new BindingImage(++ImagesCount, backOfd.FileNames[count - 1], img)); } } catch (Exception ex) { } finally { if (SourceStream != null) { SourceStream.Close(); } } } stopWatch.Stop(); // Get the elapsed time as a TimeSpan value. TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. rp.ElapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); e.Result = rp; }
private void WorkDone(object sender, RunWorkerCompletedEventArgs e) { ResultParameter rp = (ResultParameter)e.Result; foreach (BindingImage img in rp.Images) { ImageList.Add(img); } int TotalImages = ofd.FileNames.Length; int Errors = TotalImages - rp.Images.Count; StateBar.Value = 0; ProcessLabel.Text = "Complete! " + TotalImages + " new files loaded with " + Errors + " errors. " + "RunTime: " + rp.ElapsedTime; }