AllBlendingModes.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つの画像を合成する方法を紹介します。
    // このサンプルは BlendingModes サンプルと同じ手法を使用していますが、
    // 見栄えの良い結果を生むいくつかのモードだけを示すのではなく、
    // 利用可能なすべてのモードを小さなスケールでデモンストレーションします。
    // ベース画像とブレンド画像は BlendingModes サンプルで使用されているものと同じです。
    // BlendImages_0 および BlendText のサンプルも参照してください。
    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())
            {
                // サンプル画像を読み込みます。
                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-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;

                // 生成されるビットマップ上に 4 つのサンプルを配置できるよう、
                // 元の写真をリサイズします。
                int w = pixelSize.Width / 4;
                int h = pixelSize.Height / 4;
                using (var g = bmp.CreateGraphics())
                {
                    // 説明文(キャプション)の準備をします。
                    var tl = g.CreateTextLayout();
                    tl.DefaultFormat.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.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;

                    // 全 16 種類のブレンドモードを 4x4 のグリッド状に描画します。
                    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)
                            {
                                // サンプル
                                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);

                                // 説明文
                                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;
        }
    }
}