BlendText.cs
  1. //
  2. // This code is part of Document Solutions for Imaging demos.
  3. // Copyright (c) MESCIUS inc. All rights reserved.
  4. //
  5. using System;
  6. using System.IO;
  7. using System.Drawing;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Numerics;
  11. using GrapeCity.Documents.Drawing;
  12. using GrapeCity.Documents.Text;
  13. using GrapeCity.Documents.Imaging;
  14. using GCTEXT = GrapeCity.Documents.Text;
  15. using GCDRAW = GrapeCity.Documents.Drawing;
  16.  
  17. namespace DsImagingWeb.Demos
  18. {
  19. // This sample demonstrates the effects of different blend modes
  20. // when drawing on a GcGraphics. Note that the current blend mode
  21. // set on an instance of GcGraphics (including GcBitmapGraphics)
  22. // affects not only images but all drawing operations.
  23. // See also BlendImages_0 and BlendingModes samples.
  24. public class BlendText
  25. {
  26. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] _ = null)
  27. {
  28. var ispectr = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-500x500.png"));
  29. const int margin = 36;
  30. const int NCOLS = 4;
  31. var w = (int)((pixelSize.Width - margin * 2) / NCOLS);
  32. var h = w / 2;
  33. int row = 0, col = 0;
  34. float bottomy = 0;
  35.  
  36. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  37. using (var g = bmp.CreateGraphics(Color.White))
  38. {
  39. // Text layout for captions:
  40. var tl = g.CreateTextLayout();
  41. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "courbd.ttf"));
  42. tl.DefaultFormat.FontSize = 22;
  43. tl.ParagraphAlignment = ParagraphAlignment.Center;
  44. tl.TextAlignment = TextAlignment.Center;
  45. tl.MaxWidth = w;
  46. tl.MaxHeight = h;
  47. tl.MarginTop = (h - h / 1.4f) / 2;
  48. tl.DefaultFormat.ForeColor = Color.Black;
  49. var tfWhite = new TextFormat(tl.DefaultFormat) { ForeColor = Color.White };
  50.  
  51. // Render all blend modes in a grid:
  52. var modes = Enum.GetValues(typeof(BlendMode));
  53. foreach (var mode in modes)
  54. {
  55. var blendMode = (BlendMode)mode;
  56. if (!g.IsBlendModeSupported(blendMode))
  57. continue;
  58.  
  59. int x = margin + w * col;
  60. int y = margin + h * row;
  61. var r = new RectangleF(x, y, w, h);
  62.  
  63. // Draw spectrum image normally:
  64. g.BlendMode = BlendMode.Normal;
  65. g.DrawImage(ispectr, r, null, ImageAlign.StretchImage);
  66. // Draw blend mode name using the current blend mode:
  67. tl.Clear();
  68. tl.AppendLine($"B: {blendMode}");
  69. tl.Append($"W: {blendMode}", tfWhite);
  70. tl.PerformLayout(true);
  71. var rc = tl.ContentRectangle;
  72. rc.Offset(x, y);
  73. rc.Inflate(4, 2);
  74. // Current blend mode:
  75. g.BlendMode = blendMode;
  76. g.DrawTextLayout(tl, new PointF(x, y));
  77. // Draw spectrum image again using BlendMode.Difference
  78. // to produce (mostly) colorful text on black background:
  79. g.BlendMode = BlendMode.Difference;
  80. g.DrawImage(ispectr, r, null, ImageAlign.StretchImage);
  81. // Draw a rectangle to mark the current area:
  82. g.BlendMode = BlendMode.Normal;
  83. g.DrawRectangle(r, Color.Gray);
  84. bottomy = r.Bottom;
  85. if (++col == NCOLS)
  86. {
  87. col = 0;
  88. ++row;
  89. }
  90. }
  91. // For reference, draw the backdrop:
  92. tl.Clear();
  93. tl.MarginAll = 0;
  94. tl.MaxWidth = bmp.PixelWidth;
  95. tl.MaxHeight = null;
  96. tl.DefaultFormat.FontSize = 14;
  97. tl.TextAlignment = TextAlignment.Leading;
  98. tl.ParagraphAlignment = ParagraphAlignment.Near;
  99. tl.Append("The spectrum image used as the backdrop for the texts above:");
  100. g.DrawTextLayout(tl, new PointF(margin, bottomy + margin));
  101. g.DrawImage(ispectr, new RectangleF(margin, bottomy + margin + tl.ContentHeight + 24, w, w), null, ImageAlign.StretchImage);
  102. }
  103. return bmp;
  104. }
  105. }
  106. }
  107.