AutoLevels.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 GcBitmap.AutoLevel()
  17. '' to automatically adjust the output levels of an image.
  18. Public Class AutoLevels
  19. Function GenerateImage(
  20. ByVal pixelSize As Size,
  21. ByVal dpi As Single,
  22. ByVal opaque As Boolean,
  23. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  24.  
  25. opaque = True
  26. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  27. Using origBmp = New GcBitmap()
  28. '' Load a sample photo:
  29. Dim imagePath = Path.Combine("Resources", "ImagesBis", "red-yellow-wall.jpg")
  30. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  31. origBmp.Load(stm)
  32. End Using
  33.  
  34. '' Resize the original photo to fit two versions on the resulting bitmap:
  35. Dim w = pixelSize.Width
  36. Dim h = pixelSize.Height / 2
  37. Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
  38. '' Copy the resized original into the upper half of the resulting bitmap:
  39. bmp.BitBlt(sizedBmp, 0, 0)
  40. '' Auto adjust levels of the original And copy the result into the lower half:
  41. sizedBmp.AutoLevel()
  42. bmp.BitBlt(sizedBmp, 0, h)
  43. End Using
  44.  
  45. '' Add captions (original And adjusted images):
  46. Dim lineh = 2
  47. Using g = bmp.CreateGraphics(Nothing)
  48. Dim forColor = Color.Yellow
  49. Dim bakColor = Color.Blue
  50. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  51. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(Color.Gray, lineh * 2))
  52. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = forColor, .BackColor = bakColor, .FontBold = True}
  53. Dim th = g.MeasureString("QWERTY", tf).Height
  54. g.DrawString(" Original image ", tf, New PointF(0, h - th + lineh))
  55. g.DrawString(" Auto levels applied ", tf, New PointF(0, h * 2 + lineh - th + lineh))
  56. End Using
  57. End Using
  58. Return bmp
  59. End Function
  60. End Class
  61.