OutlinedText.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 GrapeCity.Documents.Drawing;
  11. using GrapeCity.Documents.Text;
  12. using GrapeCity.Documents.Imaging;
  13. using GCTEXT = GrapeCity.Documents.Text;
  14. using GCDRAW = GrapeCity.Documents.Drawing;
  15.  
  16. namespace DsImagingWeb.Demos
  17. {
  18. // Creating outlined text
  19. public class OutlinedText
  20. {
  21. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  22. {
  23. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  24. using (var g = bmp.CreateGraphics(Color.White))
  25. {
  26. var tl = g.CreateTextLayout();
  27.  
  28. // Set up text layout:
  29. tl.MaxWidth = g.Width;
  30. tl.MaxHeight = g.Height;
  31. tl.MarginAll = g.Resolution / 2;
  32. const float fontSize = 64;
  33.  
  34. var rcBack = new RectangleF(0, 0, pixelSize.Width, pixelSize.Height);
  35. g.DrawImage(GCDRAW.Image.FromFile(Path.Combine("Resources", "Images", "purples.jpg")), rcBack, null, ImageAlign.StretchImage);
  36.  
  37. TextFormat tf0 = new TextFormat()
  38. {
  39. ForeColor = Color.LemonChiffon,
  40. Hollow = true,
  41. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "GOTHICB.TTF")),
  42. FontSize = fontSize,
  43. };
  44. tl.AppendLine("Hollow Text", tf0);
  45.  
  46. TextFormat tf1 = new TextFormat()
  47. {
  48. StrokePen = Color.DarkMagenta,
  49. FillBrush = new GCDRAW.SolidBrush(Color.Yellow),
  50. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FoglihtenNo07.otf")),
  51. FontSize = fontSize,
  52. };
  53. tl.AppendLine("Outlined Text", tf1);
  54.  
  55. var grad0 = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Blue, new PointF(1, 0));
  56. TextFormat tf2 = new TextFormat()
  57. {
  58. // StrokePen = Color.DarkMagenta,
  59. FillBrush = grad0,
  60. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
  61. FontSize = fontSize,
  62. };
  63. tl.AppendLine("Gradient Fill", tf2);
  64.  
  65. var grad1 = new LinearGradientBrush(Color.Red, Color.Purple);
  66. grad1.GradientStops = new List<GradientStop>()
  67. {
  68. new GradientStop(Color.Orange, 1 / 6f),
  69. new GradientStop(Color.Yellow, 2 / 6f),
  70. new GradientStop(Color.Green, 3 / 6f),
  71. new GradientStop(Color.Cyan, 4 / 6f),
  72. new GradientStop(Color.Blue, 5 / 6f)
  73. };
  74. TextFormat tf3 = new TextFormat()
  75. {
  76. // StrokePen = Color.DarkMagenta,
  77. FillBrush = grad1,
  78. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cambriab.ttf")),
  79. FontSize = fontSize,
  80. };
  81. tl.AppendLine("Multi-stop gradient", tf3);
  82.  
  83. var tf4 = new TextFormat(tf3);
  84. tf4.StrokePen = Color.GreenYellow;
  85. tl.AppendLine("Multi-stop gradient with outline", tf4);
  86.  
  87. var tf5 = new TextFormat(tf4);
  88. tf5.Hollow = true;
  89. tf5.StrokePen = new GCDRAW.Pen(Color.DarkRed, g.Resolution / 72);
  90. tl.AppendLine("Text outlined with 1pt wide pen", tf5);
  91.  
  92. // It is not necessary to call PerformLayout() for a newly created TextLayout
  93. // or after a call to TextLayout.Clear():
  94. g.DrawTextLayout(tl, PointF.Empty);
  95. }
  96. // Done:
  97. return bmp;
  98. }
  99. }
  100. }
  101.