AllBlendingModes.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 composing two images using the available blending modes.
  20. // The sample uses the same approach as the BlendingModes sample,
  21. // but instead of showing a few of the available modes that produce
  22. // a nice looking result, demonstrates all of them in smaller scale.
  23. // The base and the blended images used as the same as in the BlendingModes sample.
  24. // See also BlendImages_0 and BlendText samples.
  25. public class AllBlendingModes
  26. {
  27. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  28. {
  29. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  30. using (var origBmp = new GcBitmap())
  31. using (var spectrumBmp = new GcBitmap())
  32. {
  33. // Load a sample photo:
  34. var imagePath = Path.Combine("Resources", "ImagesBis", "orchid.jpg");
  35. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  36. origBmp.Load(stm);
  37.  
  38. // Blending source:
  39. var spectrumPath = Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png");
  40. using (var stm = new FileStream(spectrumPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  41. spectrumBmp.Load(stm);
  42.  
  43. origBmp.Opaque = spectrumBmp.Opaque = opaque;
  44.  
  45. // Resize the original photo so we can place 4 samples of it
  46. // on the resulting bitmap:
  47. int w = pixelSize.Width / 4;
  48. int h = pixelSize.Height / 4;
  49. using (var g = bmp.CreateGraphics())
  50. {
  51. // Prepare for captions:
  52. var tl = g.CreateTextLayout();
  53. tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  54. tl.DefaultFormat.FontSize = 12;
  55. tl.ParagraphAlignment = ParagraphAlignment.Center;
  56. tl.TextAlignment = TextAlignment.Center;
  57. tl.MaxWidth = w;
  58. tl.MaxHeight = h;
  59. tl.MarginTop = h - g.MeasureString("QWERTY", tl.DefaultFormat).Height * 1.4f;
  60. // Render all 16 blending modes in a 4x4 grid:
  61. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  62. using (var sizedSpectrumBmp = spectrumBmp.Resize(w, h))
  63. {
  64. for (int row = 0; row < 4; ++row)
  65. for (int col = 0; col < 4; ++col)
  66. {
  67. // Sample:
  68. var blendMode = (BlendMode)(row * 4 + col);
  69. int x = w * col, y = h * row;
  70. bmp.BitBlt(sizedBmp, x, y);
  71. bmp.CompositeAndBlend(sizedSpectrumBmp, x, y, CompositeMode.SourceOver, blendMode);
  72. // Caption:
  73. tl.Clear();
  74. tl.Append(blendMode.ToString());
  75. tl.PerformLayout(true);
  76. var rc = tl.ContentRectangle;
  77. rc.Offset(x, y);
  78. rc.Inflate(4, 2);
  79. g.FillRectangle(rc, Color.White);
  80. g.DrawTextLayout(tl, new PointF(x, y));
  81. }
  82. }
  83. }
  84. }
  85. return bmp;
  86. }
  87. }
  88. }
  89.