InterpolationModes.vb
''
'' This code is part of Document Solutions for PDF .NET demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Pdf
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Drawing
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

Public Class InterpolationModes
    Public Function CreatePDF(ByVal stream As Stream) As Integer
        ' Small image of a QRCode that will be enlarged in PDFs that are then saved as JPEGs:
        Using image = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "QRCode-57x57.png"))
            ' Create 4 PDFs, each is one page on which the small image is enlarged,
            ' save each PDF as JPEG with different interpolation modes specified in SaveAsImageOptions:
            Dim tmpNearestNeighbor = Path.GetTempFileName()
            MakePDF(image, InterpolationMode.NearestNeighbor).SaveAsJpeg(tmpNearestNeighbor, Nothing,
                New SaveAsImageOptions() With {.InterpolationMode = InterpolationMode.NearestNeighbor})
            Dim tmpLinear = Path.GetTempFileName()
            MakePDF(image, InterpolationMode.Linear).SaveAsJpeg(tmpLinear, Nothing,
                New SaveAsImageOptions() With {.InterpolationMode = InterpolationMode.Linear})
            Dim tmpCubic = Path.GetTempFileName()
            MakePDF(image, InterpolationMode.Cubic).SaveAsJpeg(tmpCubic, Nothing,
                New SaveAsImageOptions() With {.InterpolationMode = InterpolationMode.Cubic})
            Dim tmpDownscale = Path.GetTempFileName()
            MakePDF(image, InterpolationMode.Downscale).SaveAsJpeg(tmpDownscale, Nothing,
                New SaveAsImageOptions() With {.InterpolationMode = InterpolationMode.Downscale})

            ' Draw each JPEG at its natural size onto a page of the resulting PDF:
            Dim pageCount As Integer = 0
            Using imgNearestNeighbor = GCDRAW.Image.FromFile(tmpNearestNeighbor),
                  imgLinear = GCDRAW.Image.FromFile(tmpLinear),
                  imgCubic = GCDRAW.Image.FromFile(tmpCubic),
                  imgDownscale = GCDRAW.Image.FromFile(tmpDownscale)
                Dim doc = New GcPdfDocument()
                Dim page = doc.NewPage()
                page.Graphics.DrawImage(imgNearestNeighbor, page.Bounds, Nothing, ImageAlign.Default)
                page = doc.NewPage()
                page.Graphics.DrawImage(imgLinear, page.Bounds, Nothing, ImageAlign.Default)
                page = doc.NewPage()
                page.Graphics.DrawImage(imgCubic, page.Bounds, Nothing, ImageAlign.Default)
                page = doc.NewPage()
                page.Graphics.DrawImage(imgDownscale, page.Bounds, Nothing, ImageAlign.Default)
                ' Save the resulting PDF:
                doc.Save(stream)
                pageCount = doc.Pages.Count
            End Using
            ' Cleanup:
            File.Delete(tmpNearestNeighbor)
            File.Delete(tmpLinear)
            File.Delete(tmpCubic)
            File.Delete(tmpDownscale)

            '' Done:
            Return pageCount
        End Using
    End Function

    ' InterpolationMode parameter here is only to show in the resulting PDF,
    ' GcPdfGraphics only supports InterpolationMode.NearestNeighbor:
    Public Function MakePDF(ByVal image As GCDRAW.Image, ByVal imode As InterpolationMode) As GcPdfDocument
        Dim tfCaption = New TextFormat() With {
            .Font = StandardFonts.Times,
            .FontSize = 20
        }
        Dim tf = New TextFormat() With {
            .Font = StandardFonts.Times,
            .FontSize = 18
        }

        Dim doc = New GcPdfDocument()
        Dim page = doc.NewPage()
        Dim g = page.Graphics

        Dim xpad = 36
        Dim ypad = 36
        Dim ip = New PointF(xpad, ypad)
        g.DrawString($"SaveAsImageOptions.InterpolationMode is {imode}", tfCaption, ip)
        ip.Y *= 2.5F

        ' Draw the original with the original size:
        g.DrawImage(image, New RectangleF(ip.X, ip.Y, image.Width, image.Height), Nothing, ImageAlign.Default)
        g.DrawString($"⟵ Original image ({image.Width} by {image.Height} pixels)", tf, New PointF(ip.X * 2 + image.Width, ip.Y))
        ip.Y += image.Height + ypad

        ' Enlarge the original small image by a factor of 8:
        Dim f = 8
        Dim twidth As Integer = image.Width * f
        Dim theight As Integer = image.Height * f

        ' Draw the enlarged image:
        g.DrawImage(image, New RectangleF(ip.X, ip.Y, twidth, theight), Nothing, ImageAlign.StretchImage)
        g.DrawString($"Image enlarged to {twidth} by {theight} pixels:", tf, New PointF(ip.X, ip.Y - ypad))

        Return doc
    End Function
End Class