BlendImages.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
{
// GcGraphics に描画する際の様々なブレンドモードの効果を示します。
// GcGraphics インスタンス(GcBitmapGraphics を含む)に設定されている現在のブレンドモードは、
// 画像だけでなくすべての描画操作に影響することに注意してください。
// BlendText および BlendingModes のサンプルも参照してください。
public class BlendImages
{
const string c_keyOrchid = "orchid";
const string c_keySpectrum = "spectrum";
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
var iorchid = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "orchid.jpg"));
var ispectr = GCDRAW.Image.FromFile(Path.Combine("Resources", "ImagesBis", "spectrum-pastel-500x500.png"));
GCDRAW.Image ifore, iback;
if (sampleParams[3] == c_keyOrchid)
{
ifore = iorchid;
iback = ispectr;
}
else // c_keyOrchid
{
ifore = ispectr;
iback = iorchid;
}
const int margin = 36;
const int NCOLS = 4;
var w = (int)((pixelSize.Width - margin * 2) / NCOLS);
var h = (int)((iorchid.Height * w) / iorchid.Width);
int row = 0, col = 0;
var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
using (var g = bmp.CreateGraphics(Color.White))
{
// 説明文(キャプション)用のテキストレイアウトを設定します。
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;
// すべてのブレンドモードをグリッド状に描画します。
var modes = Enum.GetValues(typeof(BlendMode));
foreach (var mode in modes)
{
var blendMode = (BlendMode)mode;
if (!g.IsBlendModeSupported(blendMode))
continue;
int x = margin + w * col;
int y = margin + h * row;
var r = new RectangleF(x, y, w, h);
g.BlendMode = BlendMode.Normal;
g.DrawImage(iback, r, null, ImageAlign.StretchImage);
g.BlendMode = blendMode;
g.DrawImage(ifore, r, null, ImageAlign.StretchImage);
g.BlendMode = BlendMode.Normal;
// 説明文
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));
if (++col == NCOLS)
{
col = 0;
++row;
}
}
}
return bmp;
}
public static List<string[]> GetSampleParamsList()
{
return new List<string[]>()
{
// 文字列は名前、説明、情報です。残りは任意の文字列です。
new string[] { "Blend Orchid", "Draw the orchid image over the spectrum image using all supported blend modes",
"We draw the tiles in this image by composing two images (an orchid and a spectrum) using the " +
"different blend modes supported by GcBitmapGraphics. " +
"The spectrum image is drawn using BlendMode.Normal, then the current blend mode on GcBitmapGraphics " +
"is set to one of the supported blend modes, and the orchid image is drawn using it. " +
"The name of the used blend mode is shown below each tile." +
"Note that the blend mode set on the graphics affects not only images but any drawing on this graphics. ",
c_keyOrchid },
new string[] { "Blend Spectrum", "Draw the spectrum image over the orchid image using all supported blend modes",
"We draw the tiles in this image by composing two images (a spectrum and an orchid) using the " +
"different blend modes supported by GcBitmapGraphics. " +
"The orchid image is drawn using BlendMode.Normal, then the current blend mode on GcBitmapGraphics " +
"is set to one of the supported blend modes, and the spectrum image is drawn using it. " +
"The name of the used blend mode is shown below each tile." +
"Note that the blend mode set on the graphics affects not only images but any drawing on this graphics. ",
c_keySpectrum },
};
}
}
}