AutoContrast.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 GrayscaleBitmap.AutoContrast()
  17. '' to automatically adjust the contrast of a black and white image.
  18. Public Class AutoContrast
  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", "tremblant.png")
  30. Dim grayBmp As GrayscaleBitmap
  31. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  32. origBmp.Load(stm)
  33. origBmp.Opaque = opaque
  34. grayBmp = origBmp.ToGrayscaleBitmap()
  35. End Using
  36.  
  37. '' Resize the original photo to fit two versions on the resulting bitmap:
  38. Dim w = pixelSize.Width
  39. Dim h = pixelSize.Height / 2
  40. Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
  41. '' Copy the resized original into the upper half of the resulting bitmap:
  42. bmp.BitBlt(sizedBmp, 0, 0)
  43. '' Auto adjust the contrast of the original image and copy the result into the lower half:
  44. grayBmp.AutoContrast()
  45. Using tbmp = grayBmp.ToGcBitmap()
  46. bmp.BitBlt(tbmp, 0, h)
  47. End Using
  48. End Using
  49.  
  50. '' Add captions (original and adjusted images):
  51. Dim lineh = 2
  52. Using g = bmp.CreateGraphics(Nothing)
  53. Dim foreColor = Color.Yellow
  54. Dim backColor = Color.Blue
  55. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  56. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(Color.Gray, lineh * 2))
  57. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  58. Dim th = g.MeasureString("QWERTY", tf).Height
  59. g.DrawString(" Original image ", tf, New PointF(0, h - th + lineh))
  60. g.DrawString(" Auto contrast applied ", tf, New PointF(0, h * 2 + lineh - th + lineh))
  61. End Using
  62. End Using
  63. Return bmp
  64. End Function
  65. End Class
  66.