MatrixEffects2.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 LuminanceToAlphaEffect,
  15. '' OpacityEffect and SepiaEffect on an image.
  16. '' See also MatrixEffects1.
  17. Public Class MatrixEffects2
  18. Function GenerateImage(
  19. ByVal pixelSize As Size,
  20. ByVal dpi As Single,
  21. ByVal opaque As Boolean,
  22. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  23.  
  24. opaque = False
  25. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  26. Using origBmp = New GcBitmap()
  27. '' Load a sample photo:
  28. Dim imagePath = Path.Combine("Resources", "Images", "lavender.jpg")
  29. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  30. origBmp.Load(stm)
  31. End Using
  32.  
  33. origBmp.SetAlphaTo255()
  34. origBmp.Opaque = False
  35.  
  36. '' Resize the original photo so we can place 4 samples of it
  37. '' on the resulting bitmap:
  38. Dim w = pixelSize.Width / 2
  39. Dim h = pixelSize.Height / 2
  40. Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
  41. sizedBmp.Opaque = opaque
  42. '' Copy the resized original into 4 quadrants of the resulting bitmap:
  43. bmp.BitBlt(sizedBmp, 0, 0)
  44. bmp.BitBlt(sizedBmp, w, 0)
  45. bmp.BitBlt(sizedBmp, 0, h)
  46. bmp.BitBlt(sizedBmp, w, h)
  47. End Using
  48.  
  49. '' Moving the 3 lines with the "ApplyEffect" comment from below to here
  50. '' will apply the effects only to the photos but Not to the texts.
  51.  
  52. '' Add borders between the quadrants, And captions for each:
  53. Dim lineh = 2
  54. Using g = bmp.CreateGraphics(Nothing)
  55. Dim foreColor = Color.Yellow
  56. Dim backColor = Color.Blue
  57. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  58. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(foreColor, lineh * 2))
  59. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(foreColor, lineh * 2))
  60. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  61. g.DrawString(" Original image ", tf, New PointF(0, 0))
  62. g.DrawString(" LuminanceToAlphaEffect.Get() ", tf, New PointF(w + lineh, 0))
  63. g.DrawString(" OpacityEffect.Get(0.5f) ", tf, New PointF(0, h + lineh))
  64. g.DrawString(" SepiaEffect.Get() ", tf, New PointF(w + lineh, h + lineh))
  65. End Using
  66. '' ApplyEffect (move this code up to before drawing texts
  67. '' to limit it to photos only And Not affect the captions).
  68. ''
  69. '' Keep the pixels in top left quadrant intact,
  70. '' apply effects to the other 3 quadrants:
  71. bmp.ApplyEffect(LuminanceToAlphaEffect.Get(), New Rectangle(w + lineh, 0, w - lineh, h - lineh))
  72. bmp.ApplyEffect(OpacityEffect.Get(0.5), New Rectangle(0, h + lineh, w - lineh, h - lineh))
  73. bmp.ApplyEffect(SepiaEffect.Get(), New Rectangle(w + lineh, h + lineh, w - lineh, h - lineh))
  74. End Using
  75. '' Done
  76. Return bmp
  77. End Function
  78. End Class
  79.