RoundCLip.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 GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // このサンプルは、画像の一部を円形にクリッピングし、
    // GcBitmap に描画する方法を示します。
    public class RoundCLip
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            // 注意:単色の背景の代わりに Color.Transparent を使用することもできますが、
            // これが期待通りに動作するためには、結果の画像フォーマットが
            // 透明度をサポートしている必要があります。
            var backColor = Color.FromArgb(unchecked((int)0xff0066cc));
            var foreColor = Color.FromArgb(unchecked((int)0xffffcc00));
            const int bottom = 144, pad = 36;
            int minSize = Math.Min(pixelSize.Width, pixelSize.Height) / 6, maxSize = (int)(Math.Min(pixelSize.Width, pixelSize.Height) / 1.5);

            // サンプルのいくつかのパラメータをランダム化します。
            var rnd = Common.Util.NewRandom();

            // 不要になったビットマップは破棄(Dispose)するのが良いため、
            // 返却されるもの以外のすべてのビットマップに 'using' を使用しています。
            using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "Stock", "woman-brick-wall.jpg")))
            {
                // 元の画像とターゲットの不透明度が一致することを確認します。
                bmpSrc.Opaque = opaque;

                // 元の画像におけるクリッピングの座標とサイズです。
                const int x = 143, y = 0, w = 655, h = 655;

                // 指定された円の外側をすべて除外するクリッピング領域を作成します。
                var rgn = new GrapeCity.Documents.Imaging.Region(new RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight));
                var ellipse = new EllipticFigure(x, y, w, h);
                rgn.CombineWithRegion(new GrapeCity.Documents.Imaging.Region(ellipse), RegionCombineMode.Exclude, false);

                // リージョンを使用してクリップするには、BitmapRenderer を使用する必要があります。
                // 注意 (v2sp2の新機能):レンダラーはデフォルトでは作成されないため、
                // 使用する前に EnsureRendererCreated() を呼び出す必要があります。
                bmpSrc.EnsureRendererCreated();
                var renderer = bmpSrc.Renderer;
                renderer.ClipRegion = rgn;
                renderer.Clear(Color.Transparent);
                var size = rnd.Next(minSize, maxSize);
                using (var bmpRound = bmpSrc.Clip(new Rectangle(x, y, w, h)))
                using (var bmpSmall = bmpRound.Resize(size, size))
                {
                    var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
                    bmp.Clear(Color.Transparent);
                    bmp.BitBlt(bmpSmall,
                        rnd.Next(pad, pixelSize.Width - pad - bmpSmall.PixelWidth),
                        rnd.Next(pad, pixelSize.Height - pad - bottom - bmpSmall.PixelHeight));
                    return bmp;
                }
            }
        }
    }
}