BackgroundBitmap.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

'' このサンプルでは、ビットマップへの描画における
'' BitmapRenderer.BackgroundBitmap の使用方法を紹介します。
''
'' 通常、半透明の図形オブジェクトを描画する場合、各ピクセルの最終的な色は
'' 描画先ビットマップのピクセルカラーと、図形オブジェクトのピクセルカラーを
'' 組み合わせたものになります。
'' しかし、BitmapRenderer に BackgroundBitmap が設定されている場合は、
'' 最終的な色を決定する際、描画先ビットマップのピクセルの代わりに、
'' その背景ビットマップのピクセルが計算に使用されます。
'' 背景ビットマップは、PDF を画像としてレンダリングする際に、
'' 「分離(Isolated)」および「ノックアウト(Knockout)」透明グループ
'' (PDF仕様 1.7 のセクション 7.3.4 および 7.3.5 で定義)を
'' サポートするために使用されます。
Public Class BackgroundBitmap
    Public Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim hx = pixelSize.Width \ 2
        Dim hy = pixelSize.Height \ 2

        '' 背景となるスペクトル画像
        Using bmp0 As New GcBitmap(Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png")),
              bmp1 = bmp0.Resize(hx, hy, InterpolationMode.Cubic),
              bmpBackdrop As New GcBitmap(hx, hy, False, dpi, dpi),
              bmpInitial As New GcBitmap(hx, hy, False, dpi, dpi)

            Using gB = bmpBackdrop.CreateGraphics(),
                  gI = bmpInitial.CreateGraphics()

                gB.Renderer.Aliased = True
                gB.Renderer.BlendMode = BlendMode.Multiply
                gI.Renderer.Aliased = True
                gI.Renderer.BlendMode = BlendMode.Multiply

                '' 描画先ビットマップは4つのデモ用区画で構成されます。
                Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, False, dpi, dpi)

                '' 分離(Isolated)、ノックアウト(Knockout)
                bmpBackdrop.BitBlt(bmp1, 0, 0)
                bmpInitial.Clear(Color.Transparent)
                gB.Renderer.BackgroundBitmap = bmpInitial
                FillCircles(gB, hx, hy)
                bmp.BitBlt(bmpBackdrop, 0, 0)

                '' 分離 (Isolated)、非ノックアウト (Non-knockout)
                FillCircles(gI, hx, hy)
                bmpBackdrop.BitBlt(bmp1, 0, 0)
                bmpBackdrop.AlphaBlend(bmpInitial, 0, 0)
                bmp.BitBlt(bmpBackdrop, hx, 0)

                '' 非分離 (Non-isolated)、ノックアウト (Knockout)
                bmpBackdrop.BitBlt(bmp1, 0, 0)
                bmpInitial.BitBlt(bmp1, 0, 0)
                FillCircles(gB, hx, hy)
                bmp.BitBlt(bmpBackdrop, 0, hy)

                '' 非分離 (Non-isolated)、非ノックアウト (Non-knockout)
                bmpBackdrop.BitBlt(bmp1, 0, 0)
                gB.Renderer.BackgroundBitmap = Nothing
                FillCircles(gB, hx, hy)
                bmp.BitBlt(bmpBackdrop, hx, hy)

                '' 装飾 (Adornments)
                Using g = bmp.CreateGraphics()
                    g.DrawLine(0, hy, bmp.PixelWidth, hy, New GCDRAW.Pen(Color.Black))
                    g.DrawLine(hx, 0, hx, bmp.PixelHeight, New GCDRAW.Pen(Color.Black))

                    Dim tf = New TextFormat() With {
                        .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMonoBold.ttf")),
                        .FontSize = 16
                    }
                    Dim d = 40
                    g.DrawString("Isolated, Knockout", tf, New RectangleF(0, hy - d, hx, d),
                                 TextAlignment.Center, ParagraphAlignment.Center, False)
                    g.DrawString("Isolated, Non-knockout", tf, New RectangleF(hx, hy - d, hx, d),
                                 TextAlignment.Center, ParagraphAlignment.Center, False)
                    g.DrawString("Non-isolated, Knockout", tf, New RectangleF(0, hy * 2 - d, hx, d),
                                 TextAlignment.Center, ParagraphAlignment.Center, False)
                    g.DrawString("Non-isolated, Non-knockout", tf, New RectangleF(hx, hy * 2 - d, hx, d),
                                 TextAlignment.Center, ParagraphAlignment.Center, False)
                End Using

                '' 画像ファイルを保存します。
                Return bmp
            End Using
        End Using
    End Function

    Private Sub FillCircles(ByVal g As GcBitmapGraphics, ByVal hx As Single, ByVal hy As Single)
        Dim dx = hx / 10.0F
        Dim dy = hy / 10.0F
        Dim qx = hx / 2.0F
        Dim qy = hy / 2.0F

        g.FillEllipse(New RectangleF(dx, dy, qx, qy), Color.LightGray)
        g.FillEllipse(New RectangleF(qx - dx, dy, qx, qy), Color.LightGray)
        g.FillEllipse(New RectangleF(dx, qy - dy, qx, qy), Color.LightGray)
        g.FillEllipse(New RectangleF(qx - dx, qy - dy, qx, qy), Color.LightGray)
    End Sub
End Class