EnlargeImage.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 shows how to enlarge a loaded image
'' using different interpolation modes.
Public Class EnlargeImage
    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", "Stock", "woman-window-small.jpg")
        '' Create and clear the target bitmap:
        Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        targetBmp.Clear(Color.Transparent)

        Const fontSize = 16, fpad = 4, xpad = 4, ypad = 3
        Dim tf = New TextFormat With
        {
            .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

            '' Draw the original 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(origBmp.Width + fpad, fpad))
            End Using
            Dim dy = origBmp.Height + ypad

            '' Enlarge image so that we can have 4 enlarged tiled images preserving aspect ratio:
            Dim f = Math.Min(
                (targetBmp.Width - xpad) / origBmp.Width / 2,
                (targetBmp.Height - ypad - dy) / origBmp.Height / 2)
            Dim twidth = (origBmp.Width * f)
            Dim theight = (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 RectangleF(x + fpad, y + theight - fontSize - fpad * 2, twidth - fpad * 2, fontSize + fpad * 2))
                End Using
            End Sub

            '' Enlarge and draw 4 copies using the 4 available interpolation modes:
            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor)
                targetBmp.BitBlt(bmp, 0, dy)
            End Using
            drawCaption("InterpolationMode.NearestNeighbor", 0, dy)

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

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic)
                targetBmp.BitBlt(bmp, twidth + xpad, dy)
            End Using
            drawCaption("InterpolationMode.Cubic", twidth + xpad, dy)

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale)
                targetBmp.BitBlt(bmp, twidth + xpad, dy + theight + ypad)
            End Using
            drawCaption("InterpolationMode.Downscale", twidth + xpad, dy + theight + ypad)
            ''
        End Using
        Return targetBmp
    End Function
End Class