BlendingModes.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.Collections.Generic
  8. Imports System.Linq
  9. Imports System.Numerics
  10. Imports GrapeCity.Documents.Drawing
  11. Imports GrapeCity.Documents.Text
  12. Imports GrapeCity.Documents.Imaging
  13. Imports GCTEXT = GrapeCity.Documents.Text
  14. Imports GCDRAW = GrapeCity.Documents.Drawing
  15.  
  16. '' This sample demonstrates composing two images using a few
  17. '' of the many available blending modes.
  18. '' The base image is a photo of a white orchid on a dark background.
  19. '' The blended image is a simple spectrum (thumbnail shown in the bottom right corner).
  20. '' See the AllBlendingModes sample for a demo of all available blending modes.
  21. Public Class BlendingModes
  22. Function GenerateImage(
  23. ByVal pixelSize As Size,
  24. ByVal dpi As Single,
  25. ByVal opaque As Boolean,
  26. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  27.  
  28. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  29. Using origBmp = New GcBitmap(), spectrumBmp = New GcBitmap()
  30. '' Load a sample photo:
  31. Dim imagePath = Path.Combine("Resources", "ImagesBis", "orchid.jpg")
  32. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  33. origBmp.Load(stm)
  34. End Using
  35.  
  36. '' Blending source:
  37. Dim spectrumPath = Path.Combine("Resources", "ImagesBis", "spectrum-500x500.png")
  38. Using stm = New FileStream(spectrumPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  39. spectrumBmp.Load(stm)
  40. End Using
  41.  
  42. origBmp.Opaque = opaque
  43. spectrumBmp.Opaque = opaque
  44.  
  45. '' Resize the original photo so we can place 4 samples of it
  46. '' on the resulting bitmap:
  47. Dim w = pixelSize.Width / 2
  48. Dim h = pixelSize.Height / 2
  49. Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic), sizedSpectrumBmp = spectrumBmp.Resize(w, h)
  50. '' Resized original image:
  51. bmp.BitBlt(sizedBmp, 0, 0)
  52.  
  53. '' Blending mode Color
  54. bmp.BitBlt(sizedBmp, w, 0)
  55. bmp.CompositeAndBlend(sizedSpectrumBmp, w, 0, CompositeMode.SourceOver, BlendMode.Color)
  56.  
  57. '' Blending mode SoftLight
  58. bmp.BitBlt(sizedBmp, 0, h)
  59. bmp.CompositeAndBlend(sizedSpectrumBmp, 0, h, CompositeMode.SourceOver, BlendMode.SoftLight)
  60.  
  61. '' Blending mode Hue
  62. bmp.BitBlt(sizedBmp, w, h)
  63. bmp.CompositeAndBlend(sizedSpectrumBmp, w, h, CompositeMode.SourceOver, BlendMode.Hue)
  64. End Using
  65. '' Show a thumbnail of the blend source in the bottom right corner
  66. Using spectrumThumbnail = spectrumBmp.Resize(w / 4, h / 4)
  67. bmp.BitBlt(spectrumThumbnail, pixelSize.Width - spectrumThumbnail.PixelWidth, pixelSize.Height - spectrumThumbnail.PixelHeight)
  68. End Using
  69. '' Add borders between the quadrants, And captions for each:
  70. Dim lineh = 2
  71. Using g = bmp.CreateGraphics(Nothing)
  72. Dim foreColor = Color.Yellow
  73. Dim backColor = Color.Blue
  74. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  75. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(Color.Gray, lineh * 2))
  76. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(Color.Gray, lineh * 2))
  77. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  78. Dim th = g.MeasureString("QWERTY", tf).Height
  79. g.DrawString(" Original image ", tf, New PointF(0, h - th + lineh))
  80. g.DrawString(" BlendMode.Color ", tf, New PointF(w + lineh, h - th + lineh))
  81. g.DrawString(" BlendMode.SoftLight ", tf, New PointF(0, h * 2 + lineh - th + lineh))
  82. g.DrawString(" BlendMode.Hue ", tf, New PointF(w + lineh, h * 2 + lineh - th + lineh))
  83. End Using
  84. End Using
  85. Return bmp
  86. End Function
  87. End Class
  88.