您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.IO.Packaging;
  3. using System.IO;
  4. namespace Novacode
  5. {
  6. /// <summary>
  7. /// Represents an Image embedded in a document.
  8. /// </summary>
  9. public class Image
  10. {
  11. /// <summary>
  12. /// A unique id which identifies this Image.
  13. /// </summary>
  14. private string id;
  15. private DocX document;
  16. internal PackageRelationship pr;
  17. public Stream GetStream(FileMode mode, FileAccess access)
  18. {
  19. string temp = pr.SourceUri.OriginalString;
  20. string start = temp.Remove(temp.LastIndexOf('/'));
  21. string end = pr.TargetUri.OriginalString;
  22. string full = start + "/" + end;
  23. return(document.package.GetPart(new Uri(full, UriKind.Relative)).GetStream(mode, access));
  24. }
  25. /// <summary>
  26. /// Returns the id of this Image.
  27. /// </summary>
  28. public string Id
  29. {
  30. get {return id;}
  31. }
  32. internal Image(DocX document, PackageRelationship pr)
  33. {
  34. this.document = document;
  35. this.pr = pr;
  36. this.id = pr.Id;
  37. }
  38. /// <summary>
  39. /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
  40. /// </summary>
  41. /// <returns></returns>
  42. /// <example>
  43. /// Add an image to a document, create a custom view of that image (picture) and then insert it into a Paragraph using append.
  44. /// <code>
  45. /// using (DocX document = DocX.Create("Test.docx"))
  46. /// {
  47. /// // Add an image to the document.
  48. /// Image i = document.AddImage(@"Image.jpg");
  49. ///
  50. /// // Create a picture i.e. (A custom view of an image)
  51. /// Picture p = i.CreatePicture();
  52. /// p.FlipHorizontal = true;
  53. /// p.Rotation = 10;
  54. ///
  55. /// // Create a new Paragraph.
  56. /// Paragraph par = document.InsertParagraph();
  57. ///
  58. /// // Append content to the Paragraph.
  59. /// par.Append("Here is a cool picture")
  60. /// .AppendPicture(p)
  61. /// .Append(" don't you think so?");
  62. ///
  63. /// // Save all changes made to this document.
  64. /// document.Save();
  65. /// }
  66. /// </code>
  67. /// </example>
  68. public Picture CreatePicture()
  69. {
  70. return Paragraph.CreatePicture(document, id, string.Empty, string.Empty);
  71. }
  72. public Picture CreatePicture(int height, int width) {
  73. Picture picture = Paragraph.CreatePicture(document, id, string.Empty, string.Empty);
  74. picture.Height = height;
  75. picture.Width = width;
  76. return picture;
  77. }
  78. ///<summary>
  79. /// Returns the name of the image file.
  80. ///</summary>
  81. public string FileName
  82. {
  83. get
  84. {
  85. return Path.GetFileName(this.pr.TargetUri.ToString());
  86. }
  87. }
  88. }
  89. }