BlendingModes.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 composing two images using a few
'' of the many available blending modes.
'' The base image is a photo of a white orchid on a dark background.
'' The blended image is a simple spectrum (thumbnail shown in the bottom right corner).
'' See the AllBlendingModes sample for a demo of all available blending modes.
Public Class BlendingModes
    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 bmp = New GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY)
        Using origBmp = New GcBitmap(), spectrumBmp = New GcBitmap()
            '' Load a sample photo:
            Dim imagePath = Path.Combine("Resources", "ImagesBis", "orchid.jpg")
            Using stm = New FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                origBmp.Load(stm)
            End Using

            '' Blending source:
            Dim spectrumPath = Path.Combine("Resources", "ImagesBis", "spectrum.png")
            Using stm = New FileStream(spectrumPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess)
                spectrumBmp.Load(stm)
            End Using

            origBmp.Opaque = opaque
            spectrumBmp.Opaque = opaque

            '' Resize the original photo so we can place 4 samples of it
            '' on the resulting bitmap:
            Dim w = pixelWidth / 2
            Dim h = pixelHeight / 2
            Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic), sizedSpectrumBmp = spectrumBmp.Resize(w, h)
                '' Resized original image:
                bmp.BitBlt(sizedBmp, 0, 0)

                '' Blending mode Color
                bmp.BitBlt(sizedBmp, w, 0)
                bmp.CompositeAndBlend(sizedSpectrumBmp, w, 0, CompositeMode.SourceOver, BlendMode.Color)

                '' Blending mode SoftLight
                bmp.BitBlt(sizedBmp, 0, h)
                bmp.CompositeAndBlend(sizedSpectrumBmp, 0, h, CompositeMode.SourceOver, BlendMode.SoftLight)

                '' Blending mode Hue
                bmp.BitBlt(sizedBmp, w, h)
                bmp.CompositeAndBlend(sizedSpectrumBmp, w, h, CompositeMode.SourceOver, BlendMode.Hue)
            End Using
            '' Show a thumbnail of the blend source in the bottom right corner
            Using spectrumThumbnail = spectrumBmp.Resize(w / 4, h / 4)
                bmp.BitBlt(spectrumThumbnail, pixelWidth - spectrumThumbnail.PixelWidth, pixelHeight - spectrumThumbnail.PixelHeight)
            End Using
            '' Add borders between the quadrants, And captions for each:
            Dim lineh = 2
            Using g = bmp.CreateGraphics(Nothing)
                Dim foreColor = Color.Yellow
                Dim backColor = Color.Blue
                Dim fnt = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
                g.DrawLine(w, 0, w, h * 2, New Pen(Color.Gray, lineh * 2))
                g.DrawLine(0, h, w * 2, h, New Pen(Color.Gray, lineh * 2))
                Dim tf = New TextFormat() With {.Font = fnt, .FontSize = 18, .ForeColor = foreColor, .BackColor = backColor, .FontBold = True}
                Dim th = g.MeasureString("QWERTY", tf).Height
                g.DrawString(" Original image ", tf, New PointF(0, h - th + lineh))
                g.DrawString(" BlendMode.Color ", tf, New PointF(w + lineh, h - th + lineh))
                g.DrawString(" BlendMode.SoftLight ", tf, New PointF(0, h * 2 + lineh - th + lineh))
                g.DrawString(" BlendMode.Hue ", tf, New PointF(w + lineh, h * 2 + lineh - th + lineh))
            End Using
        End Using
        Return bmp
    End Function
End Class