Glow.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 how to create a Glow effect using DsImaging.
  20. // The Glow effect inflates all non-transparent areas of an image by a specified amount,
  21. // then applies a Gaussian blur to make the border smooth. It is one of the effects
  22. // that are found in MS Word for example.
  23. //
  24. // To achieve this effect in DsImaging, the GrayscaleBitmap.ApplyGlow() method is used
  25. // on the transparency mask built from the color image on which we want the glow to appear,
  26. // along with a few other simple steps as illustrated by this code.
  27. //
  28. // This example shows how to achieve the glow effect by blurring the content (text and image)
  29. // and drawing the original content on it again. See GlowAlt for an alternative way
  30. // that avoids drawing the content twice.
  31. //
  32. // The same GrayscaleBitmap.ApplyGlow() method is also used to achieve the Soft Edges effect
  33. // as illustrated by the SoftEdges example.
  34. public class Glow
  35. {
  36. private GCTEXT.Font _font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf"));
  37.  
  38. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  39. {
  40. // Draw text and logo on a transparent bitmap:
  41. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, false);
  42. bmp.Clear(Color.Transparent);
  43. drawTextAndLogo();
  44.  
  45. // Convert the image to a transparency mask:
  46. using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
  47.  
  48. // Apply the Glow effect to it; while for Soft Edges the inflation radius is negative,
  49. // for Glow it should be positive, 6 here (9 is the blur radius):
  50. gs.ApplyGlow(6, 9);
  51.  
  52. // Map a shadow from the transparency mask to the source GcBitmap drawing opaque pixels
  53. // with glow color (Yellow) and applying some additional transparency:
  54. gs.ToShadowBitmap(bmp, Color.Yellow, 0.8f);
  55.  
  56. // Fill the background:
  57. bmp.ConvertToOpaque(Color.Gray);
  58.  
  59. // Finally, draw the text and logo once more over the prepared background:
  60. drawTextAndLogo();
  61.  
  62. // Done
  63. return bmp;
  64.  
  65. void drawTextAndLogo()
  66. {
  67. var renderer = bmp.EnsureRendererCreated();
  68. DrawText(renderer, pixelSize, dpi);
  69. DrawLogo(renderer, new PointF(pixelSize.Width / 2, (int)(pixelSize.Height * 0.8)), pixelSize.Width / 2);
  70. }
  71. }
  72.  
  73. void DrawText(BitmapRenderer renderer, Size pixelSize, float dpi)
  74. {
  75. var f1 = new TextFormat
  76. {
  77. Font = _font,
  78. FontSize = pixelSize.Height / 6,
  79. ForeColor = Color.DarkOrchid,
  80. FontBold = true
  81. };
  82. var f2 = new TextFormat(f1)
  83. {
  84. ForeColor = Color.White,
  85. StrokePen = new GCDRAW.Pen(Color.DarkOrchid, 3)
  86. };
  87. var tl = new TextLayout(dpi)
  88. {
  89. MaxWidth = pixelSize.Width,
  90. MaxHeight = pixelSize.Height,
  91. ParagraphAlignment = ParagraphAlignment.Near,
  92. TextAlignment = TextAlignment.Center,
  93. MarginTop = dpi * 2,
  94. LineSpacingScaleFactor = 0.7f,
  95. };
  96. tl.Append("Document", f1);
  97. tl.Append("Solutions", f2);
  98.  
  99. // Draw the text:
  100. renderer.SlowAntialiasing = true;
  101. renderer.DrawTextLayout(tl, 0, 0);
  102. }
  103.  
  104. void DrawLogo(BitmapRenderer renderer, PointF center, float width)
  105. {
  106. var pb = new PathBuilder();
  107. pb.BeginFigure(100, 350);
  108. pb.AddLine(210, 310);
  109. var arc = new ArcSegment
  110. {
  111. Size = new SizeF(183, 173),
  112. SweepDirection = SweepDirection.Clockwise,
  113. Point = new PointF(550, 205),
  114. };
  115. pb.AddArc(arc);
  116. pb.AddLine(650, 170);
  117. pb.AddLine(680, 250);
  118. pb.AddLine(575, 285);
  119. arc.Point = new PointF(240, 390);
  120. pb.AddArc(arc);
  121. pb.AddLine(130, 430);
  122. pb.EndFigure(true);
  123. pb.Figures.Add(new EllipticFigure(new RectangleF(295, 197, 200, 190)));
  124. var gpFill = pb.ToPath();
  125. var gpStroke = gpFill.Widen(new GCDRAW.Pen(Color.Black, 20));
  126.  
  127. // Our 'base' size is 800 x 600 pixels:
  128. float scale = width / 800;
  129. renderer.Transform = Matrix3x2.CreateScale(scale) *
  130. Matrix3x2.CreateTranslation(center.X - 400 * scale, center.Y - 300 * scale);
  131. renderer.FillPath(gpFill, Color.CornflowerBlue);
  132. renderer.FillPath(gpStroke, Color.DarkOrchid);
  133. renderer.Transform = Matrix3x2.Identity;
  134. }
  135. }
  136. }
  137.