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

'' This sample demonstrates how to render a watermark on an image
'' at an angle using a rotation transformation on the bitmap graphics.
Public Class Watermark2
    Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        '' Note: we can use Color.Transparent instead Of a solid background,
        '' but the resulting image format must support transparency for this
        '' to work as expected:
        Dim backColor = Color.FromArgb(&HFF0066CC)
        Dim foreColor = Color.FromArgb(&HFFFFCC00)
        Dim angle = -30
        Dim rad = (angle * Math.PI) / 180.0F

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        Using bmpSrc = New GcBitmap(Path.Combine("Resources", "ImagesBis", "alpamayo-sq.jpg"))
            '' BitBlt requires the opacity of both images to be the same:
            bmpSrc.Opaque = opaque
            '' Render source image onto the target bitmap
            '' (generally we might want to resize the source image first,
            '' but in this case we just know that the source image has
            '' the same size as the target, so skip this step):
            bmp.BitBlt(bmpSrc, 0, 0)
        End Using

        Using g = bmp.CreateGraphics()
            '' Draw watermark text in a loop over all image at an angle:
            g.Transform = Matrix3x2.CreateRotation((angle * Math.PI) / 180.0F, New Vector2(pixelSize.Width / 2, pixelSize.Height / 2))
            Dim tf = New TextFormat() With
                {
                    .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
                    .FontSize = 14,
                    .ForeColor = Color.FromArgb(64, Color.White)
                }
            Dim tl = g.CreateTextLayout()
            tl.Append("Copyright (c) MESCIUS", tf)
            tl.PerformLayout(True)
            Dim dx = tl.ContentWidth * 3
            Dim dy = tl.ContentHeight * 5
            Dim n = 0
            Dim offX = -(Math.Cos(rad) * pixelSize.Height / 2)
            Dim offY = (Math.Sin(rad) * pixelSize.Width / 2)
            For y = offY To pixelSize.Height - offY Step dy
                For x = offX + dx / 2 * (n Mod 2) To pixelSize.Width - offX Step dx
                    g.DrawTextLayout(tl, New PointF(x, y))
                Next
                n += 1
            Next
        End Using
        Return bmp
    End Function
End Class