CreateThumbnails.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 to resize (downscale) an existing image
'' to create a thumbnail, using different interpolation modes.
Public Class DownscaleImage
    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 origImagePath = Path.Combine("Resources", "Images", "minerva.jpg")
        '' Create and fill the target bitmap:
        Dim targetBmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using g = targetBmp.CreateGraphics(Color.FromArgb(&HFF004D99))
            '' creating a graphics with a fill color does what we need
        End Using
        Const fontSize = 16, fpad = 4, xpad = 10, ypad = 3
        Dim tf = New TextFormat With
            {
                .ForeColor = Color.FromArgb(&HFFFFCC00),
                .Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                .FontSize = fontSize
            }
        Using origBmp = New GcBitmap()
            Using stm = New FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                origBmp.Load(stm)
            End Using

            '' Bitwise copy the original image with the original size:
            targetBmp.BitBlt(origBmp, 0, 0)
            Using g = targetBmp.CreateGraphics(Nothing)
                g.DrawString($"Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, New PointF(fpad, origBmp.Height + fpad))
            End Using
            Dim dx = origBmp.Width + xpad

            '' Scale down the image using the various interpolation modes,
            '' and render those on the right of the original:
            Dim f = (origBmp.Height - ypad * 3) / origBmp.Height / 4
            Dim twidth = Math.Round(origBmp.Width * f)
            Dim theight = Math.Round(origBmp.Height * f)

            ''
            Dim drawCaption As Action(Of String, Single, Single) =
            Sub(caption, x, y)
                Using g = targetBmp.CreateGraphics(Nothing)
                    g.DrawString(caption, tf, New PointF(x + twidth + fpad, y + fpad))
                End Using
            End Sub

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor)
                targetBmp.BitBlt(bmp, dx, 0)
            End Using
            drawCaption("InterpolationMode.NearestNeighbor", dx, 0)

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear)
                targetBmp.BitBlt(bmp, dx, theight + ypad)
            End Using
            drawCaption("InterpolationMode.Linear", dx, theight + ypad)

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic)
                targetBmp.BitBlt(bmp, dx, (theight + ypad) * 2)
            End Using
            drawCaption("InterpolationMode.Cubic", dx, (theight + ypad) * 2)

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale)
                targetBmp.BitBlt(bmp, dx, (theight + ypad) * 3)
            End Using
            drawCaption("InterpolationMode.Downscale", dx, (theight + ypad) * 3)
        End Using
        Return targetBmp
    End Function
End Class