Indexing.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 convert full color RGB
  15. '' to 4 BPP and 8 BPP indexed images.
  16. Public Class Indexing
  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 bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
  24. Using origBmp = New GcBitmap()
  25. '' Load a sample photo:
  26. Dim imagePath = Path.Combine("Resources", "Images", "maple.jpg")
  27. Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
  28. origBmp.Load(stm)
  29. End Using
  30.  
  31. origBmp.SetAlphaTo255()
  32. origBmp.Opaque = opaque
  33.  
  34. '' Resize the original photo so we can place 4 samples of it
  35. '' on the resulting bitmap:
  36. Dim w = pixelSize.Width / 2
  37. Dim h = pixelSize.Height / 2
  38. Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic),
  39. ib4bpp = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.JarvisJudiceNinke),
  40. ib8bpp = sizedBmp.ToIndexed8bppBitmap(DitheringMethod.JarvisJudiceNinke),
  41. b4bpp = ib4bpp.ToGcBitmap(),
  42. b8bpp = ib8bpp.ToGcBitmap()
  43. '' Copy the original And indexed images into 4 quadrants of the resulting bitmap:
  44. bmp.BitBlt(sizedBmp, 0, 0)
  45. b4bpp.Opaque = opaque
  46. bmp.BitBlt(b4bpp, w, 0)
  47. b8bpp.Opaque = opaque
  48. bmp.BitBlt(b8bpp, 0, h)
  49. bmp.BitBlt(sizedBmp, w, h)
  50. End Using
  51.  
  52. '' Add borders between the quadrants, And captions for each:
  53. Dim lineh = 2
  54. Using g = bmp.CreateGraphics(Nothing)
  55. Dim foreColor = Color.Yellow
  56. Dim backColor = Color.Blue
  57. Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
  58. g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(foreColor, lineh * 2))
  59. g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(foreColor, lineh * 2))
  60. Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
  61. g.DrawString(" Original image ", tf, New PointF(0, 0))
  62. g.DrawString(" Indexed 4 bits per pixel ", tf, New PointF(w + lineh, 0))
  63. g.DrawString(" Indexed 8 bits per pixel ", tf, New PointF(0, h + lineh))
  64. g.DrawString(" Original image ", tf, New PointF(w + lineh, h + lineh))
  65. End Using
  66. Return bmp
  67. End Using
  68. End Function
  69. End Class
  70.