CreateThumbnails.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 demonstrates how to resize (downscale) an existing image
  15. '' to create a thumbnail, using different interpolation modes.
  16. Public Class DownscaleImage
  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", "Images", "minerva.jpg")
  24. '' Create and fill the target bitmap:
  25. Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
  26. Using g = targetBmp.CreateGraphics(Color.FromArgb(&HFF004D99))
  27. '' creating a graphics with a fill color does what we need
  28. End Using
  29. Const fontSize = 16, fpad = 4, xpad = 10, ypad = 3
  30. Dim tf = New TextFormat With
  31. {
  32. .ForeColor = Color.FromArgb(&HFFFFCC00),
  33. .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
  34. .FontSize = fontSize
  35. }
  36. Using origBmp = New GcBitmap()
  37. Using stm = New FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  38. origBmp.Load(stm)
  39. End Using
  40.  
  41. '' Bitwise copy the original image with the original size:
  42. targetBmp.BitBlt(origBmp, 0, 0)
  43. Using g = targetBmp.CreateGraphics(Nothing)
  44. g.DrawString($"Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, New PointF(fpad, origBmp.Height + fpad))
  45. End Using
  46. Dim dx = origBmp.Width + xpad
  47.  
  48. '' Scale down the image using the various interpolation modes,
  49. '' and render those on the right of the original:
  50. Dim f = (origBmp.Height - ypad * 3) / origBmp.Height / 4
  51. Dim twidth = Math.Round(origBmp.Width * f)
  52. Dim theight = Math.Round(origBmp.Height * f)
  53.  
  54. ''
  55. Dim drawCaption As Action(Of String, Single, Single) =
  56. Sub(caption, x, y)
  57. Using g = targetBmp.CreateGraphics(Nothing)
  58. g.DrawString(caption, tf, New PointF(x + twidth + fpad, y + fpad))
  59. End Using
  60. End Sub
  61.  
  62. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor)
  63. targetBmp.BitBlt(bmp, dx, 0)
  64. End Using
  65. drawCaption("InterpolationMode.NearestNeighbor", dx, 0)
  66.  
  67. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear)
  68. targetBmp.BitBlt(bmp, dx, theight + ypad)
  69. End Using
  70. drawCaption("InterpolationMode.Linear", dx, theight + ypad)
  71.  
  72. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic)
  73. targetBmp.BitBlt(bmp, dx, (theight + ypad) * 2)
  74. End Using
  75. drawCaption("InterpolationMode.Cubic", dx, (theight + ypad) * 2)
  76.  
  77. Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale)
  78. targetBmp.BitBlt(bmp, dx, (theight + ypad) * 3)
  79. End Using
  80. drawCaption("InterpolationMode.Downscale", dx, (theight + ypad) * 3)
  81. End Using
  82. Return targetBmp
  83. End Function
  84. End Class
  85.