GrayscaleEffects.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
'' このサンプルでは、さまざまなグレースケール効果の使用方法を示します。
'' MatrixEffects1 および MatrixEffects2 のサンプルも参照してください。
Public Class GrayscaleEffects
Function GenerateImage(
ByVal pixelSize As Size,
ByVal dpi As Single,
ByVal opaque As Boolean,
Optional ByVal sampleParams As String() = Nothing) As GcBitmap
opaque = False
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
Using origBmp = New GcBitmap()
'' サンプル画像を読み込みます。
Dim imagePath = Path.Combine("Resources", "Images", "maple.jpg")
Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
origBmp.Load(stm)
End Using
origBmp.SetAlphaTo255()
origBmp.Opaque = False
'' 生成されるビットマップに4つのサンプルを配置するため、
'' 元の画像をリサイズします。
Dim w = pixelSize.Width / 2
Dim h = pixelSize.Height / 2
Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic)
'' リサイズした元の画像を、生成用ビットマップの4つの領域にコピーします。
bmp.BitBlt(sizedBmp, 0, 0)
bmp.BitBlt(sizedBmp, w, 0)
bmp.BitBlt(sizedBmp, 0, h)
bmp.BitBlt(sizedBmp, w, h)
End Using
'' 下記にある「ApplyEffect」の3行をここに移動させると、
'' キャプションには影響を与えず、写真のみにエフェクトを適用できます。
'' 各領域の間に境界線を引き、それぞれのキャプションを追加します。
Dim lineh = 2
Using g = bmp.CreateGraphics(Nothing)
Dim foreColor = Color.Yellow
Dim backColor = Color.Blue
Dim fnt = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.ttf"))
g.DrawLine(w, 0, w, h * 2, New GCDRAW.Pen(foreColor, lineh * 2))
g.DrawLine(0, h, w * 2, h, New GCDRAW.Pen(foreColor, lineh * 2))
Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
g.DrawString(" Original image ", tf, New PointF(0, 0))
g.DrawString(" GrayscaleStandard.BT601 ", tf, New PointF(w + lineh, 0))
g.DrawString(" GrayscaleStandard.BT709 ", tf, New PointF(0, h + lineh))
g.DrawString(" GrayscaleStandard.BT2100 ", tf, New PointF(w + lineh, h + lineh))
End Using
'' ApplyEffect(キャプションに影響を与えず写真のみに限定するには、
'' このコードをテキスト描画の前に移動させます)。
''
'' 左上の領域のピクセルはそのまま保持し、
'' 他の3つの領域にエフェクトを適用します。
bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT601), New Rectangle(w + lineh, 0, w - lineh, h - lineh))
bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT709), New Rectangle(0, h + lineh, w - lineh, h - lineh))
bmp.ApplyEffect(GrayscaleEffect.Get(GrayscaleStandard.BT2100), New Rectangle(w + lineh, h + lineh, w - lineh, h - lineh))
End Using
Return bmp
End Function
End Class