CreateThumbnails.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS 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
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' This sample demonstrates how to resize (downscale) an existing image
'' to create a thumbnail, using different interpolation modes.
Public Class DownscaleImage
    Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim origImagePath = Path.Combine("Resources", "Images", "minerva.jpg")
        '' Create and fill the target bitmap:
        Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        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 = GCTEXT.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