BackgroundBitmap.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 BitmapRenderer.BackgroundBitmap
  20. // when drawing on a bitmap.
  21. //
  22. // When drawing semi-transparent graphic objects, usually the resulting color
  23. // of a pixel is a combination of the target bitmap pixel's color and the color
  24. // of the graphic object's pixel. But if the BackgroundBitmap is set on the
  25. // BitmapRenderer, pixels of that bitmap will be used instead of the target
  26. // bitmap's pixels when determining the resulting color. Background bitmaps
  27. // are used to support Isolated and Knockout transparency groups (as defined
  28. // in the PDF specification 1.7 sections 7.3.4 and 7.3.5) when rendering PDFs
  29. // to images.
  30. public class BackgroundBitmap
  31. {
  32. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool _, string[] _1 = null)
  33. {
  34. var hx = pixelSize.Width / 2;
  35. var hy = pixelSize.Height / 2;
  36.  
  37. // The spectrum image used for the backdrop:
  38. using var bmp0 = new GcBitmap(Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png"));
  39. using var bmp1 = bmp0.Resize(hx, hy, InterpolationMode.Cubic);
  40.  
  41. using var bmpBackdrop = new GcBitmap(hx, hy, false, dpi, dpi);
  42. using var bmpInitial = new GcBitmap(hx, hy, false, dpi, dpi);
  43. using var gB = bmpBackdrop.CreateGraphics();
  44. using var gI = bmpInitial.CreateGraphics();
  45.  
  46. gB.Renderer.Aliased = true;
  47. gB.Renderer.BlendMode = BlendMode.Multiply;
  48. gI.Renderer.Aliased = true;
  49. gI.Renderer.BlendMode = BlendMode.Multiply;
  50.  
  51. // The target bitmap will contain 4 demo quadrants:
  52. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, false, dpi, dpi);
  53.  
  54. // Isolated, Knockout:
  55. bmpBackdrop.BitBlt(bmp1, 0, 0);
  56. bmpInitial.Clear(Color.Transparent);
  57. gB.Renderer.BackgroundBitmap = bmpInitial;
  58. FillCircles(gB, hx, hy);
  59. bmp.BitBlt(bmpBackdrop, 0, 0);
  60.  
  61. // Isolated, Non-knockout:
  62. FillCircles(gI, hx, hy);
  63. bmpBackdrop.BitBlt(bmp1, 0, 0);
  64. bmpBackdrop.AlphaBlend(bmpInitial, 0, 0);
  65. bmp.BitBlt(bmpBackdrop, hx, 0);
  66.  
  67. // Non-isolated, Knockout:
  68. bmpBackdrop.BitBlt(bmp1, 0, 0);
  69. bmpInitial.BitBlt(bmp1, 0, 0);
  70. FillCircles(gB, hx, hy);
  71. bmp.BitBlt(bmpBackdrop, 0, hy);
  72.  
  73. // Non-isolated, Non-knockout:
  74. bmpBackdrop.BitBlt(bmp1, 0, 0);
  75. gB.Renderer.BackgroundBitmap = null;
  76. FillCircles(gB, hx, hy);
  77. bmp.BitBlt(bmpBackdrop, hx, hy);
  78.  
  79. // Adornments:
  80. using var g = bmp.CreateGraphics();
  81. g.DrawLine(0, hy, bmp.PixelWidth, hy, new GCDRAW.Pen(Color.Black));
  82. g.DrawLine(hx, 0, hx, bmp.PixelHeight, new GCDRAW.Pen(Color.Black));
  83. var tf = new TextFormat()
  84. {
  85. Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "courbd.ttf")),
  86. FontSize = 16,
  87. };
  88. var d = 40;
  89. g.DrawString("Isolated, Knockout", tf, new RectangleF(0, hy - d, hx, d),
  90. TextAlignment.Center, ParagraphAlignment.Center, false);
  91. g.DrawString("Isolated, Non-knockout", tf, new RectangleF(hx, hy - d, hx, d),
  92. TextAlignment.Center, ParagraphAlignment.Center, false);
  93. g.DrawString("Non-isolated, Knockout", tf, new RectangleF(0, hy * 2 - d, hx, d),
  94. TextAlignment.Center, ParagraphAlignment.Center, false);
  95. g.DrawString("Non-isolated, Non-knockout", tf, new RectangleF(hx, hy * 2 - d, hx, d),
  96. TextAlignment.Center, ParagraphAlignment.Center, false);
  97.  
  98. // Done:
  99. return bmp;
  100. }
  101.  
  102. void FillCircles(GcBitmapGraphics g, float hx, float hy)
  103. {
  104. var dx = hx / 10;
  105. var dy = hy / 10;
  106. var qx = hx / 2;
  107. var qy = hy / 2;
  108.  
  109. g.FillEllipse(new RectangleF(dx, dy, qx, qy), Color.LightGray);
  110. g.FillEllipse(new RectangleF(qx - dx, dy, qx, qy), Color.LightGray);
  111. g.FillEllipse(new RectangleF(dx, qy - dy, qx, qy), Color.LightGray);
  112. g.FillEllipse(new RectangleF(qx - dx, qy - dy, qx, qy), Color.LightGray);
  113. }
  114. }
  115. }
  116.