HatchStyles.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
{
// このサンプルでは、さまざまなハッチスタイルを紹介します。
// これらのスタイルの多くは、MS Excelのパターンスタイルを実装しています。
public class HatchStyles
{
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
// ハッチスタイルの列挙値を取得します。
var hatchStyles = Enum.GetValues(typeof(HatchStyle));
int COLS = 4;
var w = pixelSize.Width / COLS;
var h = pixelSize.Height / ((hatchStyles.Length + COLS -1) / COLS);
var tf = new TextFormat
{
Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf")),
FontSize = 12,
};
var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
using (var g = bmp.CreateGraphics(Color.White))
{
int row = 0, col = 0;
foreach (HatchStyle hs in hatchStyles)
{
// ハッチングで矩形を塗りつぶします。
var rc = new RectangleF(col * w, row * h, w, h);
var b = new HatchBrush(hs)
{
ForeColor = Color.Black,
BackColor = Color.White
};
g.FillRectangle(rc, b);
// キャプション(スタイル名)を描画します。
var cap = hs.ToString();
var scap = g.MeasureString(cap, tf);
var rcap = new RectangleF(
rc.X + (rc.Width - scap.Width) / 2,
rc.Y + (rc.Height - scap.Height) / 2,
scap.Width, scap.Height);
// テキストの背景を白で塗りつぶして読みやすくします。
rcap.Inflate(3, 2);
g.FillRectangle(rcap, Color.White);
g.DrawString(cap, tf, rcap, TextAlignment.Center, ParagraphAlignment.Center);
// 次の描画位置へ移動します。
if (++col >= COLS)
{
col = 0;
++row;
}
}
}
return bmp;
}
}
}