BlendImages.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 the effects of different blend modes
    // when drawing on a GcGraphics. Note that the current blend mode
    // set on an instance of GcGraphics (including GcBitmapGraphics)
    // affects not only images but all drawing operations.
    // See also BlendText and BlendingModes samples.
    public class BlendImages
    {
        const string c_keyOrchid = "orchid";
        const string c_keySpectrum = "spectrum";

        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var iorchid = Image.FromFile(Path.Combine("Resources", "ImagesBis", "orchid.jpg"));
            var ispectr = Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png"));
            Image ifore, iback;
            if (sampleParams[3] == c_keyOrchid)
            {
                ifore = iorchid;
                iback = ispectr;
            }
            else // c_keyOrchid
            {
                ifore = ispectr;
                iback = iorchid;
            }
            const int margin = 36;
            const int NCOLS = 4;
            var w = (int)((pixelSize.Width - margin * 2) / NCOLS);
            var h = (int)((iorchid.Height * w) / iorchid.Width);
            int row = 0, col = 0;

            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using (var g = bmp.CreateGraphics(Color.White))
            {
                // Text layout 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 blend modes in a grid:
                var modes = Enum.GetValues(typeof(BlendMode));
                foreach (var mode in modes)
                {
                    var blendMode = (BlendMode)mode;
                    if (!g.IsBlendModeSupported(blendMode))
                        continue;

                    int x = margin + w * col;
                    int y = margin + h * row;
                    var r = new RectangleF(x, y, w, h);

                    g.BlendMode = BlendMode.Normal;
                    g.DrawImage(iback, r, null, ImageAlign.StretchImage);
                    g.BlendMode = blendMode;
                    g.DrawImage(ifore, r, null, ImageAlign.StretchImage);
                    g.BlendMode = BlendMode.Normal;

                    // 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));
                    if (++col == NCOLS)
                    {
                        col = 0;
                        ++row;
                    }
                }
            }
            return bmp;
        }

        public static List<string[]> GetSampleParamsList()
        {
            return new List<string[]>()
            {
                // Strings are name, description, info. Rest are arbitrary strings:
                new string[] { "Blend Orchid", "Draw the orchid image over the spectrum image using all supported blend modes",
                    "We draw the tiles in this image by composing two images (an orchid and a spectrum) using the " +
                    "different blend modes supported by GcBitmapGraphics. " +
                    "The spectrum image is drawn using BlendMode.Normal, then the current blend mode on GcBitmapGraphics " +
                    "is set to one of the supported blend modes, and the orchid image is drawn using it. " +
                    "The name of the used blend mode is shown below each tile." +
                    "Note that the blend mode set on the graphics affects not only images but any drawing on this graphics. ",
                    c_keyOrchid },
                new string[] { "Blend Spectrum", "Draw the spectrum image over the orchid image using all supported blend modes",
                    "We draw the tiles in this image by composing two images (a spectrum and an orchid) using the " +
                    "different blend modes supported by GcBitmapGraphics. " +
                    "The orchid image is drawn using BlendMode.Normal, then the current blend mode on GcBitmapGraphics " +
                    "is set to one of the supported blend modes, and the spectrum image is drawn using it. " +
                    "The name of the used blend mode is shown below each tile." +
                    "Note that the blend mode set on the graphics affects not only images but any drawing on this graphics. ",
                    c_keySpectrum },
            };
        }
    }
}