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

'' This sample demonstrates the use of GcBitmap.GetContentRect() method
'' that allows you to trim single colored margins off an image.
Public Class GetContentRect
    Function GenerateImage(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) 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(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
        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