ThresholdingEffects.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 ThresholdingEffects
  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", "Stock", "bw-hiking.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. var lineh = 2;
  51.  
  52. // Keep the pixels in top left quadrant intact,
  53. // apply effects to the other 3 quadrants
  54. // (these effects make sense for grayscale images only, so we do it before adding captions):
  55. bmp.ApplyEffect(OtsuThresholdingEffect.Get(), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
  56. bmp.ApplyEffect(BradleyThresholdingEffect.Get(), new Rectangle(0, h + lineh, w - lineh, h - lineh));
  57. bmp.ApplyEffect(BradleyThresholdingEffect.Get(16, 6), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
  58.  
  59. // Add borders between the quadrants, and captions for each:
  60. using (var g = bmp.CreateGraphics(null))
  61. {
  62. var foreColor = Color.Yellow;
  63. var backColor = Color.Blue;
  64. var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
  65. g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(foreColor, lineh * 2));
  66. g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(foreColor, lineh * 2));
  67. var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
  68. g.DrawString(" Original image ", tf, new PointF(0, 0));
  69. g.DrawString(" OtsuThresholdingEffect ", tf, new PointF(w + lineh, 0));
  70. g.DrawString(" BradleyThresholdingEffect ", tf, new PointF(0, h + lineh));
  71. g.DrawString(" BradleyThresholdingEffect(16,6) ", tf, new PointF(w + lineh, h + lineh));
  72. }
  73. }
  74. return bmp;
  75. }
  76. }
  77. }
  78.