EnlargeQRCode.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
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 the results of enlarging a small (57 by 57 pixels) image
'' of a QRCode using the 4 available interpolation modes.
'' While for most images (such as portraits or landscapes), Linear or Cubic
'' interpolation modes would normally yield better-looking results,
'' for images like QRCodes or barcodes using the NearestNeighbor or
'' Downscale modes is more appropriate.
''
'' See EnlargeImage for an example where a small photo (portrait of a woman)
'' is enlarged using the same 4 interpolation modes.
Public Class EnlargeQRCode
    Public Function GenerateImage(
            ByVal pixelSize As Size,
            ByVal dpi As Single,
            ByVal opaque As Boolean,
            Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        '' Create and clear the target bitmap:
        Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        targetBmp.Clear(Color.Transparent)

        Const fontSize As Integer = 16
        Dim xpad As Integer = CInt(dpi * 0.5F)
        Dim ypad As Integer = CInt(dpi * 0.7F)
        Dim tf As New TextFormat() With {
            .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf")),
            .FontSize = fontSize
        }

        '' Small image of a QRCode:
        Using origBmp As New GcBitmap()
            Using stm = File.OpenRead(Path.Combine("Resources", "ImagesBis", "QRCode-57x57.png"))
                origBmp.Load(stm)
            End Using

            '' Make sure opaqueness of the original bitmap matches the target:
            origBmp.Opaque = targetBmp.Opaque

            Dim ip As New Point(xpad, ypad)

            '' Draw the original with the original size:
            targetBmp.BitBlt(origBmp, ip.X, ip.Y)
            Using g = targetBmp.CreateGraphics(Nothing)
                g.DrawString($"⟵ Original image ({origBmp.PixelWidth} by {origBmp.PixelHeight} pixels)",
                             tf,
                             New PointF(xpad * 2 + origBmp.Width, ip.Y))
            End Using
            ip.Y += origBmp.PixelHeight + ypad

            '' We are going to enlarge the original small image by a factor of 6:
            Dim f As Integer = 6
            Dim twidth As Integer = origBmp.PixelWidth * f
            Dim theight As Integer = origBmp.PixelHeight * f

            '' Enlarge and draw 4 copies of the image using the 4 available interpolation modes:
            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor)
                targetBmp.BitBlt(bmp, ip.X, ip.Y)
            End Using
            drawCaption(targetBmp, tf, "InterpolationMode.NearestNeighbor", ip.X, ip.Y + theight)

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

            ip.Y += theight + ypad

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear)
                targetBmp.BitBlt(bmp, ip.X, ip.Y)
            End Using
            drawCaption(targetBmp, tf, "InterpolationMode.Linear", ip.X, ip.Y + theight)

            Using bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale)
                targetBmp.BitBlt(bmp, ip.X + twidth + xpad, ip.Y)
            End Using
            drawCaption(targetBmp, tf, "InterpolationMode.Downscale", ip.X + twidth + xpad, ip.Y + theight)
        End Using
        Return targetBmp
    End Function

    '' Local helper to draw captions:
    Sub drawCaption(ByRef targetBmp As GcBitmap, ByRef tf As TextFormat, ByVal caption As String, ByVal x As Single, ByVal y As Single)
        Using g = targetBmp.CreateGraphics(Nothing)
            g.DrawString(caption, tf, New PointF(x, y))
        End Using
    End Sub
End Class