EnlargeImage.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 shows how to enlarge a loaded image
  15. '' using different interpolation modes.
  16. Public Class EnlargeImage
  17. Function GenerateImage(
  18. ByVal pixelSize As Size,
  19. ByVal dpi As Single,
  20. ByVal opaque As Boolean,
  21. Optional ByVal sampleParams As String() = Nothing) As GcBitmap
  22.  
  23. Dim origImagePath = Path.Combine("Resources", "Stock", "woman-window-small.jpg")
  24. '' Create and clear the target bitmap:
  25. Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  26. targetBmp.Clear(Color.Transparent)
  27.  
  28. Const fontSize = 16, fpad = 4, xpad = 4, ypad = 3
  29. Dim tf = New TextFormat With
  30. {
  31. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  32. .FontSize = fontSize
  33. }
  34. Using origBmp = New GcBitmap()
  35. Using stm = New FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  36. origBmp.Load(stm)
  37. End Using
  38.  
  39. '' Draw the original with the original size:
  40. targetBmp.BitBlt(origBmp, 0, 0)
  41. Using g = targetBmp.CreateGraphics(Nothing)
  42. g.DrawString($"⟵ Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, New PointF(origBmp.Width + fpad, fpad))
  43. End Using
  44. Dim dy = origBmp.Height + ypad
  45.  
  46. '' Enlarge image so that we can have 4 enlarged tiled images preserving aspect ratio:
  47. Dim f = Math.Min(
  48. (targetBmp.Width - xpad) / origBmp.Width / 2,
  49. (targetBmp.Height - ypad - dy) / origBmp.Height / 2)
  50. Dim twidth = (origBmp.Width * f)
  51. Dim theight = (origBmp.Height * f)
  52.  
  53. ''
  54. Dim drawCaption As Action(Of String, Single, Single) =
  55. Sub(caption, x, y)
  56. Using g = targetBmp.CreateGraphics(Nothing)
  57. g.DrawString(caption, tf, New RectangleF(x + fpad, y + theight - fontSize - fpad * 2, twidth - fpad * 2, fontSize + fpad * 2))
  58. End Using
  59. End Sub
  60.  
  61. '' Enlarge and draw 4 copies using the 4 available interpolation modes:
  62. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor)
  63. targetBmp.BitBlt(bmp, 0, dy)
  64. End Using
  65. drawCaption("InterpolationMode.NearestNeighbor", 0, dy)
  66.  
  67. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear)
  68. targetBmp.BitBlt(bmp, 0, dy + theight + ypad)
  69. End Using
  70. drawCaption("InterpolationMode.Linear", 0, dy + theight + ypad)
  71.  
  72. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic)
  73. targetBmp.BitBlt(bmp, twidth + xpad, dy)
  74. End Using
  75. drawCaption("InterpolationMode.Cubic", twidth + xpad, dy)
  76.  
  77. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale)
  78. targetBmp.BitBlt(bmp, twidth + xpad, dy + theight + ypad)
  79. End Using
  80. drawCaption("InterpolationMode.Downscale", twidth + xpad, dy + theight + ypad)
  81. ''
  82. End Using
  83. Return targetBmp
  84. End Function
  85. End Class
  86.