BlendingModes.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 a few
  20. // of the many available blending modes.
  21. // The base image is a photo of a white orchid on a dark background.
  22. // The blended image is a simple spectrum (thumbnail shown in the bottom right corner).
  23. // See the AllBlendingModes sample for a demo of all available blending modes.
  24. public class BlendingModes
  25. {
  26. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  27. {
  28. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  29. using (var origBmp = new GcBitmap())
  30. using (var spectrumBmp = new GcBitmap())
  31. {
  32. // Load a sample photo:
  33. var imagePath = Path.Combine("Resources", "ImagesBis", "orchid.jpg");
  34. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  35. origBmp.Load(stm);
  36.  
  37. // Blending source:
  38. var spectrumPath = Path.Combine("Resources", "ImagesBis", "spectrum-500x500.png");
  39. using (var stm = new FileStream(spectrumPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  40. spectrumBmp.Load(stm);
  41.  
  42. origBmp.Opaque = spectrumBmp.Opaque = opaque;
  43.  
  44. // Resize the original photo so we can place 4 samples of it
  45. // on the resulting bitmap:
  46. int w = pixelSize.Width / 2;
  47. int h = pixelSize.Height / 2;
  48. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  49. using (var sizedSpectrumBmp = spectrumBmp.Resize(w, h))
  50. {
  51. // Resized original image:
  52. bmp.BitBlt(sizedBmp, 0, 0);
  53.  
  54. // Blending mode Color:
  55. bmp.BitBlt(sizedBmp, w, 0);
  56. bmp.CompositeAndBlend(sizedSpectrumBmp, w, 0, CompositeMode.SourceOver, BlendMode.Color);
  57.  
  58. // Blending mode SoftLight:
  59. bmp.BitBlt(sizedBmp, 0, h);
  60. bmp.CompositeAndBlend(sizedSpectrumBmp, 0, h, CompositeMode.SourceOver, BlendMode.SoftLight);
  61.  
  62. // Blending mode Hue:
  63. bmp.BitBlt(sizedBmp, w, h);
  64. bmp.CompositeAndBlend(sizedSpectrumBmp, w, h, CompositeMode.SourceOver, BlendMode.Hue);
  65. }
  66. // Show a thumbnail of the blend source in the bottom right corner:
  67. using (var spectrumThumbnail = spectrumBmp.Resize(w / 4, h / 4))
  68. bmp.BitBlt(spectrumThumbnail, pixelSize.Width - spectrumThumbnail.PixelWidth, pixelSize.Height - spectrumThumbnail.PixelHeight);
  69. // Add borders between the quadrants, and captions for each:
  70. var lineh = 2;
  71. using (var g = bmp.CreateGraphics(null))
  72. {
  73. var foreColor = Color.Yellow;
  74. var backColor = Color.Blue;
  75. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  76. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(Color.Gray, lineh * 2));
  77. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(Color.Gray, lineh * 2));
  78. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  79. var th = g.MeasureString("QWERTY", tf).Height;
  80. g.DrawString(" Original image ", tf, new PointF(0, h - th + lineh));
  81. g.DrawString(" BlendMode.Color ", tf, new PointF(w + lineh, h - th + lineh));
  82. g.DrawString(" BlendMode.SoftLight ", tf, new PointF(0, h * 2 + lineh - th + lineh));
  83. g.DrawString(" BlendMode.Hue ", tf, new PointF(w + lineh, h * 2 + lineh - th + lineh));
  84. }
  85. }
  86. return bmp;
  87. }
  88. }
  89. }
  90.