1

I am currently writing an app that exports certain data areas of some Excel files as image files to an image folder and then displays them. The program should also be able to update all files (including these) during the display of a file.

Problem:

The ExportRangeAsImage () method, which exports the data area of an Excel file to image files, is called in a background worker, which is implemented in the same class (ViewModel). If the update of a file runs while it is displayed at the same time, I get the following error:

// ExportRangeAsImage() {
   ...

System.Windows.Application.Current.Dispatcher.BeginInvoke(
      DispatcherPriority.Normal, new System.Action(() =>
        {
           Bitmap image = new Bitmap(System.Windows.Forms.Clipboard.GetImage());

           if (!file.Contains("XYZ"))
           {
              //The program stops here
              image.Save(ImagePathM1 + Path.GetFileNameWithoutExtension(file) + ".svg");                   Marshal.ReleaseComObject(ExcelApp);
           }
       }));

An unhandled exception of type 'System.Runtime.InteropServices.ExternalException' occurred in System.Drawing.dll Additional information: A generic error occurred in GDI+.**

I guess it is because the file that is being displayed and file has the same name and the program can not change or overwrite a file that is being used!

Can you help me solve this problem ???

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122
Guilian
  • 129
  • 1
  • 11
  • 1
    If that is your concern, the solution is: use different file names in each thread. Wow! How else can we help you? – Thomas Weller Nov 03 '16 at 14:30
  • This has nothing to do with files. The error is clearly about the UI – Panagiotis Kanavos Nov 03 '16 at 14:52
  • @PanagiotisKanavos I'm [pretty sure](http://stackoverflow.com/questions/27438058/image-save-method-is-not-save-image-gdi-error-occured) that [the chances](http://stackoverflow.com/questions/15862810/a-generic-error-occured-in-gdi-in-bitmap-save-method) are [pretty high](http://stackoverflow.com/q/5813633/6682181) that it [has to do](http://stackoverflow.com/q/29973934/6682181) with [files and multithreading](http://stackoverflow.com/q/7694741/6682181). But of course it could also be the [file format](http://stackoverflow.com/q/1053052/6682181). – haindl Nov 03 '16 at 15:10

1 Answers1

1

I am surmising that ImagePathM1 is active on your GUI thread and that is the root of the problem in the other thread which tries to access the defacto shared resource. Pass in the value of ImagePathM1 to the secondary thread instead of having the thread access ImagePathM1 reference directly.

ΩmegaMan
  • 29,542
  • 12
  • 100
  • 122