AllBlendModes.cs
  1. //
  2. // This code is part of Document Solutions for PDF demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Linq;
  9. using GrapeCity.Documents.Pdf;
  10. using GrapeCity.Documents.Text;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Imaging;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsPdfWeb.Demos
  17. {
  18. // This sample demonstrates the effects of different blend modes
  19. // when drawing on a GcPdfGraphics. Note that the current blend mode
  20. // set on an instance of GcGraphics (including GcPdfGraphics)
  21. // affects not only images but all drawing operations.
  22. // In this sample it is demonstrated by rendering the blend mode
  23. // names on the second page.
  24. public class AllBlendModes
  25. {
  26. public int CreatePDF(Stream stream)
  27. {
  28. var doc = new GcPdfDocument();
  29. var page = doc.NewPage();
  30. var g = page.Graphics;
  31.  
  32. var iorchid = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "orchid.jpg"));
  33. var ispectr = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png"));
  34.  
  35. const int margin = 36;
  36. const int NCOLS = 4;
  37. var w = (int)((page.Size.Width - margin * 2) / NCOLS);
  38. var h = (int)((iorchid.Height * w) / iorchid.Width);
  39.  
  40. // Text layout for captions:
  41. var tl = g.CreateTextLayout();
  42. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  43. tl.DefaultFormat.FontSize = 12;
  44. tl.ParagraphAlignment = ParagraphAlignment.Center;
  45. tl.TextAlignment = TextAlignment.Center;
  46. tl.MaxWidth = w;
  47. tl.MaxHeight = h;
  48. tl.MarginTop = h - g.MeasureString("QWERTY", tl.DefaultFormat).Height * 1.4f;
  49.  
  50. int row = 0, col, dy;
  51. // Render all blending modes in a grid:
  52. var modes = Enum.GetValues(typeof(BlendMode));
  53. for (int i = 0; i < 2; ++i)
  54. {
  55. row = col = 0;
  56. GCDRAW.Image iback, ifore;
  57. if (i == 0)
  58. {
  59. iback = ispectr;
  60. ifore = iorchid;
  61. var trc1 = Common.Util.AddNote(
  62. "Below are tiles drawn by composing two images (a spectrum and an orchid) using the " +
  63. "different blend modes supported by DsPdf. " +
  64. "First the spectrum image is drawn normally, then it is overlaid by the orchid image " +
  65. "drawn in a supported blend mode. The blend mode name is shown below each tile." +
  66. "\n" +
  67. "Tiles on the second page are composed of the same images but drawn in reverse order: " +
  68. "the orchid is drawn normally, the spectrum is blended with it using the different modes." +
  69. "\n" +
  70. "Note that the current blend mode affects not only images but any drawing on a GcGraphics. " +
  71. "The blend mode names on the 2nd page illustrate this.",
  72. page);
  73. dy = (int)trc1.Bottom;
  74. }
  75. else // i == 1
  76. {
  77. iback = iorchid;
  78. ifore = ispectr;
  79. page = doc.Pages.Add();
  80. g = page.Graphics;
  81. dy = 0;
  82. }
  83. foreach (var mode in modes)
  84. {
  85. var blendMode = (BlendMode)mode;
  86. if (!g.IsBlendModeSupported(blendMode))
  87. continue;
  88.  
  89. int x = margin + w * col;
  90. int y = margin + h * row + dy;
  91. var r = new RectangleF(x, y, w, h);
  92.  
  93. g.BlendMode = BlendMode.Normal;
  94. g.DrawImage(iback, r, null, ImageAlign.StretchImage);
  95. g.BlendMode = blendMode;
  96. g.DrawImage(ifore, r, null, ImageAlign.StretchImage);
  97. g.BlendMode = BlendMode.Normal;
  98.  
  99. // Caption:
  100. tl.Clear();
  101. tl.Append(blendMode.ToString());
  102. tl.PerformLayout(true);
  103. var rc = tl.ContentRectangle;
  104. rc.Offset(x, y);
  105. rc.Inflate(4, 2);
  106. g.FillRectangle(rc, Color.White);
  107. g.DrawTextLayout(tl, new PointF(x, y));
  108. nextRowCol();
  109. }
  110. }
  111. // Text blends:
  112. page = doc.Pages[1];
  113. g = page.Graphics;
  114. var trc = Common.Util.AddNote(
  115. "Below are supported blend mode names rendered into rectangles using the corresponding blend mode. " +
  116. "Each rectangle is first filled by the spectrum image using BlendMode.Normal, " +
  117. "then black and white texts are drawn using the current blend mode, " +
  118. "finally the same spectrum image is drawn using BlendMode.Difference to black out the background. " +
  119. "Names prefixed with 'B:' are drawn in black, " +
  120. "while names prefixed with 'W:' are drawn in white. " +
  121. "Most combinations yield rather colorful results, but for some the results are invisible.",
  122. page,
  123. new RectangleF(margin, margin + h * row + 12, page.Bounds.Width - margin * 2, 0));
  124.  
  125. var yoff = (int)trc.Bottom + 12;
  126. tl.DefaultFormat.FontBold = true;
  127. tl.DefaultFormat.FontSize = 16;
  128. h = (int)(g.MeasureString("QWERTY", tl.DefaultFormat).Height * 1.4f) * 2;
  129. tl.MaxWidth = w;
  130. tl.MaxHeight = h;
  131. tl.MarginTop = (h - h / 1.4f) / 2;
  132.  
  133. tl.DefaultFormat.ForeColor = Color.Black;
  134. var tfWhite = new TextFormat(tl.DefaultFormat) { ForeColor = Color.White };
  135.  
  136. row = col = 0;
  137. foreach (var mode in modes)
  138. {
  139. var blendMode = (BlendMode)mode;
  140. if (!g.IsBlendModeSupported(blendMode))
  141. continue;
  142.  
  143. int x = margin + w * col;
  144. int y = yoff + h * row;
  145. var r = new RectangleF(x, y, w, h);
  146.  
  147. // Draw spectrum image normally:
  148. g.BlendMode = BlendMode.Normal;
  149. g.DrawImage(ispectr, r, null, ImageAlign.StretchImage);
  150. // Draw blend mode name using the current blend mode:
  151. tl.Clear();
  152. tl.AppendLine($"B: {blendMode}");
  153. tl.Append($"W: {blendMode}", tfWhite);
  154. tl.PerformLayout(true);
  155. var rc = tl.ContentRectangle;
  156. rc.Offset(x, y);
  157. rc.Inflate(4, 2);
  158. // Current blend mode:
  159. g.BlendMode = blendMode;
  160. g.DrawTextLayout(tl, new PointF(x, y));
  161. // Draw spectrum image again using BlendMode.Difference
  162. // to produce (mostly) colorful text on black background:
  163. g.BlendMode = BlendMode.Difference;
  164. g.DrawImage(ispectr, r, null, ImageAlign.StretchImage);
  165. // Draw a rectangle to mark the current area:
  166. g.BlendMode = BlendMode.Normal;
  167. g.DrawRectangle(r, Color.Gray);
  168. nextRowCol();
  169. }
  170. // Done:
  171. doc.Save(stream);
  172. return doc.Pages.Count;
  173.  
  174. //
  175. void nextRowCol()
  176. {
  177. if (++col == NCOLS)
  178. {
  179. col = 0;
  180. ++row;
  181. }
  182. }
  183. }
  184. }
  185. }
  186.