DitheringInTiff.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 takes a color JPEG, And creates bilevel bitmaps from it
  17. '' using all available dithering methods. The resulting b/w images
  18. '' are added as frames to a multi-frame TIFF.
  19. '' The original color image Is added as the last page.
  20. Public Class DitheringInTiff
  21. Function GenerateImageStream(
  22. ByVal targetMime As String,
  23. ByVal pixelSize As Size,
  24. ByVal dpi As Single,
  25. ByVal opaque As Boolean,
  26. Optional sampleParams As String() = Nothing) As Stream
  27.  
  28. If Not targetMime = MimeTypes.TIFF Then
  29. Throw New Exception("This sample only supports TIFF output format.")
  30. End If
  31.  
  32. Dim pth = Path.Combine("Resources", "Images", "minerva.jpg")
  33. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  34. Dim ms = New MemoryStream()
  35. Using tw = New GcTiffWriter(ms), bmp = New GcBitmap(pth)
  36. '' Create text layout for labels:
  37. Dim tl = New TextLayout(bmp.DpiX)
  38. tl.DefaultFormat.Font = fnt
  39. tl.DefaultFormat.FontSize = 16
  40. tl.DefaultFormat.BackColor = Color.White
  41. '' Loop through all dithering methods (starting with no dithering):
  42. Dim methods = GetType(DitheringMethod).GetEnumValues()
  43. For Each method As DitheringMethod In methods
  44. Using tbmp = bmp.Clone()
  45. '' Apply dithering:
  46. tbmp.ApplyEffect(DitheringEffect.Get(method))
  47. '' Draw label:
  48. tl.Clear()
  49. tl.Append(method.ToString())
  50. tl.PerformLayout(True)
  51. Using g = tbmp.CreateGraphics()
  52. g.DrawTextLayout(tl, New PointF(0, tbmp.Height - tl.ContentHeight))
  53. End Using
  54. '' Convert to bilevel bitmap
  55. Using f = tbmp.ToBilevelBitmap()
  56. tw.AppendFrame(f)
  57. End Using
  58. End Using
  59. Next
  60. '' Add original image:
  61. tw.AppendFrame(bmp)
  62. End Using
  63. ms.Seek(0, SeekOrigin.Begin)
  64. Return ms
  65. End Function
  66.  
  67. Public ReadOnly Property DefaultMime() As String
  68. Get
  69. Return MimeTypes.TIFF
  70. End Get
  71. End Property
  72. End Class
  73.