title: Programmatically change/add thumbnail of pptx powerpoint. With Openxml sdk?
body: <p>I use openxml sdk 2.5 in combination with the power tools by Eric White. I've managed to create dynamic pptx presentations using template files. (In C#)
  Unfortunately the thumbnail gets lost during the process. <br><strong>Is there any way to (re)create the thumbnail of a pptx-file using openxml or power tools?</strong> <br> I successfully wrote some code that changes an existing thumbnail with an image. But when there is is no thumbnail it gives me a <em>System.NullReferenceException</em>. Here is the code:</p>
  
  <pre><code>using OpenXmlPowerTools;
  using System;
  using System.Collections.Generic;
  using System.IO;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using DocumentFormat.OpenXml.Packaging;
  
  namespace ConsoleApplication1
  {
      class AddThumbnail_
      {
          public static void ReplaceThumbnail(ThumbnailPart thumbnailPart, string newThumbnail)
          {
              using (
                  FileStream imgStream = new FileStream(newThumbnail, FileMode.Open, FileAccess.Read))
              {
                  thumbnailPart.FeedData(imgStream);
              }
          }
  
          static void Main(string[] args)
          {
              var templatePresentation = "Modified.pptx";
              var outputPresentation = "Modified.pptx";
              var baPresentation = File.ReadAllBytes(templatePresentation);
              var pmlMainPresentation = new PmlDocument("Main.pptx", baPresentation);
              OpenXmlMemoryStreamDocument streamDoc = new OpenXmlMemoryStreamDocument(pmlMainPresentation);
              PresentationDocument document = streamDoc.GetPresentationDocument();
              var thumbNailPart = document.ThumbnailPart;
              ReplaceThumbnail(thumbNailPart, @"C:\Path\to\image\image.jpg");
              document.SaveAs(outputPresentation);
          }
      }
  }
  </code></pre>
  
  <p><strong>EDIT:</strong>
  I realize this question has been asked before (<a href="https://stackoverflow.com/questions/11146072/how-to-generate-thumbnail-image-for-a-pptx-file-in-c">How to generate thumbnail image for a PPTX file in C#?</a>) and the answer is "enable preview screenshot when saving the presentation" but this would mean I'd have to open every pptx and manually set this flag. I would appreciate a C# solution.</p>
  
  <p>Thank you in advance!</p>