You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Picture.cs 10KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Xml.Linq;
  6. using System.Drawing;
  7. using System.IO.Packaging;
  8. namespace Novacode
  9. {
  10. /// <summary>
  11. /// Represents a Picture in this document, a Picture is a customized view of an Image.
  12. /// </summary>
  13. public class Picture: DocXElement
  14. {
  15. private const int EmusInPixel = 9525;
  16. internal Dictionary<PackagePart, PackageRelationship> picture_rels;
  17. internal Image img;
  18. private string id;
  19. private string name;
  20. private string descr;
  21. private int cx, cy;
  22. //private string fileName;
  23. private uint rotation;
  24. private bool hFlip, vFlip;
  25. private object pictureShape;
  26. private XElement xfrm;
  27. private XElement prstGeom;
  28. /// <summary>
  29. /// Remove this Picture from this document.
  30. /// </summary>
  31. public void Remove()
  32. {
  33. Xml.Remove();
  34. }
  35. /// <summary>
  36. /// Wraps an XElement as an Image
  37. /// </summary>
  38. /// <param name="i">The XElement i to wrap</param>
  39. internal Picture(DocX document, XElement i, Image img):base(document, i)
  40. {
  41. picture_rels = new Dictionary<PackagePart, PackageRelationship>();
  42. this.img = img;
  43. this.id =
  44. (
  45. from e in Xml.Descendants()
  46. where e.Name.LocalName.Equals("blip")
  47. select e.Attribute(XName.Get("embed", "http://schemas.openxmlformats.org/officeDocument/2006/relationships")).Value
  48. ).Single();
  49. this.name =
  50. (
  51. from e in Xml.Descendants()
  52. let a = e.Attribute(XName.Get("name"))
  53. where (a != null)
  54. select a.Value
  55. ).First();
  56. this.descr =
  57. (
  58. from e in Xml.Descendants()
  59. let a = e.Attribute(XName.Get("descr"))
  60. where (a != null)
  61. select a.Value
  62. ).FirstOrDefault();
  63. this.cx =
  64. (
  65. from e in Xml.Descendants()
  66. let a = e.Attribute(XName.Get("cx"))
  67. where (a != null)
  68. select int.Parse(a.Value)
  69. ).First();
  70. this.cy =
  71. (
  72. from e in Xml.Descendants()
  73. let a = e.Attribute(XName.Get("cy"))
  74. where (a != null)
  75. select int.Parse(a.Value)
  76. ).First();
  77. this.xfrm =
  78. (
  79. from d in Xml.Descendants()
  80. where d.Name.LocalName.Equals("xfrm")
  81. select d
  82. ).Single();
  83. this.prstGeom =
  84. (
  85. from d in Xml.Descendants()
  86. where d.Name.LocalName.Equals("prstGeom")
  87. select d
  88. ).Single();
  89. this.rotation = xfrm.Attribute(XName.Get("rot")) == null ? 0 : uint.Parse(xfrm.Attribute(XName.Get("rot")).Value);
  90. }
  91. private void SetPictureShape(object shape)
  92. {
  93. this.pictureShape = shape;
  94. XAttribute prst = prstGeom.Attribute(XName.Get("prst"));
  95. if (prst == null)
  96. prstGeom.Add(new XAttribute(XName.Get("prst"), "rectangle"));
  97. prstGeom.Attribute(XName.Get("prst")).Value = shape.ToString();
  98. }
  99. /// <summary>
  100. /// Set the shape of this Picture to one in the BasicShapes enumeration.
  101. /// </summary>
  102. /// <param name="shape">A shape from the BasicShapes enumeration.</param>
  103. public void SetPictureShape(BasicShapes shape)
  104. {
  105. SetPictureShape((object)shape);
  106. }
  107. /// <summary>
  108. /// Set the shape of this Picture to one in the RectangleShapes enumeration.
  109. /// </summary>
  110. /// <param name="shape">A shape from the RectangleShapes enumeration.</param>
  111. public void SetPictureShape(RectangleShapes shape)
  112. {
  113. SetPictureShape((object)shape);
  114. }
  115. /// <summary>
  116. /// Set the shape of this Picture to one in the BlockArrowShapes enumeration.
  117. /// </summary>
  118. /// <param name="shape">A shape from the BlockArrowShapes enumeration.</param>
  119. public void SetPictureShape(BlockArrowShapes shape)
  120. {
  121. SetPictureShape((object)shape);
  122. }
  123. /// <summary>
  124. /// Set the shape of this Picture to one in the EquationShapes enumeration.
  125. /// </summary>
  126. /// <param name="shape">A shape from the EquationShapes enumeration.</param>
  127. public void SetPictureShape(EquationShapes shape)
  128. {
  129. SetPictureShape((object)shape);
  130. }
  131. /// <summary>
  132. /// Set the shape of this Picture to one in the FlowchartShapes enumeration.
  133. /// </summary>
  134. /// <param name="shape">A shape from the FlowchartShapes enumeration.</param>
  135. public void SetPictureShape(FlowchartShapes shape)
  136. {
  137. SetPictureShape((object)shape);
  138. }
  139. /// <summary>
  140. /// Set the shape of this Picture to one in the StarAndBannerShapes enumeration.
  141. /// </summary>
  142. /// <param name="shape">A shape from the StarAndBannerShapes enumeration.</param>
  143. public void SetPictureShape(StarAndBannerShapes shape)
  144. {
  145. SetPictureShape((object)shape);
  146. }
  147. /// <summary>
  148. /// Set the shape of this Picture to one in the CalloutShapes enumeration.
  149. /// </summary>
  150. /// <param name="shape">A shape from the CalloutShapes enumeration.</param>
  151. public void SetPictureShape(CalloutShapes shape)
  152. {
  153. SetPictureShape((object)shape);
  154. }
  155. /// <summary>
  156. /// A unique id that identifies an Image embedded in this document.
  157. /// </summary>
  158. public string Id
  159. {
  160. get { return id; }
  161. }
  162. /// <summary>
  163. /// Flip this Picture Horizontally.
  164. /// </summary>
  165. public bool FlipHorizontal
  166. {
  167. get { return hFlip; }
  168. set
  169. {
  170. hFlip = value;
  171. XAttribute flipH = xfrm.Attribute(XName.Get("flipH"));
  172. if (flipH == null)
  173. xfrm.Add(new XAttribute(XName.Get("flipH"), "0"));
  174. xfrm.Attribute(XName.Get("flipH")).Value = hFlip ? "1" : "0";
  175. }
  176. }
  177. /// <summary>
  178. /// Flip this Picture Vertically.
  179. /// </summary>
  180. public bool FlipVertical
  181. {
  182. get { return vFlip; }
  183. set
  184. {
  185. vFlip = value;
  186. XAttribute flipV = xfrm.Attribute(XName.Get("flipV"));
  187. if (flipV == null)
  188. xfrm.Add(new XAttribute(XName.Get("flipV"), "0"));
  189. xfrm.Attribute(XName.Get("flipV")).Value = vFlip ? "1" : "0";
  190. }
  191. }
  192. /// <summary>
  193. /// The rotation in degrees of this image, actual value = value % 360
  194. /// </summary>
  195. public uint Rotation
  196. {
  197. get { return rotation / 60000; }
  198. set
  199. {
  200. rotation = (value % 360) * 60000;
  201. XElement xfrm =
  202. (from d in Xml.Descendants()
  203. where d.Name.LocalName.Equals("xfrm")
  204. select d).Single();
  205. XAttribute rot = xfrm.Attribute(XName.Get("rot"));
  206. if(rot == null)
  207. xfrm.Add(new XAttribute(XName.Get("rot"), 0));
  208. xfrm.Attribute(XName.Get("rot")).Value = rotation.ToString();
  209. }
  210. }
  211. /// <summary>
  212. /// Gets or sets the name of this Image.
  213. /// </summary>
  214. public string Name
  215. {
  216. get { return name; }
  217. set
  218. {
  219. name = value;
  220. foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("name")))
  221. a.Value = name;
  222. }
  223. }
  224. /// <summary>
  225. /// Gets or sets the description for this Image.
  226. /// </summary>
  227. public string Description
  228. {
  229. get { return descr; }
  230. set
  231. {
  232. descr = value;
  233. foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("descr")))
  234. a.Value = descr;
  235. }
  236. }
  237. ///<summary>
  238. /// Returns the name of the image file for the picture.
  239. ///</summary>
  240. public string FileName
  241. {
  242. get
  243. {
  244. return img.FileName;
  245. }
  246. }
  247. /// <summary>
  248. /// Get or sets the Width of this Image.
  249. /// </summary>
  250. public int Width
  251. {
  252. get { return cx / EmusInPixel; }
  253. set
  254. {
  255. cx = value;
  256. foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("cx")))
  257. a.Value = (cx * EmusInPixel).ToString();
  258. }
  259. }
  260. /// <summary>
  261. /// Get or sets the height of this Image.
  262. /// </summary>
  263. public int Height
  264. {
  265. get { return cy / EmusInPixel; }
  266. set
  267. {
  268. cy = value;
  269. foreach (XAttribute a in Xml.Descendants().Attributes(XName.Get("cy")))
  270. a.Value = (cy * EmusInPixel).ToString();
  271. }
  272. }
  273. //public void Delete()
  274. //{
  275. // // Remove xml
  276. // i.Remove();
  277. // // Rebuild the image collection for this paragraph
  278. // // Requires that every Image have a link to its paragraph
  279. //}
  280. }
  281. }