title: How can I export an excel worksheet as image?
body: <p>I'm triying to generate an image from an excel worksheet. After a lot of research, I'm using the following code, but at some point I get an exception:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using Microsoft.Office.Interop.Excel;
  
  namespace ConsoleApplication1
  {
      class Prueba
      {
          [STAThread]
          static void Main(string[] args)
          {
              var a = new Microsoft.Office.Interop.Excel.Application();
  
              try
              {
                  Workbook w = a.Workbooks.Open(@"C:\SCRATCH\Libro2.xlsx");
                  Worksheet ws = w.Sheets["Report"];
                  ws.Protect(Contents: false);
                  Range r = ws.Range["B2:H20"];
                  r.CopyPicture(XlPictureAppearance.xlScreen, XlCopyPictureFormat.xlBitmap);
                  a.DisplayAlerts = false;
  
                  // System.Runtime.InteropServices.COMException Excepción de HRESULT: 0x80010105 (RPC_E_SERVERFAULT)
                  ChartObject chartObj = ws.ChartObjects().Add(r.Left, r.Top, r.Width, r.Height); 
  
                  chartObj.Activate();
                  Chart chart = chartObj.Chart;
                  chart.Paste();
                  chart.Export(@"C:\SCRATCH\image.JPG", "JPG");
                  chartObj.Delete();
                  w.Close(SaveChanges: false);
              } 
              finally
              {
                  a.Quit();                
              }
  
          }
      }
  }
  </code></pre>
  
  <p>I'm using Office 2013, 64 bits, Windows 7 64 and .Net 4.5.</p>