AllBlendingModes.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 the available blending modes.
'' The sample uses the same approach as the BlendingModes sample,
'' but instead of showing a few of the available modes that produce
'' a nice looking result, demonstrates all of them in smaller scale.
'' The base and the blended images used as the same as in the BlendingModes sample.
Public Class AllBlendingModes
    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)

                spectrumBmp.Opaque = opaque
                origBmp.Opaque = opaque

                '' Resize the original photo so we can place 4 samples of it
                '' on the resulting bitmap:
                Dim w = pixelWidth / 4
                Dim h = pixelHeight / 4
                Using g = bmp.CreateGraphics()
                    '' Prepare for captions:
                    Dim tl = g.CreateTextLayout()
                    tl.DefaultFormat.Font = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"))
                    tl.DefaultFormat.FontSize = 12
                    tl.ParagraphAlignment = ParagraphAlignment.Center
                    tl.TextAlignment = TextAlignment.Center
                    tl.MaxWidth = w
                    tl.MaxHeight = h
                    tl.MarginTop = h - g.MeasureString("QWERTY", tl.DefaultFormat).Height * 1.4F
                    '' Render all 16 blending modes in a 4x4 grid
                    Using sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic),
                            sizedSpectrumBmp = spectrumBmp.Resize(w, h)
                        For row = 0 To 3
                            For col = 0 To 3
                                '' Sample:
                                Dim blendMode = CType(row * 4 + col, BlendMode)
                                Dim x = w * col, y = h * row
                                bmp.BitBlt(sizedBmp, x, y)
                                bmp.CompositeAndBlend(sizedSpectrumBmp, x, y, CompositeMode.SourceOver, blendMode)
                                '' Caption:
                                tl.Clear()
                                tl.Append(blendMode.ToString())
                                tl.PerformLayout(True)
                                Dim rc = tl.ContentRectangle
                                rc.Offset(x, y)
                                rc.Inflate(4, 2)
                                g.FillRectangle(rc, Color.White)
                                g.DrawTextLayout(tl, New PointF(x, y))
                            Next
                        Next
                    End Using
                End Using
            End Using
        End Using
        Return bmp
    End Function
End Class