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