CreateThumbnails.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
{
// このサンプルは、異なる補間モードを使用して、
// 既存の画像をリサイズ(縮小)し、サムネイルを作成する方法を示します。
public class DownscaleImage
{
public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
var origImagePath = Path.Combine("Resources", "Images", "minerva.jpg");
// ターゲットビットマップを作成して塗りつぶします。
var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, true, dpi, dpi);
using (var g = targetBmp.CreateGraphics(Color.FromArgb(unchecked((int)0xff004d99))))
{
// 背景色を指定してGraphicsを作成することで、塗りつぶし処理が行われます。
}
const int fontSize = 16, fpad = 4, xpad = 10, ypad = 3;
TextFormat tf = new TextFormat
{
ForeColor = Color.FromArgb(unchecked((int)0xffffcc00)),
Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf")),
FontSize = fontSize,
};
using (var origBmp = new GcBitmap())
{
using (var stm = new FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
origBmp.Load(stm);
// 元の画像を元のサイズでビットブロック転送(コピー)します。
targetBmp.BitBlt(origBmp, 0, 0);
using (var g = targetBmp.CreateGraphics(null))
g.DrawString($"Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, new PointF(fpad, origBmp.Height + fpad));
int dx = (int)origBmp.Width + xpad;
// 様々な補間モードを使用して画像を縮小し、それらを元の画像の右側に描画します。
float f = (origBmp.Height - ypad * 3) / origBmp.Height / 4;
int twidth = (int)Math.Round(origBmp.Width * f);
int theight = (int)Math.Round(origBmp.Height * f);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor))
targetBmp.BitBlt(bmp, dx, 0);
drawCaption("InterpolationMode.NearestNeighbor", dx, 0);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear))
targetBmp.BitBlt(bmp, dx, theight + ypad);
drawCaption("InterpolationMode.Linear", dx, theight + ypad);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic))
targetBmp.BitBlt(bmp, dx, (theight + ypad) * 2);
drawCaption("InterpolationMode.Cubic", dx, (theight + ypad) * 2);
using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale))
targetBmp.BitBlt(bmp, dx, (theight + ypad) * 3);
drawCaption("InterpolationMode.Downscale", dx, (theight + ypad) * 3);
//
void drawCaption(string caption, float x, float y)
{
using (var g = targetBmp.CreateGraphics(null))
g.DrawString(caption, tf, new PointF(x + twidth + fpad, y + fpad));
}
}
return targetBmp;
}
}
}