GammaCorrection.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 how to use the GammaCorrectionEffect.
  17. Public Class GammaCorrection
  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", "ImagesBis", "butterfly.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. '' Copy the resized original into 4 quadrants of the resulting bitmap:
  42. bmp.BitBlt(sizedBmp, 0, 0)
  43. bmp.BitBlt(sizedBmp, w, 0)
  44. bmp.BitBlt(sizedBmp, 0, h)
  45. bmp.BitBlt(sizedBmp, w, h)
  46. End Using
  47.  
  48. '' Keep the pixels in top left quadrant intact,
  49. '' apply effects to the other 3 quadrants:
  50. Dim lineh = 2
  51. bmp.ApplyEffect(GammaCorrectionEffect.Get(5), New Rectangle(w + lineh, 0, w - lineh, h - lineh))
  52. bmp.ApplyEffect(GammaCorrectionEffect.Get(0.5F), New Rectangle(0, h + lineh, w - lineh, h - lineh))
  53. bmp.ApplyEffect(GammaCorrectionEffect.Get(0.1F), New Rectangle(w + lineh, h + lineh, w - lineh, h - lineh))
  54.  
  55. '' Add borders between the quadrants, and captions for each:
  56. Using g = bmp.CreateGraphics(Nothing)
  57. Dim foreColor = Color.Yellow
  58. Dim backColor = Color.Blue
  59. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  60. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(Color.Gray, lineh * 2))
  61. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(Color.Gray, lineh * 2))
  62. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  63. Dim th = g.MeasureString("QWERTY", tf).Height
  64. g.DrawString(" Original image ", tf, New PointF(0, h - th + lineh))
  65. g.DrawString(" Gamma Correction 5 ", tf, New PointF(w + lineh, h - th + lineh))
  66. g.DrawString(" Gamma Correction 0.5 ", tf, New PointF(0, h * 2 + lineh - th + lineh))
  67. g.DrawString(" Gamma Correction 0.1 ", tf, New PointF(w + lineh, h * 2 + lineh - th + lineh))
  68. End Using
  69. End Using
  70. Return bmp
  71. End Function
  72. End Class
  73.