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

'' This sample demonstrates the use of GcBitmap.GetContentRect() method
'' that allows to trim single colored margins off an image.
Public Class GetContentRect
    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

        '' The original goldfish image has white background with margins around the actual fish:
        Dim origImagePath = Path.Combine("Resources", "Stock", "goldfish.jpg")

        '' Create the resulting bitmap:
        Dim targetBmp = New GcBitmap(pixelWidth, pixelHeight, True, dpiX, dpiY)
        Using origBmp = New GcBitmap()
            '' Load the goldfish image:
            Using stm = New FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                origBmp.Load(stm)
            End Using
            '' Create graphics on the resulting bitmap, filling it with blue color:
            Using g = targetBmp.CreateGraphics(Color.FromArgb(&HFF004D99))
                '' Render the goldfish on white background:
                targetBmp.BitBlt(origBmp, 0, 0)
                '' The GetContentRect() method returns the area of the image without
                '' margins of the specified color (white in this case):
                Dim rc = origBmp.GetContentRect(Color.White)
                '' Draw a red border on the content rectangle:
                g.DrawRectangle(rc, Color.Red)
            End Using
        End Using
        Return targetBmp
    End Function
End Class