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

'' 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(
        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)
        Const bottom = 144, pad = 36
        Dim minSize = Math.Min(pixelSize.Width, pixelSize.Height) / 6, maxSize = (Math.Min(pixelSize.Width, pixelSize.Height) / 1.5)

        '' Randomize some parameters of the sample:
        Dim rnd = Util.NewRandom()

        '' 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 GrapeCity.Documents.Imaging.Region(New RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight))
            Dim ellipse = New EllipticFigure(x, y, w, h)
            rgn.CombineWithRegion(New GrapeCity.Documents.Imaging.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(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
                    bmp.Clear(Color.Transparent)
                    bmp.BitBlt(bmpSmall,
                        rnd.Next(pad, pixelSize.Width - pad - bmpSmall.PixelWidth),
                        rnd.Next(pad, pixelSize.Height - pad - bottom - bmpSmall.PixelHeight))
                    Return bmp
                End Using
            End Using
        End Using
    End Function
End Class