ReplaceImage.cs
  1. //
  2. // This code is part of Document Solutions for Word demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using GrapeCity.Documents.Word;
  9.  
  10. namespace DsWordWeb.Demos
  11. {
  12. // This sample loads an existing document, and replaces
  13. // the (only) image in it with a different one.
  14. public class ReplaceImage
  15. {
  16. public GcWordDocument CreateDocx()
  17. {
  18. // The new image data:
  19. var picBytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "DianeForsyth.jpg"));
  20.  
  21. // Load the document:
  22. var doc = new GcWordDocument();
  23. doc.Load(Path.Combine("Resources", "WordDocs", "ProcurementLetter.docx"));
  24.  
  25. // Access the image to replace:
  26. var pic = doc.Body.Sections.First.GetRange().Pictures.First;
  27. // Replace the image data in place:
  28. pic.ImageData.SetImage(picBytes, "image/jpeg");
  29.  
  30. // Done:
  31. return doc;
  32. }
  33. }
  34. }
  35.