ThresholdingEffects.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
{
// このサンプルでは、さまざまなグレースケール効果の使用方法を示します。
// MatrixEffects1 および MatrixEffects2 のサンプルも参照ください。
public class ThresholdingEffects
{
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
opaque = false;
var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
using (var origBmp = new GcBitmap())
{
// サンプル画像を読み込みます。
var imagePath = Path.Combine("Resources", "Stock", "bw-hiking.jpg");
using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
origBmp.Load(stm);
origBmp.SetAlphaTo255();
origBmp.Opaque = false;
// 生成されるビットマップ上に4つのサンプルを配置できるように、
// 元の画像をリサイズします。
int w = pixelSize.Width / 2;
int h = pixelSize.Height / 2;
using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
{
// リサイズした元の画像を、結果となるビットマップの4つの領域にコピーします。
bmp.BitBlt(sizedBmp, 0, 0);
bmp.BitBlt(sizedBmp, w, 0);
bmp.BitBlt(sizedBmp, 0, h);
bmp.BitBlt(sizedBmp, w, h);
}
var lineh = 2;
// 左上の領域のピクセルはそのまま保持し、残りの3つの領域にエフェクトを適用します。
// (これらのエフェクトはグレースケール画像に対してのみ意味を持つため、説明文を追加する前に実行します)。
bmp.ApplyEffect(OtsuThresholdingEffect.Get(), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
bmp.ApplyEffect(BradleyThresholdingEffect.Get(), new Rectangle(0, h + lineh, w - lineh, h - lineh));
bmp.ApplyEffect(BradleyThresholdingEffect.Get(16, 6), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));
// 各領域の間に境界線を引き、それぞれに説明文を追加します。
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(foreColor, lineh * 2));
g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(foreColor, lineh * 2));
var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
g.DrawString(" Original image ", tf, new PointF(0, 0));
g.DrawString(" OtsuThresholdingEffect ", tf, new PointF(w + lineh, 0));
g.DrawString(" BradleyThresholdingEffect ", tf, new PointF(0, h + lineh));
g.DrawString(" BradleyThresholdingEffect(16,6) ", tf, new PointF(w + lineh, h + lineh));
}
}
// 画像ファイルを保存します。
return bmp;
}
}
}