PictureAdd.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. Imports GrapeCity.Documents.Imaging
  10.  
  11. '' This sample demonstrates how to add a JPEG image to a document.
  12. Public Class PictureAdd
  13. Function CreateDocx() As GcWordDocument
  14. Dim doc = New GcWordDocument()
  15.  
  16. '' Load picture data
  17. Dim picBytes = File.ReadAllBytes(Path.Combine("Resources", "Images", "road.jpg"))
  18. '' Create a GcBitmap so that we can find out the native picture size
  19. Dim image = New GcBitmap(picBytes)
  20. Dim width = doc.Body.Sections.Last.PageSetup.ClientWidth
  21. Dim height = image.Height * (width / image.Width)
  22.  
  23. '' Add an inline picture that fills the page width
  24. Dim pars = doc.Body.Sections.First.GetRange().Paragraphs
  25. Dim par = pars.Add("Picture sized to fill the page:")
  26. Dim Run = par.GetRange().Runs.Last
  27. Run.GetRange().Texts.AddBreak()
  28. '' Add picture, specifying its mime type
  29. Dim pic = Run.GetRange().Pictures.Add(picBytes, "image/jpeg")
  30. '' Scale picture size to fill the width of the page
  31. pic.Size.Width.Value = width
  32. pic.Size.Height.Value = height
  33.  
  34. pars.Add("The End.")
  35.  
  36. '' Done
  37. Return doc
  38. End Function
  39. End Class
  40.