BlendingModes.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 a few
    // of the many available blending modes.
    // The base image is a photo of a white orchid on a dark background.
    // The blended image is a simple spectrum (thumbnail shown in the bottom right corner).
    // See the AllBlendingModes sample for a demo of all available blending modes.
    public class BlendingModes
    {
        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 / 2;
                int h = pixelHeight / 2;
                using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
                using (var sizedSpectrumBmp = spectrumBmp.Resize(w, h))
                {
                    // Resized original image:
                    bmp.BitBlt(sizedBmp, 0, 0);

                    // Blending mode Color:
                    bmp.BitBlt(sizedBmp, w, 0);
                    bmp.CompositeAndBlend(sizedSpectrumBmp, w, 0, CompositeMode.SourceOver, BlendMode.Color);

                    // Blending mode SoftLight:
                    bmp.BitBlt(sizedBmp, 0, h);
                    bmp.CompositeAndBlend(sizedSpectrumBmp, 0, h, CompositeMode.SourceOver, BlendMode.SoftLight);

                    // Blending mode Hue:
                    bmp.BitBlt(sizedBmp, w, h);
                    bmp.CompositeAndBlend(sizedSpectrumBmp, w, h, CompositeMode.SourceOver, BlendMode.Hue);
                }
                // Show a thumbnail of the blend source in the bottom right corner:
                using (var spectrumThumbnail = spectrumBmp.Resize(w / 4, h / 4))
                    bmp.BitBlt(spectrumThumbnail, pixelWidth - spectrumThumbnail.PixelWidth, pixelHeight - spectrumThumbnail.PixelHeight);
                // Add borders between the quadrants, and captions for each:
                var lineh = 2;
                using (var g = bmp.CreateGraphics(null))
                {
                    var foreColor = Color.Yellow;
                    var backColor = Color.Blue;
                    var font = Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
                    g.DrawLine(w, 0, w, h * 2, new Pen(Color.Gray, lineh * 2));
                    g.DrawLine(0, h, w * 2, h, new Pen(Color.Gray, lineh * 2));
                    var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
                    var th = g.MeasureString("QWERTY", tf).Height;
                    g.DrawString(" Original image ", tf, new PointF(0, h - th + lineh));
                    g.DrawString(" BlendMode.Color ", tf, new PointF(w + lineh, h - th + lineh));
                    g.DrawString(" BlendMode.SoftLight ", tf, new PointF(0, h * 2 + lineh - th + lineh));
                    g.DrawString(" BlendMode.Hue ", tf, new PointF(w + lineh, h * 2 + lineh - th + lineh));
                }
            }
            return bmp;
        }
    }
}