ExtractFrames.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 shows how to extract frames from a TIFF image,
  17. '' and how to read them as GcBitmap instances.
  18. Public Class ExtractFrames
  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. '' Create the resulting image:
  26. Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  27.  
  28. '' If there is a possibility that we will not fill some areas of the target bitmap
  29. '' (as in this case where the number of frames in the TIFF may be less than 4),
  30. '' we MUST clear the resulting bitmap as GcBitmap ctor does NOT clear the memory
  31. '' allocated for the pixels (to save time, as initializing large bitmaps may be
  32. '' very slow):
  33. bmp.Clear()
  34.  
  35. '' Sample TIFF image path:
  36. Dim imagePath = Path.Combine("Resources", "ImagesBis", "BmpWriteTiff3.tiff")
  37.  
  38. '' List to hold bitmaps for the TIFF frames:
  39. Dim frameBmps(0) As GcBitmap
  40.  
  41. '' Read all frames from the TIFF file:
  42. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  43. Using tr = New GcTiffReader(stm)
  44. ReDim frameBmps(tr.Frames.Count - 1)
  45. For i = 0 To tr.Frames.Count - 1
  46. frameBmps(i) = tr.Frames(i).ToGcBitmap()
  47. frameBmps(i).Opaque = opaque
  48. Next
  49. End Using
  50. End Using
  51.  
  52. '' Resize and render the frames' bitmaps into the resulting image:
  53. Dim w = pixelSize.Width / 2
  54. Dim h = pixelSize.Height / 2
  55. If frameBmps.Length > 0 Then
  56. Using tbmp = frameBmps(0).Resize(w, h)
  57. bmp.BitBlt(tbmp, 0, 0)
  58. End Using
  59. End If
  60. If frameBmps.Length > 1 Then
  61. Using tbmp = frameBmps(1).Resize(w, h)
  62. bmp.BitBlt(tbmp, w, 0)
  63. End Using
  64. End If
  65. If frameBmps.Length > 2 Then
  66. Using tbmp = frameBmps(2).Resize(w, h)
  67. bmp.BitBlt(tbmp, 0, h)
  68. End Using
  69. End If
  70. If frameBmps.Length > 3 Then
  71. Using tbmp = frameBmps(3).Resize(w, h)
  72. bmp.BitBlt(tbmp, w, h)
  73. End Using
  74. End If
  75.  
  76. '' Dispose the frame bitmaps:
  77. For Each tbmp In frameBmps
  78. tbmp.Dispose()
  79. Next
  80.  
  81. '' Add borders between the quadrants, and captions for each:
  82. Using g = bmp.CreateGraphics()
  83. Dim foreColor = Color.Yellow
  84. Dim backColor = Color.Blue
  85. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  86. Dim lineh = 2
  87. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(Color.Gray, lineh * 2))
  88. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(Color.Gray, lineh * 2))
  89. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  90. Dim th = g.MeasureString("QWERTY", tf).Height
  91. If frameBmps.Length > 0 Then
  92. g.DrawString(" Frame 0 ", tf, New PointF(0, h - th + lineh))
  93. End If
  94. If frameBmps.Length > 1 Then
  95. g.DrawString(" Frame 1 ", tf, New PointF(w + lineh, h - th + lineh))
  96. End If
  97. If frameBmps.Length > 2 Then
  98. g.DrawString(" Frame 2 ", tf, New PointF(0, h * 2 + lineh - th + lineh))
  99. End If
  100. If frameBmps.Length > 3 Then
  101. g.DrawString(" Frame 3 ", tf, New PointF(w + lineh, h * 2 + lineh - th + lineh))
  102. End If
  103. End Using
  104.  
  105. Return bmp
  106. End Function
  107. End Class
  108.  
  109.