ThresholdingEffects.vb
  1. ''
  2. '' This code is part of Document Solutions for Imaging demos.
  3. '' Copyright (c) MESCIUS inc. All rights reserved.
  4. ''
  5. Imports System.IO
  6. Imports System.Drawing
  7. Imports System.Numerics
  8. Imports GrapeCity.Documents.Drawing
  9. Imports GrapeCity.Documents.Text
  10. Imports GrapeCity.Documents.Imaging
  11. Imports GCTEXT = GrapeCity.Documents.Text
  12. Imports GCDRAW = GrapeCity.Documents.Drawing
  13.  
  14. '' This sample demonstrates how to use different grayscale effects.
  15. '' See also MatrixEffects1 and MatrixEffects2.
  16. Public Class ThresholdingEffects
  17. Function GenerateImage(
  18. ByVal pixelSize As Size,
  19. ByVal dpi As Single,
  20. ByVal opaque As Boolean,
  21. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  22.  
  23. opaque = False
  24. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  25. Using origBmp = New GcBitmap()
  26. '' Load a sample photo:
  27. Dim imagePath = Path.Combine("Resources", "Stock", "bw-hiking.jpg")
  28. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  29. origBmp.Load(stm)
  30. End Using
  31.  
  32. origBmp.SetAlphaTo255()
  33. origBmp.Opaque = False
  34.  
  35. '' Resize the original photo so we can place 4 samples of it
  36. '' on the resulting bitmap:
  37. Dim w = pixelSize.Width / 2
  38. Dim h = pixelSize.Height / 2
  39. Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
  40. '' Copy the resized original into 4 quadrants of the resulting bitmap:
  41. bmp.BitBlt(sizedBmp, 0, 0)
  42. bmp.BitBlt(sizedBmp, w, 0)
  43. bmp.BitBlt(sizedBmp, 0, h)
  44. bmp.BitBlt(sizedBmp, w, h)
  45. End Using
  46.  
  47. Dim lineh = 2
  48.  
  49. '' Keep the pixels in top left quadrant intact,
  50. '' apply effects to the other 3 quadrants
  51. '' (these effects make sense for grayscale images only, so we do it before adding captions):
  52. bmp.ApplyEffect(OtsuThresholdingEffect.Get(), New Rectangle(w + lineh, 0, w - lineh, h - lineh))
  53. bmp.ApplyEffect(BradleyThresholdingEffect.Get(), New Rectangle(0, h + lineh, w - lineh, h - lineh))
  54. bmp.ApplyEffect(BradleyThresholdingEffect.Get(16, 6), New Rectangle(w + lineh, h + lineh, w - lineh, h - lineh))
  55.  
  56. '' Add borders between the quadrants, And captions for each
  57. Using g = bmp.CreateGraphics(Nothing)
  58. Dim foreColor = Color.Yellow
  59. Dim backColor = Color.Blue
  60. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  61. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(foreColor, lineh * 2))
  62. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(foreColor, lineh * 2))
  63. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  64. g.DrawString(" Original image ", tf, New PointF(0, 0))
  65. g.DrawString(" OtsuThresholdingEffect ", tf, New PointF(w + lineh, 0))
  66. g.DrawString(" BradleyThresholdingEffect ", tf, New PointF(0, h + lineh))
  67. g.DrawString(" BradleyThresholdingEffect(16,6) ", tf, New PointF(w + lineh, h + lineh))
  68. End Using
  69. End Using
  70. '' Done
  71. Return bmp
  72. End Function
  73. End Class
  74.