Watermark2.vb
''
'' This code is part of GrapeCity Documents for Imaging samples.
'' Copyright (c) GrapeCity, 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

'' 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(
            Optional pixelWidth As Integer = 1024,
            Optional pixelHeight As Integer = 1024,
            Optional opaque As Boolean = True,
            Optional dpiX As Single = 96,
            Optional dpiY As Single = 96) 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(pixelWidth, pixelHeight, opaque, dpiX, dpiY)
        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(pixelWidth / 2, pixelHeight / 2))
            Dim tf = New TextFormat() With
                {
                    .Font = Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
                    .FontSize = 14,
                    .ForeColor = Color.FromArgb(64, Color.White)
                }
            Dim tl = g.CreateTextLayout()
            tl.Append("Copyright (c) GrapeCity", tf)
            tl.PerformLayout(True)
            Dim dx = tl.ContentWidth * 3
            Dim dy = tl.ContentHeight * 5
            Dim n = 0
            Dim offX = -(Math.Cos(rad) * pixelHeight / 2)
            Dim offY = (Math.Sin(rad) * pixelWidth / 2)
            For y = offY To pixelHeight - offY Step dy
                For x = offX + dx / 2 * (n Mod 2) To pixelWidth - offX Step dx
                    g.DrawTextLayout(tl, New PointF(x, y))
                Next
                n += 1
            Next
        End Using
        Return bmp
    End Function
End Class