PushClip.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 use GcGraphics.PushClip/PopClip
'' methods to specify clipping.
Public Class PushClip
    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 backColor = Color.FromArgb(&HFF0066CC)
        Dim foreColor = Color.FromArgb(&HFFFFCC00)
        Dim bmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Dim cw = 400, ch = 300, pad = 10, bord = 4
        Dim clipRc = New RectangleF(pixelWidth - cw - pad, pad, cw, ch)
        Using g = bmp.CreateGraphics(backColor)
            '' We create a path consisting of two nested rectangles,
            '' outer for the whole bitmap, inner for a text box.
            '' We then use that path to create a clip region And
            '' draw an image covering the whole bitmap but excluding
            '' the text box
            Using gpath = g.CreatePath()
                gpath.BeginFigure(PointF.Empty)
                gpath.AddLine(New PointF(pixelWidth, 0))
                gpath.AddLine(New PointF(pixelWidth, pixelHeight))
                gpath.AddLine(New PointF(0, pixelHeight))
                gpath.EndFigure(FigureEnd.Closed)
                gpath.BeginFigure(New PointF(clipRc.Left, clipRc.Top))
                gpath.AddLine(New PointF(clipRc.Right, clipRc.Top))
                gpath.AddLine(New PointF(clipRc.Right, clipRc.Bottom))
                gpath.AddLine(New PointF(clipRc.Left, clipRc.Bottom))
                gpath.EndFigure(FigureEnd.Closed)
                Using cliprgn = g.CreateClipRegion(gpath)
                    Using img = Image.FromFile(Path.Combine("Resources", "Images", "tudor.jpg"))
                        g.PushClip(cliprgn)
                        g.DrawImage(
                            img,
                            New RectangleF(0, 0, pixelWidth, pixelHeight),
                            Nothing,
                            ImageAlign.StretchImage)
                        g.PopClip(cliprgn)
                    End Using
                End Using
            End Using
            '' We now draw some tex inside the text box,
            '' clipping to it (this overload of PushClip
            '' returns an IDisposable that removes the clipping
            '' when disposed)
            Using (g.PushClip(clipRc))
                g.DrawString(
                    Util.LoremIpsum(),
                    New TextFormat() With
                    {
                        .Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                        .FontSize = 16,
                        .ForeColor = foreColor
                    },
                    clipRc
                    )
            End Using
            '' Draw a border around the whole image,
            '' demonstrating that the clip has been removed:
            g.DrawRectangle(
                New RectangleF(bord / 2, bord / 2, pixelWidth - bord, pixelHeight - bord),
                New Pen(foreColor, bord))
        End Using
        '' Done
        Return bmp
    End Function
End Class