TransparentShadow.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 draw a window with a solid frame
  20. // and four semi-transparent colored window panes,
  21. // floating above a solid brick wall with some text written on it,
  22. // and casting a semi-transparent colored shadow on the wall.
  23. public class TransparentShadow
  24. {
  25. private GCTEXT.Font _font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "segoesc.ttf"));
  26.  
  27. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  28. {
  29. // A temporary bitmap to hold the shadow:
  30. using var shadow = new GcBitmap(pixelSize.Width, pixelSize.Height, false);
  31.  
  32. // Draw the window on the shadow bitmap; the offsetX/offsetY (64/82)
  33. // account for the shadow's offset relative to the floating window
  34. // that will be drawn later:
  35. using (var g = shadow.CreateGraphics(Color.Transparent))
  36. DrawWindow(g, pixelSize, 64, 82);
  37.  
  38. // Create a shadow without blur in 'shadowTemp' and blend it with the main shadow bitmap
  39. // in order to render colored shadows of the semi-transparent window panes:
  40. using (var gs = shadow.ToGrayscaleBitmap(ColorChannel.Alpha))
  41. using (var shadowTemp = gs.ToShadowBitmap(Color.DarkSlateGray))
  42. shadow.AlphaBlend(shadowTemp, 0, 0);
  43.  
  44. // At this point 'shadow' contains opaque colored shadow without blur,
  45. // we apply the blur effect and make it semi-transparent:
  46. shadow.ApplyEffect(GaussianBlurEffect.Get(29));
  47. shadow.ApplyEffect(OpacityEffect.Get(0.85f));
  48.  
  49. // Prepare the brick wall image:
  50. var wall = new GcBitmap(pixelSize.Width, pixelSize.Height, true);
  51. using (var g = wall.CreateGraphics(Color.DarkSalmon))
  52. DrawWall(g, pixelSize);
  53.  
  54. // Blend the shadow with the wall:
  55. wall.AlphaBlend(shadow, 0, 0);
  56.  
  57. // Finally draw the window floating in front of the wall:
  58. using (var g = wall.CreateGraphics())
  59. DrawWindow(g, pixelSize, 0, 0);
  60.  
  61. // Done
  62. return wall;
  63. }
  64.  
  65. // Draw a window with four colored semi-transparent panes:
  66. void DrawWindow(GcGraphics g, Size pixelSize, float offsetX, float offsetY)
  67. {
  68. var w = pixelSize.Width * 0.4f;
  69. var h = pixelSize.Height * 0.5f;
  70. var x = pixelSize.Width / 2 - w / 2;
  71. var y = pixelSize.Height / 2 - h / 2;
  72.  
  73. g.Transform = Matrix3x2.CreateTranslation(offsetX, offsetY);
  74.  
  75. var wnd = new RectangleF(x, y, w, h);
  76. var winHalf = new SizeF(wnd.Width / 2, wnd.Height / 2);
  77. var glassTL = Color.FromArgb(unchecked((int)0x70FF4600));
  78. var glassTR = Color.FromArgb(unchecked((int)0x70A5FF00));
  79. var glassBL = Color.FromArgb(unchecked((int)0x70007BFF));
  80. var glassBR = Color.FromArgb(unchecked((int)0x70FFCD00));
  81.  
  82. g.FillRectangle(new RectangleF(wnd.Location, winHalf), glassTL);
  83. g.FillRectangle(new RectangleF(new PointF(wnd.X + wnd.Width / 2, wnd.Y), winHalf), glassTR);
  84. g.FillRectangle(new RectangleF(new PointF(wnd.X, wnd.Y + wnd.Height / 2), winHalf), glassBL);
  85. g.FillRectangle(new RectangleF(new PointF(wnd.X + wnd.Width / 2, wnd.Y + wnd.Height / 2), winHalf), glassBR);
  86.  
  87. var outline = Color.DarkSlateGray;
  88. g.DrawRectangle(wnd, new GCDRAW.Pen(outline, 34));
  89. g.DrawLine(wnd.Left, wnd.Top + wnd.Height / 2, wnd.Right, wnd.Top + wnd.Height / 2, outline, 24);
  90. g.DrawLine(wnd.Left + wnd.Width / 2, wnd.Top, wnd.Left + wnd.Width / 2, wnd.Bottom, outline, 24);
  91. var frame = Color.LightGoldenrodYellow;
  92. g.DrawRectangle(wnd, new GCDRAW.Pen(frame, 30));
  93. g.DrawLine(wnd.Left, wnd.Top + wnd.Height / 2, wnd.Right, wnd.Top + wnd.Height / 2, frame, 20);
  94. g.DrawLine(wnd.Left + wnd.Width / 2, wnd.Top, wnd.Left + wnd.Width / 2, wnd.Bottom, frame, 20);
  95.  
  96. g.Transform = Matrix3x2.Identity;
  97. }
  98.  
  99. // Draw a brick wall with a white text:
  100. void DrawWall(GcGraphics g, Size pixelSize)
  101. {
  102. // The brick pattern:
  103. var brick = new Size(112, 44);
  104. var pen = new GCDRAW.Pen(Color.DimGray, 2);
  105. var off = brick.Width / 2;
  106. for (int y = -brick.Height / 3; y < pixelSize.Height; y += brick.Height)
  107. {
  108. for (int x = off + brick.Width / 3; x < pixelSize.Width; x += brick.Width)
  109. {
  110. g.DrawLine(x, y, x, y + brick.Height, pen);
  111. }
  112. off = off == 0 ? brick.Width / 2 : 0;
  113. g.DrawLine(0, y, pixelSize.Width, y, pen);
  114. }
  115. // Graffiti-like white text (overlay blend mode is used to visually preserve the joints):
  116. var tl = g.CreateTextLayout();
  117. tl.MaxWidth = pixelSize.Width;
  118. tl.MaxHeight = pixelSize.Height;
  119. tl.ParagraphAlignment = ParagraphAlignment.Center;
  120. tl.TextAlignment = TextAlignment.Center;
  121. tl.DefaultFormat.Font = _font;
  122. tl.DefaultFormat.FontSize = 270;
  123. tl.DefaultFormat.ForeColor = Color.White;
  124. tl.ParagraphSpacing = -120;
  125. tl.Append("The\nWall");
  126. g.BlendMode = BlendMode.Overlay;
  127. g.DrawTextLayout(tl, Point.Empty);
  128. g.BlendMode = BlendMode.Normal;
  129. }
  130. }
  131. }
  132.