RoundCLip.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 make a round clipping of a part of an image
'' and render it into a GcBitmap.
Public Class RoundCLip
    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)
        Const bottom = 144, pad = 36
        Dim minSize = Math.Min(pixelWidth, pixelHeight) / 6, maxSize = (Math.Min(pixelWidth, pixelHeight) / 1.5)

        '' Randomize some parameters of the sample:
        Dim rnd = New Random()

        '' It is a good idea to dispose bitmaps which are no longer needed,
        '' so we are 'using' all bitmaps except the one returned:
        Using bmpSrc = New GcBitmap(Path.Combine("Resources", "Stock", "woman-brick-wall.jpg"))
            '' Make sure source and target opacity match:
            bmpSrc.Opaque = opaque

            '' Coordinates and size of the clipping in the source image:
            Const x = 143, y = 0, w = 655, h = 655

            '' Create a clipping region excluding all outside of the specified circle:
            Dim rgn = New Region(New RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight))
            Dim ellipse = New EllipticFigure(x, y, w, h)
            rgn.CombineWithRegion(New Region(ellipse), RegionCombineMode.Exclude, False)

            '' To clip using a Region, we need to use the BitmapRenderer:
            '' NOTE (New in v2sp2) the renderer Is Not created by Default,
            '' we must call EnsureRendererCreated() prior to using it:
            bmpSrc.EnsureRendererCreated()
            Dim renderer = bmpSrc.Renderer
            renderer.ClipRegion = rgn
            renderer.Clear(Color.Transparent)
            Dim size = rnd.Next(minSize, maxSize)
            Using bmpRound = bmpSrc.Clip(New Rectangle(x, y, w, h))
                Using bmpSmall = bmpRound.Resize(size, size)
                    Dim bmp = New GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY)
                    bmp.Clear(Color.Transparent)
                    bmp.BitBlt(bmpSmall,
                        rnd.Next(pad, pixelWidth - pad - bmpSmall.PixelWidth),
                        rnd.Next(pad, pixelHeight - pad - bottom - bmpSmall.PixelHeight))
                    Return bmp
                End Using
            End Using
        End Using
    End Function
End Class