BackgroundBitmap.vb
''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) 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

'' This sample demonstrates the use of BitmapRenderer.BackgroundBitmap
'' when drawing on a bitmap.
''
'' When drawing semi-transparent graphic objects, usually the resulting color
'' of a pixel is a combination of the target bitmap pixel's color and the color
'' of the graphic object's pixel. But if the BackgroundBitmap is set on the
'' BitmapRenderer, pixels of that bitmap will be used instead of the target
'' bitmap's pixels when determining the resulting color. Background bitmaps
'' are used to support Isolated and Knockout transparency groups (as defined
'' in the PDF specification 1.7 sections 7.3.4 and 7.3.5) when rendering PDFs
'' to images.
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

        '' The spectrum image used for the backdrop:
        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

                '' The target bitmap will contain 4 demo quadrants:
                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

                '' Done:
                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