BlendingModes.cs
// 
// このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
// © 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
{
    // このサンプルでは、利用可能な多くのブレンドモードの中からいくつかを使用して、
    // 2つの画像を合成する方法を実演します。
    // ベースとなる画像は、暗い背景に白い蘭が写った写真です。
    // 合成される画像はシンプルなスペクトル画像です(右下隅にサムネイルが表示されています)。
    // 利用可能なすべてのブレンドモードのデモについては、AllBlendingModes を参照してください。
    public class BlendingModes
    {
        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())
            {
                // サンプル写真を読み込みます。
                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);

                // ブレンドに使用するソースを読み込みます。
                var spectrumPath = Path.Combine("Resources", "ImagesBis", "spectrum-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;

                // 生成されるビットマップ上に 4 つのサンプルを配置できるよう、
                // 元の写真をリサイズします。
                int w = pixelSize.Width / 2;
                int h = pixelSize.Height / 2;
                using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
                using (var sizedSpectrumBmp = spectrumBmp.Resize(w, h))
                {
                    // リサイズした元の画像を表示します。
                    bmp.BitBlt(sizedBmp, 0, 0);

                    // ブレンドモード「Color」を適用します。
                    bmp.BitBlt(sizedBmp, w, 0);
                    bmp.CompositeAndBlend(sizedSpectrumBmp, w, 0, CompositeMode.SourceOver, BlendMode.Color);

                    // ブレンドモード「SoftLight」を適用します。
                    bmp.BitBlt(sizedBmp, 0, h);
                    bmp.CompositeAndBlend(sizedSpectrumBmp, 0, h, CompositeMode.SourceOver, BlendMode.SoftLight);

                    // ブレンドモード「Hue」を適用します。
                    bmp.BitBlt(sizedBmp, w, h);
                    bmp.CompositeAndBlend(sizedSpectrumBmp, w, h, CompositeMode.SourceOver, BlendMode.Hue);
                }
                // 右下隅に、ブレンドソースのサムネイルを表示します。
                using (var spectrumThumbnail = spectrumBmp.Resize(w / 4, h / 4))
                    bmp.BitBlt(spectrumThumbnail, pixelSize.Width - spectrumThumbnail.PixelWidth, pixelSize.Height - spectrumThumbnail.PixelHeight);

                // 各領域の境界線と、それぞれの説明文を追加します。
                var lineh = 2;
                using (var g = bmp.CreateGraphics(null))
                {
                    var foreColor = Color.Yellow;
                    var backColor = Color.Blue;
                    var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.ttf"));
                    g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(Color.Gray, lineh * 2));
                    g.DrawLine(0, h, w * 2, h, new GCDRAW.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;
        }
    }
}