AllBlendingModes.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS 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;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // 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.
    // See also BlendImages_0 and BlendText samples.
    public class AllBlendingModes
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            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-pastel-500x500.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 = pixelSize.Width / 4;
                int h = pixelSize.Height / 4;
                using (var g = bmp.CreateGraphics())
                {
                    // Prepare for captions:
                    var 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 (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;
        }
    }
}