GrayscaleEffects.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 how to use different grayscale effects.
  20. // See also MatrixEffects1 and MatrixEffects2.
  21. public class GrayscaleEffects
  22. {
  23. public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
  24. {
  25. opaque = false;
  26. var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
  27. using (var origBmp = new GcBitmap())
  28. {
  29. // Load a sample photo:
  30. var imagePath = Path.Combine("Resources", "Images", "maple.jpg");
  31. using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
  32. origBmp.Load(stm);
  33.  
  34. origBmp.SetAlphaTo255();
  35. origBmp.Opaque = false;
  36.  
  37. // Resize the original photo so we can place 4 samples of it
  38. // on the resulting bitmap:
  39. int w = pixelSize.Width / 2;
  40. int h = pixelSize.Height / 2;
  41. using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
  42. {
  43. // Copy the resized original into 4 quadrants of the resulting bitmap:
  44. bmp.BitBlt(sizedBmp, 0, 0);
  45. bmp.BitBlt(sizedBmp, w, 0);
  46. bmp.BitBlt(sizedBmp, 0, h);
  47. bmp.BitBlt(sizedBmp, w, h);
  48. }
  49.  
  50. // Moving the 3 lines with the "ApplyEffect" comment from below to here
  51. // will apply the effects only to the photos but not to the texts.
  52.  
  53. // Add borders between the quadrants, and captions for each:
  54. var lineh = 2;
  55. using (var g = bmp.CreateGraphics(null))
  56. {
  57. var foreColor = Color.Yellow;
  58. var backColor = Color.Blue;
  59. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  60. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(foreColor, lineh * 2));
  61. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(foreColor, lineh * 2));
  62. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  63. g.DrawString(" Original image ", tf, new PointF(0, 0));
  64. g.DrawString(" GrayscaleStandard.BT601 ", tf, new PointF(w + lineh, 0));
  65. g.DrawString(" GrayscaleStandard.BT709 ", tf, new PointF(0, h + lineh));
  66. g.DrawString(" GrayscaleStandard.BT2100 ", tf, new PointF(w + lineh, h + lineh));
  67. }
  68. // ApplyEffect (move this code up to before drawing texts
  69. // to limit it to photos only and not affect the captions).
  70. //
  71. // Keep the pixels in top left quadrant intact,
  72. // apply effects to the other 3 quadrants:
  73. bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT601), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
  74. bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT709), new Rectangle(0, h + lineh, w - lineh, h - lineh));
  75. bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT2100), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
  76. }
  77. return bmp;
  78. }
  79. }
  80. }
  81.