TransparencyMask.vb
''
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' ビットマップに描画する際の TransparencyMaskBitmap の使用方法を示しています。
'' 使用される透明マスクは、中間ビットマップを 0 (黒、透明) から 255 (白、不透明) までの
'' 線形グラデーションブラシで塗りつぶすことによって、リアルタイムで作成されます。
Public Class TransparencyMask
Public Function GenerateImage(
ByVal pixelSize As Size,
ByVal dpi As Single,
ByVal opaque As Boolean,
Optional ByVal sampleParams As String() = Nothing) As GcBitmap
Dim h2 As Integer = pixelSize.Width \ 2
'' 0(透明)から255(不透明)への線形グラデーション透過マスクを準備します。
Using mask As New GcBitmap(h2, h2, True)
Using gmask = mask.CreateGraphics()
Dim grad = New LinearGradientBrush(Color.Black, Color.White)
gmask.FillRectangle(New RectangleF(0, 0, mask.Width, mask.Height), grad)
End Using
'' Renderer.TransparencyMaskBitmap として使用するために GrayscaleBitmap に変換します。
Using gsb = mask.ToGrayscaleBitmap()
'' 描画対象のビットマップを作成し、黄色い背景で塗りつぶします。
Using bmp1 As New GcBitmap(h2, h2, True)
Using g = bmp1.CreateGraphics(Color.Yellow)
'' 透過マスクを適用します。
g.Renderer.TransparencyMaskBitmap = gsb
'' 3つの円を描画します。グラデーションに沿って、塗りつぶしが
'' 透明から不透明へ(左から右へ)徐々に変化することに注目してください。
Dim d As Single = CSng(h2) / 25.0!
Dim rc As New RectangleF(0, 0, h2 * 0.7F, h2 * 0.7F)
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
bmp.Clear(Color.White)
FillCircles(g, rc, h2, d)
bmp.BitBlt(bmp1, 0, 0)
g.Renderer.TransparencyMaskBitmap = Nothing
FillCircles(g, rc, h2, d)
bmp.BitBlt(bmp1, h2, 0)
'' 画像の下に説明文を追加します。
Dim gg = bmp.CreateGraphics()
rc = New RectangleF(0, h2 + d, h2 - d * 2, h2 - d * 2)
Dim tf = New TextFormat() With {
.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.ttf")),
.FontSize = 16
}
gg.DrawString(
"The circles in the image above were rendered with TransparencyMaskBitmap " &
"on the target graphics set to a linear gradient from transparent to opaque.",
tf, rc, TextAlignment.Center, ParagraphAlignment.Near)
rc.Offset(h2, 0)
gg.DrawString(
"The circles in the image above were rendered with no TransparencyMaskBitmap " &
"on the target graphics.",
tf, rc, TextAlignment.Center, ParagraphAlignment.Near)
Return bmp
End Using
End Using
End Using
End Using
End Function
Private Sub FillCircles(ByVal g As GcBitmapGraphics, ByVal rc As RectangleF, ByVal h2 As Single, ByVal d As Single)
Dim r = rc
r.Offset((h2 - rc.Width) / 2, 0)
r.Inflate(-d, -d)
g.FillEllipse(r, Color.Red)
r = rc
r.Offset(h2 - rc.Width, h2 - rc.Height)
r.Inflate(-d, -d)
g.FillEllipse(r, Color.Green)
r = rc
r.Offset(0, h2 - rc.Height)
r.Inflate(-d, -d)
g.FillEllipse(r, Color.Blue)
End Sub
End Class