AllBlendingModes.cs
//
// This code is part of GrapeCity Documents for Imaging samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;

namespace GcImagingWeb.Samples
{
    // 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
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var bmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            using (var origBmp = new GcBitmap())
            using (var spectrumBmp = new GcBitmap())
            {
                // Load a sample photo:
                var imagePath = Path.Combine("Resources", "ImagesBis", "orchid.jpg");
                using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    origBmp.Load(stm);

                // Blending source:
                var spectrumPath = Path.Combine("Resources", "ImagesBis", "spectrum.png");
                using (var stm = new FileStream(spectrumPath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    spectrumBmp.Load(stm);

                origBmp.Opaque = spectrumBmp.Opaque = opaque;

                // Resize the original photo so we can place 4 samples of it
                // on the resulting bitmap:
                int w = pixelWidth / 4;
                int h = pixelHeight / 4;
                using (var g = bmp.CreateGraphics())
                {
                    // Prepare for captions:
                    var 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 (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
                    using (var sizedSpectrumBmp = spectrumBmp.Resize(w, h))
                    {
                        for (int row = 0; row < 4; ++row)
                            for (int col = 0; col < 4; ++col)
                            {
                                // Sample:
                                var blendMode = (BlendMode)(row * 4 + col);
                                int 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);
                                var rc = tl.ContentRectangle;
                                rc.Offset(x, y);
                                rc.Inflate(4, 2);
                                g.FillRectangle(rc, Color.White);
                                g.DrawTextLayout(tl, new PointF(x, y));
                            }
                    }
                }
            }
            return bmp;
        }
    }
}