ReplaceImage.vb
  1. ''
  2. '' This code is part of Document Solutions for Word demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System
  6. Imports System.IO
  7. Imports System.Drawing
  8. Imports GrapeCity.Documents.Word
  9.  
  10. '' This sample loads an existing document, And replaces
  11. '' the (only) image in it with a different one.
  12. Public Class ReplaceImage
  13. Public Function CreateDocx() As GcWordDocument
  14. '' The New image data
  15. Dim picBytes = File.ReadAllBytes(Path.Combine("Resources", "ImagesBis", "DianeForsyth.jpg"))
  16.  
  17. '' Load the document
  18. Dim doc = New GcWordDocument()
  19. doc.Load(Path.Combine("Resources", "WordDocs", "ProcurementLetter.docx"))
  20.  
  21. '' Access the image to replace
  22. Dim pic = doc.Body.Sections.First.GetRange().Pictures.First
  23. '' Replace the image data in place:
  24. pic.ImageData.SetImage(picBytes, "image/jpeg")
  25.  
  26. '' Done
  27. Return doc
  28. End Function
  29. End Class
  30.