使用backgroundworker更新progressbar + clipboard.getimage() - WPF C#

时间:2016-10-03 13:46:03

标签: c# wpf backgroundworker clipboard

我的程序应该将文件夹中的某些Excel文件作为图像(SVG格式)导出到另一个文件夹中。这可以通过方法CopyAndSaveTo和ExportRangeAsImage来完成,它们可以完成它们的工作。 在MainWindow上,我有一个按钮,在单击时执行这两个功能。我希望通知点击此按钮的用户(进度条)此过程有多远(复制+导出)。 我试图使用BackgroundWorker来实现它,只有当我评论以下代码时(在方法ExportRangeAsImage中)它才有效:

Bitmap image = new Bitmap(System.Windows.Forms.Clipboard.GetImage());
image.Save(ImagePath + Path.GetFileNameWithoutExtension(file) + ".svg");

否则我收到以下错误消息( 德语翻译):

在System.NullReferenceException中,发生了类型为“System.Drawing.dll”的异常,但未在用户代码中处理。 附加信息:未将对象引用设置为对象实例。 如果处理程序可用于此异常,则程序可以继续安全运行。

以下是整个代码:

private void UpdateDataOfLine1(object sender, ExecutedRoutedEventArgs e)
        {
            string[] files = Directory.GetFiles(RootPath);
 
            BackgroundWorker worker = new BackgroundWorker();
            worker.WorkerReportsProgress = true;
            worker.DoWork += worker_DoWork;
            worker.ProgressChanged += worker_ProgressChanged;
            worker.RunWorkerAsync();
        }
 

 
        void worker_DoWork(object sender, DoWorkEventArgs e)
        {
            string[] files = Directory.GetFiles(RootPath);
 
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Contains("$") || files[i].Contains("~") || files[i].Contains("Thumb"))
                {
                    continue;
                }
 
                File.Copy(files[i], DestPath + Path.GetFileName(files[i]), true);
 
                string newFile = DestPath + Path.GetFileName(files[i]);
 
                ExPortRangeAsImage(newFile);
 
                (sender as BackgroundWorker).ReportProgress(i);
                Thread.Sleep(100);
            }
        }
 

 
        void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            Status.Value = e.ProgressPercentage;
        }
 

        public void ExPortRangeAsImage(string file)
        {
            var ExcelApp = new Microsoft.Office.Interop.Excel.Application();
 
            try
            {
                if (file.Contains("BeispielDatei"))
                {
                    Workbook wb = ExcelApp.Workbooks.Open(file);
                    Worksheet ws = (Worksheet)wb.Sheets[1];
                    Range range = ws.Range["A1:U35"];
                    range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
                    wb.Close(SaveChanges: false);
                    Marshal.ReleaseComObject(wb);
                }
 
                else
                {
                    Workbook wb = ExcelApp.Workbooks.Open(file);
                    Worksheet ws = (Worksheet)wb.Sheets[1];
                    Range range = ws.Range["A1:U35"];
                    range.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
                    wb.Close(SaveChanges: false);
                    Marshal.ReleaseComObject(wb);
                }
            }
 
            finally
            {
                Bitmap image = new Bitmap(System.Windows.Forms.Clipboard.GetImage());
                image.Save(ImagePath + Path.GetFileNameWithoutExtension(file) + ".svg");
 
                Marshal.ReleaseComObject(ExcelApp);
            }
        }

你能告诉我我做错了什么或者我怎么能意识到这一点? 除了BackgroundWorker之外还有其他方法吗?

提前感谢您的帮助!

以下是错误的屏幕截图

1 个答案:

答案 0 :(得分:0)

您应该查看堆栈跟踪,我认为您的内部异常很可能是由于跨线程访问冲突。

由于:System.Windows.Forms.Clipboard.GetImage()可能正在返回null,因此可能会触发空引用。

您可以尝试在UI调度程序线程中运行此部分:

Application.Current.Dispatcher.BeginInvoke(DispatcherPriority.Normal, new Action(()=>
{
   Bitmap image = new Bitmap(System.Windows.Forms.Clipboard.GetImage());
   image.Save(ImagePath + Path.GetFileNameWithoutExtension(file) + ".svg");
}

这也很可能会引入同步问题(复制和粘贴之间的竞争)。在将另一个图像复制到剪贴板之前,您可能需要重新构建代码以确保图像写入完成。

相关问题