Shadow.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 example shows how to create a semi-transparent blurred shadow
  20. // of a text and graphics image, offset by a specified amount.
  21. // To achieve this the code employs the ApplyGaussianBlur and ToShadowBitmap
  22. // methods of the GrayscaleBitmap class.
  23. public class Shadow
  24. {
  25. private GCTEXT.Font _font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibri.ttf"));
  26.  
  27. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  28. {
  29. // Create a transparent bitmap and draw the shadow image on it (offset left/down by 30/50):
  30. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, false);
  31. using (var g = bmp.CreateGraphics(Color.Transparent))
  32. {
  33. Draw(g, 30, 50);
  34. }
  35.  
  36. // Extract the alpha channel from GcBitmap to a GrayscaleBitmap:
  37. using var gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha);
  38.  
  39. // Blur the GrayscaleBitmap to create a believable looking shadow:
  40. gs.ApplyGaussianBlur(9);
  41.  
  42. // Convert the transparency mask from GrayscaleBitmap to GcBitmap,
  43. // filling the opaque pixels with the shadow color (CadetBlue),
  44. // also making the resulting 'shadow' slightly transparent (0.6f):
  45. gs.ToShadowBitmap(bmp, Color.CadetBlue, 0.6f);
  46.  
  47. // Replace the transparent background with an opaque background color:
  48. bmp.ConvertToOpaque(Color.LightGoldenrodYellow);
  49.  
  50. // Finally draw the original image without offset on top:
  51. using (var g = bmp.CreateGraphics())
  52. {
  53. Draw(g, 0, 0);
  54. }
  55.  
  56. // Done
  57. return bmp;
  58. }
  59.  
  60. private void Draw(GcGraphics g, float offsetX, float offsetY)
  61. {
  62. var baseT = Matrix3x2.CreateTranslation(offsetX, offsetY);
  63. g.Transform = baseT;
  64. g.DrawEllipse(new RectangleF(100, 100, 300, 200), new GCDRAW.Pen(Color.Orange, 20));
  65. g.DrawLine(new PointF(50, 400), new PointF(500, 50), new GCDRAW.Pen(Color.RoyalBlue, 20)
  66. {
  67. LineCap = PenLineCap.Round
  68. });
  69. g.DrawString("Howl's Moving Castle",
  70. new TextFormat
  71. {
  72. Font = _font,
  73. FontSize = 40,
  74. ForeColor = Color.MistyRose,
  75. StrokePen = new GCDRAW.Pen(Color.DarkRed, 1)
  76. },
  77. new PointF(200, 150));
  78. g.Transform =
  79. Matrix3x2.CreateRotation((float)(Math.PI / 6)) *
  80. (Matrix3x2.CreateTranslation(50, 250) * baseT);
  81. g.DrawString("The quick brown fox jumps over the lazy dog.",
  82. new TextFormat
  83. {
  84. Font = _font,
  85. FontSize = 18,
  86. ForeColor = Color.CornflowerBlue
  87. },
  88. new PointF(0, 0));
  89. g.DrawRectangle(new RectangleF(-15, -10, 470, 50), new GCDRAW.Pen(Color.Salmon, 1));
  90.  
  91. g.Transform = baseT;
  92.  
  93. // Draw a window with four colored semi-transparent panes:
  94. var wnd = new RectangleF(520, 420, 400, 500);
  95. var winHalf = new SizeF(wnd.Width / 2, wnd.Height / 2);
  96. var frame = Color.Brown;
  97. var glassTL = Color.FromArgb(unchecked((int)0x70FF4600));
  98. var glassTR = Color.FromArgb(unchecked((int)0x70A5FF00));
  99. var glassBL = Color.FromArgb(unchecked((int)0x70007BFF));
  100. var glassBR = Color.FromArgb(unchecked((int)0x70FFCD00));
  101.  
  102. g.FillRectangle(new RectangleF(wnd.Location, winHalf), glassTL);
  103. g.FillRectangle(new RectangleF(new PointF(wnd.X + wnd.Width / 2, wnd.Y), winHalf), glassTR);
  104. g.FillRectangle(new RectangleF(new PointF(wnd.X, wnd.Y + wnd.Height / 2), winHalf), glassBL);
  105. g.FillRectangle(new RectangleF(new PointF(wnd.X + wnd.Width / 2, wnd.Y + wnd.Height / 2), winHalf), glassBR);
  106.  
  107. g.DrawRectangle(wnd, new GCDRAW.Pen(frame, 30));
  108. g.DrawLine(wnd.Left, wnd.Top + wnd.Height / 2, wnd.Right, wnd.Top + wnd.Height / 2, frame, 20);
  109. g.DrawLine(wnd.Left + wnd.Width / 2, wnd.Top, wnd.Left + wnd.Width / 2, wnd.Bottom, frame, 20);
  110.  
  111. g.Transform = Matrix3x2.Identity;
  112. }
  113. }
  114. }
  115.