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