CreateThumbnails.vb
'' 
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © 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

'' このサンプルは、異なる補間モードを使用して、
'' 既存の画像をリサイズ(縮小)し、サムネイルを作成する方法を示します。
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")
        '' ターゲットビットマップを作成して塗りつぶします。
        Dim targetBmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        Using g = targetBmp.CreateGraphics(Color.FromArgb(&HFF004D99))
            '' 背景色を指定してGraphicsを作成することで、塗りつぶし処理が行われます。
        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", "FreeSerif.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

            '' 元の画像を元のサイズでビットブロック転送(コピー)します。
            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

            '' 様々な補間モードを使用して画像を縮小し、それらを元の画像の右側に描画します。
            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