GdiGraphics.cs 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  1. using System;
  2. using System.Drawing;
  3. using System.Drawing.Drawing2D;
  4. using System.Drawing.Imaging;
  5. using System.Drawing.Text;
  6. namespace FastReport
  7. {
  8. /// <summary>
  9. /// Drawing objects to a standard Graphics or Bitmap
  10. /// </summary>
  11. public class GdiGraphics : IGraphics
  12. {
  13. private Graphics graphics;
  14. private readonly bool haveToDispose;
  15. #region Properties
  16. public Graphics Graphics
  17. {
  18. get { return graphics; }
  19. }
  20. float IGraphics.DpiX => this.graphics.DpiX;
  21. float IGraphics.DpiY => this.graphics.DpiY;
  22. TextRenderingHint IGraphics.TextRenderingHint { get => this.graphics.TextRenderingHint; set => this.graphics.TextRenderingHint = value; }
  23. InterpolationMode IGraphics.InterpolationMode { get => this.graphics.InterpolationMode; set => this.graphics.InterpolationMode = value; }
  24. SmoothingMode IGraphics.SmoothingMode { get => this.graphics.SmoothingMode; set => this.graphics.SmoothingMode = value; }
  25. System.Drawing.Drawing2D.Matrix IGraphics.Transform { get => this.graphics.Transform; set => this.graphics.Transform = value; }
  26. GraphicsUnit IGraphics.PageUnit { get => this.graphics.PageUnit; set => this.graphics.PageUnit = value; }
  27. bool IGraphics.IsClipEmpty => this.graphics.IsClipEmpty;
  28. Region IGraphics.Clip { get => this.graphics.Clip; set => this.graphics.Clip = value; }
  29. CompositingQuality IGraphics.CompositingQuality { get => this.graphics.CompositingQuality; set => this.graphics.CompositingQuality = value; }
  30. #endregion
  31. public GdiGraphics(Image image)
  32. : this(Graphics.FromImage(image), true)
  33. {
  34. }
  35. public GdiGraphics(Graphics graphics, bool haveToDispose)
  36. {
  37. this.graphics = graphics;
  38. this.haveToDispose = haveToDispose;
  39. }
  40. #region IDisposable Support
  41. private bool disposedValue = false; // To detect redundant calls
  42. protected virtual void Dispose(bool disposing)
  43. {
  44. if (!disposedValue)
  45. {
  46. if (disposing)
  47. {
  48. if (graphics != null && haveToDispose)
  49. graphics.Dispose();
  50. graphics = null;
  51. // TODO: dispose managed state (managed objects).
  52. }
  53. // TODO: free unmanaged resources (unmanaged objects) and override a finalizer below.
  54. // TODO: set large fields to null.
  55. disposedValue = true;
  56. }
  57. }
  58. // TODO: override a finalizer only if Dispose(bool disposing) above has code to free unmanaged resources.
  59. // ~ImageGraphicsRenderer() {
  60. // // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  61. // Dispose(false);
  62. // }
  63. // This code added to correctly implement the disposable pattern.
  64. public void Dispose()
  65. {
  66. // Do not change this code. Put cleanup code in Dispose(bool disposing) above.
  67. Dispose(true);
  68. // TODO: uncomment the following line if the finalizer is overridden above.
  69. // GC.SuppressFinalize(this);
  70. }
  71. #endregion
  72. #region Draw and measure text
  73. public void DrawString(string text, Font drawFont, Brush brush, float left, float top)
  74. {
  75. this.graphics.DrawString(text, drawFont, brush, left, top);
  76. }
  77. public void DrawString(string text, Font drawFont, Brush brush, RectangleF rectangleF)
  78. {
  79. this.graphics.DrawString(text, drawFont, brush, rectangleF);
  80. }
  81. public void DrawString(string text, Font font, Brush textBrush, RectangleF textRect, StringFormat format)
  82. {
  83. this.graphics.DrawString(text, font, textBrush, textRect, format);
  84. }
  85. public void DrawString(string text, Font font, Brush brush, float left, float top, StringFormat format)
  86. {
  87. this.graphics.DrawString(text, font, brush, left, top, format);
  88. }
  89. void IGraphics.DrawString(string s, Font font, Brush brush, PointF point, StringFormat format)
  90. {
  91. this.graphics.DrawString(s, font, brush, point, format);
  92. }
  93. public Region[] MeasureCharacterRanges(string text, Font font, RectangleF rect, StringFormat format)
  94. {
  95. return this.graphics.MeasureCharacterRanges(text, font, rect, format);
  96. }
  97. public SizeF MeasureString(string text, Font font, SizeF size)
  98. {
  99. return this.graphics.MeasureString(text, font, size);
  100. }
  101. public SizeF MeasureString(string text, Font font, int width, StringFormat format)
  102. {
  103. return this.graphics.MeasureString(text, font, width, format);
  104. }
  105. public void MeasureString(string text, Font font, SizeF size, StringFormat format, out int charsFit, out int linesFit)
  106. {
  107. this.graphics.MeasureString(text, font, size, format, out charsFit, out linesFit);
  108. }
  109. public SizeF MeasureString(string text, Font drawFont)
  110. {
  111. return this.graphics.MeasureString(text, drawFont);
  112. }
  113. public SizeF MeasureString(string text, Font font, SizeF layoutArea, StringFormat format)
  114. {
  115. return this.graphics.MeasureString(text, font, layoutArea, format);
  116. }
  117. #endregion
  118. #region Draw images
  119. public void DrawImage(Image image, float x, float y)
  120. {
  121. this.graphics.DrawImage(image, x, y);
  122. }
  123. public void DrawImage(Image image, RectangleF destRect, RectangleF srcRect, GraphicsUnit unit)
  124. {
  125. this.graphics.DrawImage(image, destRect, srcRect, unit);
  126. }
  127. public void DrawImage(Image image, RectangleF rect)
  128. {
  129. this.graphics.DrawImage(image, rect);
  130. }
  131. public void DrawImage(Image image, float x, float y, float width, float height)
  132. {
  133. this.graphics.DrawImage(image, x, y, width, height);
  134. }
  135. public void DrawImage(Image image, PointF[] points)
  136. {
  137. this.graphics.DrawImage(image, points);
  138. }
  139. public void DrawImage(Image image, Rectangle destRect, int srcX, int srcY, int srcWidth, int srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttr)
  140. {
  141. this.graphics.DrawImage(image, destRect, srcX, srcY, srcWidth, srcHeight, srcUnit, imageAttr);
  142. }
  143. public void DrawImage(Image image, Rectangle destRect, float srcX, float srcY, float srcWidth, float srcHeight, GraphicsUnit srcUnit, ImageAttributes imageAttrs)
  144. {
  145. this.graphics.DrawImage(image, destRect, srcX, srcY, srcWidth, srcHeight, srcUnit, imageAttrs);
  146. }
  147. public void DrawImageUnscaled(Image image, Rectangle rect)
  148. {
  149. this.graphics.DrawImageUnscaled(image, rect);
  150. }
  151. #endregion
  152. #region Draw geometry
  153. public void DrawArc(Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle)
  154. {
  155. this.graphics.DrawArc(pen, x, y, width, height, startAngle, sweepAngle);
  156. }
  157. public void DrawCurve(Pen pen, PointF[] points, int offset, int numberOfSegments, float tension)
  158. {
  159. this.graphics.DrawCurve(pen, points, offset, numberOfSegments, tension);
  160. }
  161. public void DrawEllipse(Pen pen, float left, float top, float width, float height)
  162. {
  163. this.graphics.DrawEllipse(pen, left, top, width, height);
  164. }
  165. public void DrawEllipse(Pen pen, RectangleF rect)
  166. {
  167. this.graphics.DrawEllipse(pen, rect);
  168. }
  169. public void DrawLine(Pen pen, float x1, float y1, float x2, float y2)
  170. {
  171. this.graphics.DrawLine(pen, x1, y1, x2, y2);
  172. }
  173. public void DrawLine(Pen pen, PointF p1, PointF p2)
  174. {
  175. this.graphics.DrawLine(pen, p1, p2);
  176. }
  177. public void DrawLines(Pen pen, PointF[] points)
  178. {
  179. this.graphics.DrawLines(pen, points);
  180. }
  181. public void DrawPath(Pen outlinePen, GraphicsPath path)
  182. {
  183. this.graphics.DrawPath(outlinePen, path);
  184. }
  185. public void DrawPie(Pen pen, float x, float y, float width, float height, float startAngle, float sweepAngle)
  186. {
  187. this.graphics.DrawPie(pen, x, y, width, height, startAngle, sweepAngle);
  188. }
  189. public void DrawPolygon(Pen pen, PointF[] points)
  190. {
  191. this.graphics.DrawPolygon(pen, points);
  192. }
  193. public void DrawPolygon(Pen pen, Point[] points)
  194. {
  195. this.graphics.DrawPolygon(pen, points);
  196. }
  197. public void DrawRectangle(Pen pen, float left, float top, float width, float height)
  198. {
  199. this.graphics.DrawRectangle(pen, left, top, width, height);
  200. }
  201. public void DrawRectangle(Pen pen, Rectangle rect)
  202. {
  203. this.graphics.DrawRectangle(pen, rect);
  204. }
  205. public void PathAddRectangle(GraphicsPath path, RectangleF rect)
  206. {
  207. path.AddRectangle(rect);
  208. }
  209. #endregion
  210. #region Fill geometry
  211. public void FillEllipse(Brush brush, float x, float y, float dx, float dy)
  212. {
  213. this.graphics.FillEllipse(brush, x, y, dx, dy);
  214. }
  215. public void FillEllipse(Brush brush, RectangleF rect)
  216. {
  217. this.graphics.FillEllipse(brush, rect);
  218. }
  219. public void FillPath(Brush brush, GraphicsPath path)
  220. {
  221. this.graphics.FillPath(brush, path);
  222. }
  223. public void FillPie(Brush brush, float x, float y, float width, float height, float startAngle, float sweepAngle)
  224. {
  225. this.graphics.FillPie(brush, x, y, width, height, startAngle, sweepAngle);
  226. }
  227. public void FillPolygon(Brush brush, PointF[] points)
  228. {
  229. this.graphics.FillPolygon(brush, points);
  230. }
  231. public void FillPolygon(Brush brush, Point[] points)
  232. {
  233. this.graphics.FillPolygon(brush, points);
  234. }
  235. public void FillRectangle(Brush brush, RectangleF rect)
  236. {
  237. this.graphics.FillRectangle(brush, rect);
  238. }
  239. public void FillRectangle(Brush brush, float left, float top, float width, float height)
  240. {
  241. this.graphics.FillRectangle(brush, left, top, width, height);
  242. }
  243. public void FillRegion(Brush brush, Region region)
  244. {
  245. this.graphics.FillRegion(brush, region);
  246. }
  247. #endregion
  248. #region Fill And Draw
  249. public void FillAndDrawPath(Pen pen, Brush brush, GraphicsPath path)
  250. {
  251. FillPath(brush, path);
  252. DrawPath(pen, path);
  253. }
  254. public void FillAndDrawEllipse(Pen pen, Brush brush, RectangleF rect)
  255. {
  256. FillEllipse(brush, rect);
  257. DrawEllipse(pen, rect);
  258. }
  259. public void FillAndDrawEllipse(Pen pen, Brush brush, float left, float top, float width, float height)
  260. {
  261. FillEllipse(brush, left, top, width, height);
  262. DrawEllipse(pen, left, top, width, height);
  263. }
  264. public void FillAndDrawPolygon(Pen pen, Brush brush, Point[] points)
  265. {
  266. FillPolygon(brush, points);
  267. DrawPolygon(pen, points);
  268. }
  269. public void FillAndDrawPolygon(Pen pen, Brush brush, PointF[] points)
  270. {
  271. FillPolygon(brush, points);
  272. DrawPolygon(pen, points);
  273. }
  274. public void FillAndDrawRectangle(Pen pen, Brush brush, float left, float top, float width, float height)
  275. {
  276. FillRectangle(brush, left, top, width, height);
  277. DrawRectangle(pen, left, top, width, height);
  278. }
  279. #endregion
  280. #region Transform
  281. public void MultiplyTransform(System.Drawing.Drawing2D.Matrix matrix, MatrixOrder order)
  282. {
  283. this.graphics.MultiplyTransform(matrix, order);
  284. }
  285. public void RotateTransform(float angle)
  286. {
  287. this.graphics.RotateTransform(angle);
  288. }
  289. public void ScaleTransform(float scaleX, float scaleY)
  290. {
  291. this.graphics.ScaleTransform(scaleX, scaleY);
  292. }
  293. public void TranslateTransform(float left, float top)
  294. {
  295. this.graphics.TranslateTransform(left, top);
  296. }
  297. #endregion
  298. #region State
  299. public void Restore(IGraphicsState state)
  300. {
  301. if (state is ImageGraphicsRendererState)
  302. this.graphics.Restore((state as ImageGraphicsRendererState).GraphicsState);
  303. }
  304. public IGraphicsState Save()
  305. {
  306. return new ImageGraphicsRendererState(this.graphics.Save());
  307. }
  308. #endregion
  309. #region Clip
  310. public bool IsVisible(RectangleF rect)
  311. {
  312. return this.graphics.IsVisible(rect);
  313. }
  314. public void ResetClip()
  315. {
  316. this.graphics.ResetClip();
  317. }
  318. public void SetClip(RectangleF rect)
  319. {
  320. this.graphics.SetClip(rect);
  321. }
  322. public void SetClip(RectangleF rect, CombineMode combineMode)
  323. {
  324. this.graphics.SetClip(rect, combineMode);
  325. }
  326. public void SetClip(GraphicsPath path, CombineMode combineMode)
  327. {
  328. this.graphics.SetClip(path, combineMode);
  329. }
  330. #endregion
  331. public class ImageGraphicsRendererState : IGraphicsState
  332. {
  333. private readonly GraphicsState graphicsState;
  334. public GraphicsState GraphicsState
  335. {
  336. get
  337. {
  338. return graphicsState;
  339. }
  340. }
  341. public ImageGraphicsRendererState(GraphicsState state)
  342. {
  343. this.graphicsState = state;
  344. }
  345. }
  346. public static GdiGraphics FromImage(Image image)
  347. {
  348. return new GdiGraphics(image);
  349. }
  350. public static GdiGraphics FromGraphics(Graphics graphics)
  351. {
  352. return new GdiGraphics(graphics, false);
  353. }
  354. public static GdiGraphics FromHdc(IntPtr hdc)
  355. {
  356. return FromGraphics(Graphics.FromHdc(hdc));
  357. }
  358. }
  359. }