AutoLevels.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
{
    // このサンプルでは、GcBitmap.AutoLevel() を使用して
    // 画像の出力レベルを自動調整する方法を示します。
    public class AutoLevels
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            opaque = true;
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using (var origBmp = new GcBitmap())
            {
                // サンプル画像を読み込みます。
                var imagePath = Path.Combine("Resources", "ImagesBis", "red-yellow-wall.jpg");
                using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    origBmp.Load(stm);

                // 2つのバージョンを並べるため、元の画像をリサイズします。
                int w = pixelSize.Width;
                int h = pixelSize.Height / 2;
                using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
                {
                    // リサイズした元の画像を上半分にコピーします。
                    bmp.BitBlt(sizedBmp, 0, 0);
                    // レベルを自動調整し、結果を下半分にコピーします。
                    sizedBmp.AutoLevel();
                    bmp.BitBlt(sizedBmp, 0, h);
                }

                // キャプション(元の画像と調整後の画像)を追加します。
                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(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(" Auto levels applied ", tf, new PointF(0, h * 2 + lineh - th + lineh));
                }
            }
            return bmp;
        }
    }
}