'' 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
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing

'' 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(
        ByVal pixelSize As Size,
        ByVal dpi As Single,
        ByVal opaque As Boolean,
        Optional ByVal sampleParams As String() = Nothing) As GcBitmap

        Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
        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)
            End Using

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

                spectrumBmp.Opaque = opaque
                origBmp.Opaque = opaque

                '' Resize the original photo so we can place 4 samples of it
                '' on the resulting bitmap:
                Dim w = pixelSize.Width / 4
                Dim h = pixelSize.Height / 4
                Using g = bmp.CreateGraphics()
                    '' Prepare for captions:
                    Dim tl = g.CreateTextLayout()
                    tl.DefaultFormat.Font = GCTEXT.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:
                                Dim rc = tl.ContentRectangle
                                rc.Offset(x, y)
                                rc.Inflate(4, 2)
                                g.FillRectangle(rc, Color.White)
                                g.DrawTextLayout(tl, New PointF(x, y))
                    End Using
                End Using
            End Using
        End Using
        Return bmp
    End Function
End Class