Watermark.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 GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging

'' This sample demonstrates how to add a text watermark
'' to an image. The image is rendered using its native
'' resolution on a GcBitmap, then the watermark text
'' is drawn on top using a semitransparent color.
'' The resulting bitmap with the added watermark
'' can be saved to any of the supported image formats.
Public Class Watermark
    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
        Dim Inch = dpiX
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using g = bmp.CreateGraphics(Color.White)
            Using img = Image.FromFile(Path.Combine("Resources", "Images", "reds.jpg"))
                Dim rc = New RectangleF(0, 0, img.Width, img.Height)
                g.DrawImage(img, rc, Nothing, ImageAlign.Default)

                g.DrawString(
                    "Watermark",
                    New TextFormat() With
                    {
                        .Font = Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
                        .FontSize = Inch,
                        .ForeColor = Color.FromArgb(128, Color.Yellow)
                    },
                    rc, TextAlignment.Center, ParagraphAlignment.Center, False)

                Util.AddNote(
                    "The image above has a watermark added to it using text drawn with a semitransparent color.",
                    g, New RectangleF(Inch / 2, img.Height + Inch / 4, pixelWidth - Inch, pixelHeight - img.Height - Inch / 4))

                '' Draw border around the whole image
                g.DrawRectangle(New RectangleF(0, 0, bmp.Width, bmp.Height), Color.DarkSlateBlue, 4)
            End Using

        End Using
        '' Done
        Return bmp
    End Function
End Class