Watermark.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. using System;
  2. using System.ComponentModel;
  3. using System.Drawing;
  4. using FastReport.Utils;
  5. using System.Windows.Forms;
  6. using System.Drawing.Design;
  7. namespace FastReport
  8. {
  9. /// <summary>
  10. /// Specifies the watermark image size mode.
  11. /// </summary>
  12. public enum WatermarkImageSize
  13. {
  14. /// <summary>
  15. /// Specifies the normal (original) size.
  16. /// </summary>
  17. Normal,
  18. /// <summary>
  19. /// Specifies the centered image.
  20. /// </summary>
  21. Center,
  22. /// <summary>
  23. /// Specifies the stretched image.
  24. /// </summary>
  25. Stretch,
  26. /// <summary>
  27. /// Specifies the stretched image that keeps its aspect ratio.
  28. /// </summary>
  29. Zoom,
  30. /// <summary>
  31. /// Specifies the tiled image.
  32. /// </summary>
  33. Tile
  34. }
  35. /// <summary>
  36. /// Specifies the watermark text rotation.
  37. /// </summary>
  38. public enum WatermarkTextRotation
  39. {
  40. /// <summary>
  41. /// Specifies a horizontal text.
  42. /// </summary>
  43. Horizontal,
  44. /// <summary>
  45. /// Specifies a vertical text.
  46. /// </summary>
  47. Vertical,
  48. /// <summary>
  49. /// Specifies a diagonal text.
  50. /// </summary>
  51. ForwardDiagonal,
  52. /// <summary>
  53. /// Specifies a backward diagonal text.
  54. /// </summary>
  55. BackwardDiagonal
  56. }
  57. /// <summary>
  58. /// Represents the report page watermark.
  59. /// </summary>
  60. /// <remarks>
  61. /// Watermark can draw text and/or image behind the page objects on in front of them. To enable
  62. /// watermark, set its <b>Enabled</b> property to <b>true</b>.
  63. /// </remarks>
  64. [TypeConverter(typeof(FastReport.TypeConverters.FRExpandableObjectConverter))]
  65. [EditorAttribute("FastReport.TypeEditors.WatermarkEditor, FastReport", typeof(UITypeEditor))]
  66. public class Watermark : IDisposable
  67. {
  68. #region Fields
  69. private bool enabled;
  70. private PictureObject pictureObject;
  71. private TextObject textObject;
  72. private WatermarkTextRotation textRotation;
  73. private WatermarkImageSize imageSize;
  74. private bool showImageOnTop;
  75. private bool showTextOnTop;
  76. #endregion
  77. #region Properties
  78. /// <summary>
  79. /// Gets or sets avalue indicating that watermark is enabled.
  80. /// </summary>
  81. [DefaultValue(false)]
  82. public bool Enabled
  83. {
  84. get { return enabled; }
  85. set { enabled = value; }
  86. }
  87. /// <summary>
  88. /// Gets or sets the watermark image.
  89. /// </summary>
  90. public Image Image
  91. {
  92. get { return pictureObject.Image; }
  93. set { pictureObject.Image = value; }
  94. }
  95. /// <summary>
  96. /// Gets or sets the watermark image size mode.
  97. /// </summary>
  98. [DefaultValue(WatermarkImageSize.Zoom)]
  99. public WatermarkImageSize ImageSize
  100. {
  101. get { return imageSize; }
  102. set { imageSize = value; }
  103. }
  104. /// <summary>
  105. /// Gets or sets an image transparency.
  106. /// </summary>
  107. /// <remarks>
  108. /// Valid values are 0..1. 1 means totally transparent image.
  109. /// </remarks>
  110. [DefaultValue(0f)]
  111. public float ImageTransparency
  112. {
  113. get { return pictureObject.Transparency; }
  114. set { pictureObject.Transparency = value; }
  115. }
  116. /// <summary>
  117. /// Gets or sets the watermark text.
  118. /// </summary>
  119. public string Text
  120. {
  121. get { return textObject.Text; }
  122. set { textObject.Text = value; }
  123. }
  124. /// <summary>
  125. /// Gets or sets a font of the watermark text.
  126. /// </summary>
  127. public Font Font
  128. {
  129. get { return textObject.Font; }
  130. set { textObject.Font = value; }
  131. }
  132. /// <summary>
  133. /// Gets or sets a text fill.
  134. /// </summary>
  135. [Editor("FastReport.TypeEditors.FillEditor, FastReport", typeof(UITypeEditor))]
  136. public FillBase TextFill
  137. {
  138. get { return textObject.TextFill; }
  139. set { textObject.TextFill = value; }
  140. }
  141. /// <summary>
  142. /// Gets or sets a text rotation.
  143. /// </summary>
  144. [DefaultValue(WatermarkTextRotation.ForwardDiagonal)]
  145. public WatermarkTextRotation TextRotation
  146. {
  147. get { return textRotation; }
  148. set { textRotation = value; }
  149. }
  150. /// <summary>
  151. /// Gets or sets a value indicates that the text should be displayed on top of all page objects.
  152. /// </summary>
  153. [DefaultValue(true)]
  154. public bool ShowTextOnTop
  155. {
  156. get { return showTextOnTop; }
  157. set { showTextOnTop = value; }
  158. }
  159. /// <summary>
  160. /// Gets or sets a value indicates that the image should be displayed on top of all page objects.
  161. /// </summary>
  162. [DefaultValue(false)]
  163. public bool ShowImageOnTop
  164. {
  165. get { return showImageOnTop; }
  166. set { showImageOnTop = value; }
  167. }
  168. internal TextObject TextObject
  169. {
  170. get { return textObject; }
  171. }
  172. /// <summary>
  173. ///
  174. /// </summary>
  175. public PictureObject PictureObject
  176. {
  177. get { return pictureObject; }
  178. set { pictureObject = value; }
  179. }
  180. #endregion
  181. #region Private Methods
  182. //private bool ShouldSerializeFont()
  183. //{
  184. // return Font.Name != DrawUtils.DefaultReportFont.Name || Font.Size != 60 || Font.Style != FontStyle.Regular;
  185. //}
  186. private bool ShouldSerializeTextFill()
  187. {
  188. return !(TextFill is SolidFill) || (TextFill as SolidFill).Color != Color.LightGray;
  189. }
  190. private bool ShouldSerializeImage()
  191. {
  192. return Image != null;
  193. }
  194. #endregion
  195. #region Public Methods
  196. /// <summary>
  197. /// Draws watermark image.
  198. /// </summary>
  199. /// <param name="e"></param>
  200. /// <param name="displayRect"></param>
  201. /// <param name="report"></param>
  202. /// <param name="isPrinting"></param>
  203. public virtual void DrawImage(FRPaintEventArgs e, RectangleF displayRect, Report report, bool isPrinting)
  204. {
  205. pictureObject.SetReport(report);
  206. pictureObject.Bounds = displayRect;
  207. PictureBoxSizeMode sizeMode = PictureBoxSizeMode.Normal;
  208. if (ImageSize == WatermarkImageSize.Stretch)
  209. sizeMode = PictureBoxSizeMode.StretchImage;
  210. else if (ImageSize == WatermarkImageSize.Zoom)
  211. sizeMode = PictureBoxSizeMode.Zoom;
  212. else if (ImageSize == WatermarkImageSize.Center)
  213. sizeMode = PictureBoxSizeMode.CenterImage;
  214. pictureObject.SizeMode = sizeMode;
  215. pictureObject.Tile = ImageSize == WatermarkImageSize.Tile;
  216. pictureObject.SetPrinting(isPrinting);
  217. pictureObject.DrawImage(e);
  218. }
  219. /// <summary>
  220. /// Draws watermark text.
  221. /// </summary>
  222. /// <param name="e"></param>
  223. /// <param name="displayRect"></param>
  224. /// <param name="report"></param>
  225. /// <param name="isPrinting"></param>
  226. public void DrawText(FRPaintEventArgs e, RectangleF displayRect, Report report, bool isPrinting)
  227. {
  228. textObject.SetReport(report);
  229. textObject.Bounds = displayRect;
  230. int angle = 0;
  231. switch (TextRotation)
  232. {
  233. case WatermarkTextRotation.Horizontal:
  234. angle = 0;
  235. break;
  236. case WatermarkTextRotation.Vertical:
  237. angle = 270;
  238. break;
  239. case WatermarkTextRotation.ForwardDiagonal:
  240. angle = 360 - (int)(Math.Atan(displayRect.Height / displayRect.Width) * (180 / Math.PI));
  241. break;
  242. case WatermarkTextRotation.BackwardDiagonal:
  243. angle = (int)(Math.Atan(displayRect.Height / displayRect.Width) * (180 / Math.PI));
  244. break;
  245. }
  246. textObject.Angle = angle;
  247. textObject.SetPrinting(isPrinting);
  248. textObject.DrawText(e);
  249. }
  250. /// <summary>
  251. /// Serializes the watermark.
  252. /// </summary>
  253. /// <param name="writer">Writer object.</param>
  254. /// <param name="prefix">The watermark property name.</param>
  255. /// <param name="c">Another Watermark object to compare with.</param>
  256. /// <remarks>
  257. /// This method is for internal use only.
  258. /// </remarks>
  259. public void Serialize(FRWriter writer, string prefix, Watermark c)
  260. {
  261. if (Enabled != c.Enabled)
  262. writer.WriteBool(prefix + ".Enabled", Enabled);
  263. if (!writer.AreEqual(Image, c.Image))
  264. writer.WriteValue(prefix + ".Image", Image);
  265. if (ImageSize != c.ImageSize)
  266. writer.WriteValue(prefix + ".ImageSize", ImageSize);
  267. if (ImageTransparency != c.ImageTransparency)
  268. writer.WriteFloat(prefix + ".ImageTransparency", ImageTransparency);
  269. if (Text != c.Text)
  270. writer.WriteStr(prefix + ".Text", Text);
  271. if ((writer.SerializeTo != SerializeTo.Preview || !writer.AreEqual(Font, c.Font)) && writer.ItemName != "inherited")
  272. writer.WriteValue(prefix + ".Font", Font);
  273. TextFill.Serialize(writer, prefix + ".TextFill", c.TextFill);
  274. if (TextRotation != c.TextRotation)
  275. writer.WriteValue(prefix + ".TextRotation", TextRotation);
  276. if (ShowTextOnTop != c.ShowTextOnTop)
  277. writer.WriteBool(prefix + ".ShowTextOnTop", ShowTextOnTop);
  278. if (ShowImageOnTop != c.ShowImageOnTop)
  279. writer.WriteBool(prefix + ".ShowImageOnTop", ShowImageOnTop);
  280. }
  281. /// <summary>
  282. /// Disposes resources used by the watermark.
  283. /// </summary>
  284. public void Dispose()
  285. {
  286. pictureObject.Dispose();
  287. textObject.Dispose();
  288. }
  289. /// <summary>
  290. /// Assigns values from another source.
  291. /// </summary>
  292. /// <param name="source">Source to assign from.</param>
  293. public void Assign(Watermark source)
  294. {
  295. Enabled = source.Enabled;
  296. Image = source.Image == null ? null : source.Image.Clone() as Image;
  297. ImageSize = source.ImageSize;
  298. ImageTransparency = source.ImageTransparency;
  299. Text = source.Text;
  300. Font = source.Font;
  301. TextFill = source.TextFill.Clone();
  302. TextRotation = source.TextRotation;
  303. ShowTextOnTop = source.ShowTextOnTop;
  304. ShowImageOnTop = source.ShowImageOnTop;
  305. }
  306. /// <summary>
  307. /// Creates exact copy of this <b>Watermark</b>.
  308. /// </summary>
  309. /// <returns>Copy of this watermark.</returns>
  310. public Watermark Clone()
  311. {
  312. Watermark result = new Watermark();
  313. result.Assign(this);
  314. return result;
  315. }
  316. #endregion
  317. /// <summary>
  318. /// Initializes a new instance of the <see cref="Watermark"/> class with default settings.
  319. /// </summary>
  320. public Watermark()
  321. {
  322. pictureObject = new PictureObject();
  323. textObject = new TextObject();
  324. pictureObject.ShowErrorImage = false;
  325. textObject.HorzAlign = HorzAlign.Center;
  326. textObject.VertAlign = VertAlign.Center;
  327. ImageSize = WatermarkImageSize.Zoom;
  328. Font = new Font(DrawUtils.DefaultReportFont.Name, 60);
  329. TextFill = new SolidFill(Color.FromArgb(40, Color.Gray));
  330. TextRotation = WatermarkTextRotation.ForwardDiagonal;
  331. ShowTextOnTop = true;
  332. }
  333. }
  334. }