GrayscaleEffects.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 GrayscaleEffects
  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", "Images", "maple.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. '' Moving the 3 lines with the "ApplyEffect" comment from below to here
  47. '' will apply the effects only to the photos but Not to the texts.
  48.  
  49. '' Add borders between the quadrants, And captions for each:
  50. Dim lineh = 2
  51. Using g = bmp.CreateGraphics(Nothing)
  52. Dim foreColor = Color.Yellow
  53. Dim backColor = Color.Blue
  54. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  55. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(foreColor, lineh * 2))
  56. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(foreColor, lineh * 2))
  57. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  58. g.DrawString(" Original image ", tf, New PointF(0, 0))
  59. g.DrawString(" GrayscaleStandard.BT601 ", tf, New PointF(w + lineh, 0))
  60. g.DrawString(" GrayscaleStandard.BT709 ", tf, New PointF(0, h + lineh))
  61. g.DrawString(" GrayscaleStandard.BT2100 ", tf, New PointF(w + lineh, h + lineh))
  62. End Using
  63. '' ApplyEffect (move this code up to before drawing texts
  64. '' to limit it to photos only And Not affect the captions).
  65. ''
  66. '' Keep the pixels in top left quadrant intact,
  67. '' apply effects to the other 3 quadrants:
  68. bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT601), New Rectangle(w + lineh, 0, w - lineh, h - lineh))
  69. bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT709), New Rectangle(0, h + lineh, w - lineh, h - lineh))
  70. bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT2100), New Rectangle(w + lineh, h + lineh, w - lineh, h - lineh))
  71. End Using
  72. '' Done
  73. Return bmp
  74. End Function
  75. End Class
  76.