thorpedosg

DMs9RJw9

Aug 6th, 2018
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.89 KB | None | 0 0
  1. Interface implementation in to my project
  2. public interface IElementCreator
  3. {
  4. int posX { get; set; }
  5. int posY { get; set; }
  6. StringFormat format { get; set; }
  7. string value { get; set; }
  8. Font font { get; set; }
  9. bool caption { get; set; }
  10. Font captionFont { get; set; }
  11. }
  12.  
  13. public class ElementBase : IElementCreator, IImageUpdater
  14. {
  15. #region IElementCreator Variables
  16.  
  17. private int _posX;
  18. private int _posY;
  19. private StringFormat _format;
  20. private string _value;
  21. private Font _font;
  22. private bool _caption;
  23. private Font _captionFont;
  24.  
  25. #endregion
  26.  
  27. #region IElementCreator Members
  28.  
  29. public int posX
  30. {
  31. get { return _posX; }
  32. set { _posX = value; }
  33. }
  34.  
  35. public int posY
  36. {
  37. get { return _posY; }
  38. set { _posY = value; }
  39. }
  40.  
  41. public StringFormat format
  42. {
  43. get { return _format; }
  44. set { _format = value; }
  45. }
  46.  
  47. public string value
  48. {
  49. get { return _value; }
  50. set { _value = value; }
  51. }
  52.  
  53. public Font font
  54. {
  55. get { return _font; }
  56. set { _font = value; }
  57. }
  58.  
  59. public bool caption
  60. {
  61. get { return _caption; }
  62. set { _caption = value; }
  63. }
  64.  
  65. public Font captionFont
  66. {
  67. get { return _captionFont; }
  68. set { _captionFont = value; }
  69. }
  70.  
  71. #endregion
  72.  
  73. #region Constructor
  74.  
  75. public ElementBase(int posX, int posY, StringFormat format, string value, Font font, bool caption, Font captionFont)
  76. {
  77. this.posX = posX;
  78. this.posY = posY;
  79. this.format = format;
  80. this.value = value;
  81. this.font = font;
  82. this.caption = caption;
  83. this.captionFont = captionFont;
  84. }
  85.  
  86. #endregion
  87.  
  88. public Image updatePreview(IElementCreator element, Image img)
  89. {
  90. }
  91. }
  92.  
  93. public interface IImageUpdater
  94. {
  95. Image updatePreview(IElementCreator element, Image img);
  96. }
  97.  
  98. public class ImageCreator : IImageCreator
  99. {
  100. public Image CreatePreviewImg(string value, int fontSize, int x, int y)
  101. {
  102. //Your code goes here...
  103. }
  104. }
  105.  
  106. IImageCreator imageCreator = new ImageCreator;
  107. var preview = imageCreator.CreatePreviewImg(...);
  108.  
  109. public interface IImageCreator
  110. {
  111. public Image CreatePreviewImg(string value, int fontSize, int x, int y);
  112. }
  113.  
  114. public class ImageSettings : IImageSettings
  115. {
  116. public int posX { get; set; }
  117. public int posY { get; set; }
  118. public StringFormat format { get; set; }
  119. public string value { get; set; }
  120. public Font font { get; set; }
  121. public bool caption { get; set; }
  122. public Font captionFont { get; set; }
  123. }
  124.  
  125. public interface IImageSettings
  126. {
  127. int posX { get; set; }
  128. int posY { get; set; }
  129. StringFormat format { get; set; }
  130. string value { get; set; }
  131. Font font { get; set; }
  132. bool caption { get; set; }
  133. Font captionFont { get; set; }
  134. }
  135.  
  136.  
  137. public class ImageCreator : IImageCreator
  138. {
  139.  
  140. public Image CreatePreviewImg(IImageSettings imageSettings)
  141. {
  142. //ALL THE MAGIC HAPPENS HERE
  143. }
  144.  
  145. //Image size A4 at 300dpi
  146. Bitmap bm = new Bitmap(width, height);
  147. bm.SetResolution(300, 300);
  148.  
  149. // Create rectangle for the canvas
  150. RectangleF rectBg = new RectangleF(0, 0, width, height);
  151.  
  152. // Load fonts
  153. Font bcFont = new Font(private_fonts.Families[0], fontSize, FontStyle.Regular, GraphicsUnit.Point);
  154. Font valueFont = new Font("Tahoma", 12, FontStyle.Regular, GraphicsUnit.Point);
  155.  
  156. // Set StringFormat for the text value
  157. StringFormat sfValue = new StringFormat();
  158. sfValue.LineAlignment = StringAlignment.Far;
  159. sfValue.Alignment = StringAlignment.Center;
  160.  
  161. // Set StringFormat for the barcode string
  162. StringFormat sfBarcode = StringFormat.GenericTypographic;
  163. sfBarcode.FormatFlags = StringFormatFlags.NoClip;
  164.  
  165. using (Graphics g = Graphics.FromImage(bm))
  166. {
  167. // Create rectangle to place the barcode and text
  168. SizeF bcStringSize = g.MeasureString("*" + value + "*", bcFont);
  169. SizeF valueSize = g.MeasureString(value, valueFont);
  170. RectangleF recText = new RectangleF(x, y, (float)MeasureDisplayStringWidth(g, "*" + value + "*", bcFont), (float)(bcStringSize.Height + valueSize.Height));
  171.  
  172. // Drawing the barcode and text
  173. g.SmoothingMode = SmoothingMode.AntiAlias;
  174. g.TextRenderingHint = TextRenderingHint.AntiAlias;
  175. g.FillRectangle(new SolidBrush(Color.White), rectBg);
  176. g.DrawString("*" + value + "*", bcFont, new SolidBrush(Color.Black), recText, sfBarcode);
  177.  
  178. g.DrawString(value, valueFont, new SolidBrush(Color.Black), recText, sfValue);
  179. }
  180.  
  181. return bm;
  182. }
  183. }
Add Comment
Please, Sign In to add comment