RenderArgs.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Drawing;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. namespace SmartCoalApplication.Core
  8. {
  9. public sealed class RenderArgs
  10. : IDisposable
  11. {
  12. private Surface surface;
  13. private Bitmap bitmap;
  14. private Graphics graphics;
  15. private bool disposed = false;
  16. public Surface Surface
  17. {
  18. get
  19. {
  20. if (this.disposed)
  21. {
  22. throw new ObjectDisposedException("RenderArgs");
  23. }
  24. return this.surface;
  25. }
  26. }
  27. /// <summary>
  28. /// Gets a Bitmap reference that aliases the Surface.
  29. /// </summary>
  30. public Bitmap Bitmap
  31. {
  32. get
  33. {
  34. if (this.disposed)
  35. {
  36. throw new ObjectDisposedException("RenderArgs");
  37. }
  38. if (this.bitmap == null)
  39. {
  40. this.bitmap = surface.CreateAliasedBitmap();
  41. }
  42. return this.bitmap;
  43. }
  44. }
  45. /// <summary>
  46. /// Retrieves a Graphics instance that can be used to draw on to the Surface.
  47. /// </summary>
  48. /// <remarks>
  49. /// Use of this object is not thread-safe. You must wrap retrieval and consumption of this
  50. /// property with a critical section.
  51. /// </remarks>
  52. public Graphics Graphics
  53. {
  54. get
  55. {
  56. if (this.disposed)
  57. {
  58. throw new ObjectDisposedException("RenderArgs");
  59. }
  60. if (this.graphics == null)
  61. {
  62. this.graphics = Graphics.FromImage(Bitmap);
  63. }
  64. return this.graphics;
  65. }
  66. }
  67. /// <summary>
  68. /// Gets the size of the associated Surface object.
  69. /// </summary>
  70. /// <remarks>
  71. /// This is a convenience method equivalent to using RenderArgs.Surface.Bounds.
  72. /// </remarks>
  73. public Rectangle Bounds
  74. {
  75. get
  76. {
  77. if (this.disposed)
  78. {
  79. throw new ObjectDisposedException("RenderArgs");
  80. }
  81. return this.Surface.Bounds;
  82. }
  83. }
  84. /// <summary>
  85. /// Gets the size of the associated Surface object.
  86. /// </summary>
  87. /// <remarks>
  88. /// This is a convenient method equivalent to using RenderArgs.Surface.Size.
  89. /// </remarks>
  90. public Size Size
  91. {
  92. get
  93. {
  94. if (this.disposed)
  95. {
  96. throw new ObjectDisposedException("RenderArgs");
  97. }
  98. return this.Surface.Size;
  99. }
  100. }
  101. /// <summary>
  102. /// Gets the width of the associated Surface object.
  103. /// </summary>
  104. /// <remarks>
  105. /// This is a convenience method equivalent to using RenderArgs.Surface.Width.
  106. /// </remarks>
  107. public int Width
  108. {
  109. get
  110. {
  111. if (this.disposed)
  112. {
  113. throw new ObjectDisposedException("RenderArgs");
  114. }
  115. return this.surface.Width;
  116. }
  117. }
  118. /// <summary>
  119. /// Gets the height of the associated Surface object.
  120. /// </summary>
  121. /// <remarks>
  122. /// This is a convenience method equivalent to using RenderArgs.Surface.Height.
  123. /// </remarks>
  124. public int Height
  125. {
  126. get
  127. {
  128. if (this.disposed)
  129. {
  130. throw new ObjectDisposedException("RenderArgs");
  131. }
  132. return this.surface.Height;
  133. }
  134. }
  135. /// <summary>
  136. /// Creates an instance of the RenderArgs class.
  137. /// </summary>
  138. /// <param name="surface">
  139. /// The Surface to associate with this instance. This instance of RenderArgs does not
  140. /// take ownership of this Surface.
  141. /// </param>
  142. public RenderArgs(Surface surface)
  143. {
  144. this.surface = surface;
  145. this.bitmap = null;
  146. this.graphics = null;
  147. }
  148. ~RenderArgs()
  149. {
  150. Dispose(false);
  151. }
  152. /// <summary>
  153. /// Disposes of the contained Bitmap and Graphics instances, if necessary.
  154. /// </summary>
  155. /// <remarks>
  156. /// Note that since this class does not take ownership of the Surface, it
  157. /// is not disposed.
  158. /// </remarks>
  159. public void Dispose()
  160. {
  161. Dispose(true);
  162. GC.SuppressFinalize(this);
  163. }
  164. private void Dispose(bool disposing)
  165. {
  166. if (!this.disposed)
  167. {
  168. this.disposed = true;
  169. if (disposing)
  170. {
  171. if (this.graphics != null)
  172. {
  173. this.graphics.Dispose();
  174. this.graphics = null;
  175. }
  176. if (this.bitmap != null)
  177. {
  178. this.bitmap.Dispose();
  179. this.bitmap = null;
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }