TransparencyMask.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 the use of TransparencyMaskBitmap
  20. // when drawing on a bitmap. The transparency mask used is
  21. // created on the fly by filling an intermediary bitmap
  22. // with a linear gradient brush from 0 (black, transparent)
  23. // to 255 (white, opaque).
  24. public class TransparencyMask
  25. {
  26. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  27. {
  28. var h2 = pixelSize.Width / 2;
  29.  
  30. // Prepare a linear gradient transparency mask,
  31. // from 0 (transparent) to 255 (opaque):
  32. using var mask = new GcBitmap(h2, h2, true);
  33. using var gmask = mask.CreateGraphics();
  34. var grad = new LinearGradientBrush(Color.Black, Color.White);
  35. gmask.FillRectangle(new RectangleF(0, 0, mask.Width, mask.Height), grad);
  36. // Convert to GrayscaleBitmap to be used as Renderer.TransparencyMaskBitmap:
  37. using var gsb = mask.ToGrayscaleBitmap();
  38.  
  39. // Create a bitmap to draw on, fill it with yellow background:
  40. using var bmp1 = new GcBitmap(h2, h2, true);
  41. using var g = bmp1.CreateGraphics(Color.Yellow);
  42.  
  43. // Apply the transparency mask:
  44. g.Renderer.TransparencyMaskBitmap = gsb;
  45.  
  46. // Fill 3 circles, note how the fill gradually changes
  47. // from transparent to opaque (left to right) along with the gradient:
  48. var d = h2 / 25f;
  49. var rc = new RectangleF(0, 0, h2 * 0.7f, h2 * 0.7f);
  50.  
  51. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
  52. bmp.Clear(Color.White);
  53. FillCircles(g, rc, h2, d);
  54. bmp.BitBlt(bmp1, 0, 0);
  55.  
  56. g.Renderer.TransparencyMaskBitmap = null;
  57. FillCircles(g, rc, h2, d);
  58. bmp.BitBlt(bmp1, h2, 0);
  59.  
  60. // Add some explanatory texts below the images:
  61. var gg = bmp.CreateGraphics();
  62. rc = new RectangleF(0, h2 + d, h2 - d * 2, h2 - d * 2);
  63. var tf = new TextFormat()
  64. {
  65. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf")),
  66. FontSize = 16
  67. };
  68. gg.DrawString(
  69. "The circles in the image above were rendered with TransparencyMaskBitmap " +
  70. "on the target graphics set to a linear gradient from transparent to opaque.",
  71. tf, rc, TextAlignment.Center, ParagraphAlignment.Near);
  72.  
  73. rc.Offset(h2, 0);
  74. gg.DrawString(
  75. "The circles in the image above were rendered with no TransparencyMaskBitmap " +
  76. "on the target graphics.",
  77. tf, rc, TextAlignment.Center, ParagraphAlignment.Near);
  78.  
  79. return bmp;
  80. }
  81.  
  82. void FillCircles(GcBitmapGraphics g, RectangleF rc, float h2, float d)
  83. {
  84. var r = rc;
  85. r.Offset((h2 - rc.Width) / 2, 0);
  86. r.Inflate(-d, -d);
  87. g.FillEllipse(r, Color.Red);
  88. r = rc;
  89. r.Offset(h2 - rc.Width, h2 - rc.Height);
  90. r.Inflate(-d, -d);
  91. g.FillEllipse(r, Color.Green);
  92. r = rc;
  93. r.Offset(0, h2 - rc.Height);
  94. r.Inflate(-d, -d);
  95. g.FillEllipse(r, Color.Blue);
  96. }
  97. }
  98. }
  99.