AutoLevels.vb
'' 
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
'' 
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
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

'' このサンプルでは、GcBitmap.AutoLevel() を使用して
'' 画像の出力レベルを自動調整する方法を示します。
Public Class AutoLevels
    Function GenerateImage(
            ByVal pixelSize As Size,
            ByVal dpi As Single,
            ByVal opaque As Boolean,
            Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        opaque = True
        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        Using origBmp = New GcBitmap()
            '' サンプル画像を読み込みます。
            Dim imagePath = Path.Combine("Resources", "ImagesBis", "red-yellow-wall.jpg")
            Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                origBmp.Load(stm)
            End Using

            '' 2つのバージョンを並べるため、元の画像をリサイズします。
            Dim w = pixelSize.Width
            Dim h = pixelSize.Height / 2
            Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
                '' リサイズした元の画像を上半分にコピーします。
                bmp.BitBlt(sizedBmp, 0, 0)
                '' レベルを自動調整し、結果を下半分にコピーします。
                sizedBmp.AutoLevel()
                bmp.BitBlt(sizedBmp, 0, h)
            End Using

            '' キャプション(元の画像と調整後の画像)を追加します。
            Dim lineh = 2
            Using g = bmp.CreateGraphics(Nothing)
                Dim forColor = Color.Yellow
                Dim bakColor = Color.Blue
                Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.ttf"))
                g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(Color.Gray, lineh * 2))
                Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = forColor, .BackColor = bakColor, .FontBold = True}
                Dim th = g.MeasureString("QWERTY", tf).Height
                g.DrawString(" Original image ", tf, New PointF(0, h - th + lineh))
                g.DrawString(" Auto levels applied ", tf, New PointF(0, h * 2 + lineh - th + lineh))
            End Using
        End Using
        Return bmp
    End Function
End Class