Indexing.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, Inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample demonstrates how convert full color RGB
'' to 4 BPP and 8 BPP indexed images.
Public Class Indexing
    Function GenerateImage(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) As GcBitmap
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY)
        Using origBmp = New GcBitmap()
            '' Load a sample photo:
            Dim imagePath = Path.Combine("Resources", "Images", "maple.jpg")
            Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                origBmp.Load(stm)
            End Using

            origBmp.SetAlphaTo255()
            origBmp.Opaque = opaque

            '' Resize the original photo so we can place 4 samples of it
            '' on the resulting bitmap:
            Dim w = pixelWidth / 2
            Dim h = pixelHeight / 2
            Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic),
                ib4bpp = sizedBmp.ToIndexed4bppBitmap(DitheringMethod.JarvisJudiceNinke),
                ib8bpp = sizedBmp.ToIndexed8bppBitmap(DitheringMethod.JarvisJudiceNinke),
                b4bpp = ib4bpp.ToGcBitmap(),
                b8bpp = ib8bpp.ToGcBitmap()
                '' Copy the original And indexed images into 4 quadrants of the resulting bitmap:
                bmp.BitBlt(sizedBmp, 0, 0)
                b4bpp.Opaque = opaque
                bmp.BitBlt(b4bpp, w, 0)
                b8bpp.Opaque = opaque
                bmp.BitBlt(b8bpp, 0, h)
                bmp.BitBlt(sizedBmp, w, h)
            End Using

            '' Add borders between the quadrants, And captions for each:
            Dim lineh = 2
            Using g = bmp.CreateGraphics(Nothing)
                Dim foreColor = Color.Yellow
                Dim backColor = Color.Blue
                Dim fnt = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
                g.DrawLine(w, 0, w, h * 2, New Pen(foreColor, lineh * 2))
                g.DrawLine(0, h, w * 2, h, New Pen(foreColor, lineh * 2))
                Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
                g.DrawString(" Original image ", tf, New PointF(0, 0))
                g.DrawString(" Indexed 4 bits per pixel ", tf, New PointF(w + lineh, 0))
                g.DrawString(" Indexed 8 bits per pixel ", tf, New PointF(0, h + lineh))
                g.DrawString(" Original image ", tf, New PointF(w + lineh, h + lineh))
            End Using
            Return bmp
        End Using
    End Function
End Class