SvgGraphicsRoundRectangle.cs
//
// このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
// © MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Svg;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;
namespace DsImagingWeb.Demos
{
// この例は、GcSvgGraphics 上で描画する方法を示します。
// 専用の DrawRoundRect / FillRoundRect メソッドと
// グラフィックスパスの両方を使用して、角丸四角形を描画します。
// なお、このサンプル内のコードは RoundRectangle サンプルの描画コードと同一ですが、
// ラスター形式の GcBitmap ではなく、ベクター形式の SVG 画像を作成します。
public class SvgGraphicsRoundRectangle
{
public string DefaultMime { get => Common.Util.MimeTypes.SVG; }
public Stream GenerateImageStream(string targetMime, Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
{
if (targetMime != Common.Util.MimeTypes.SVG)
throw new Exception("This sample only supports SVG output format.");
var Inch = dpi;
var ms = new MemoryStream();
using var g = new GcSvgGraphics(pixelSize.Width, pixelSize.Height);
if (opaque)
g.FillRectangle(new RectangleF(0, 0, g.Width, g.Height), Color.White);
var rc = Common.Util.AddNote(
"GcGraphics has dedicated methods to easily draw and fill rectangles with rounded corners. " +
"This sample also shows how the same result may be achieved using graphics paths. " +
"While they are not really needed for drawing round rectangles, graphics paths allow " +
"to draw and fill arbitrary figures with complex geometries.\r\n\r\n" +
"Note that this version of the \"Round Rectangles\" sample draws on a GcSvgGraphics, " +
"and produces a vector SVG image rather than a raster image (JPEG, PNG etc.).",
g);
// 角丸長方形の半径です。
float rx = 36, ry = 24;
// 専用メソッドを使用して角丸長方形を描画および塗りつぶします。
var rEasy = new RectangleF(rc.Left, rc.Bottom + Inch / 2, Inch * 2, Inch);
g.FillRoundRect(rEasy, rx, ry, Color.PaleGreen);
g.DrawRoundRect(rEasy, rx, ry, Color.Blue, 4);
// ラベルを追加します。
var tf = new TextFormat()
{
Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSerif.ttf")),
FontSize = Inch / 6
};
g.DrawString("The easy way.", tf, rEasy, TextAlignment.Center, ParagraphAlignment.Center, false);
// グラフィックスパスを使用して同じ結果を実現します。
var rHard = rEasy;
rHard.Offset(0, rEasy.Height + Inch / 2);
var path = MakeRoundRect(g, rHard, rx, ry);
g.FillPath(path, Color.PaleVioletRed);
g.DrawPath(path, Color.Purple, 4);
// ラベルを追加します。
g.DrawString("The hard way.", tf, rHard, TextAlignment.Center, ParagraphAlignment.Center, false);
// 画像ファイルを保存します。
var svg = g.ToSvgDocument();
svg.Save(ms);
ms.Seek(0, SeekOrigin.Begin);
return ms;
}
// このメソッドは、GcGraphics上で任意の形状を塗りつぶしたり描画したりするために使用できる
// グラフィックスパスの作成方法を示しています。
private IPath MakeRoundRect(GcGraphics g, RectangleF rc, float rx, float ry)
{
var path = g.CreatePath();
var sz = new SizeF(rx, ry);
// 上部の左側の水平部分から開始します。
path.BeginFigure(new PointF(rc.Left + rx, rc.Top));
path.AddLine(new PointF(rc.Right - rx, rc.Top));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Right, rc.Top + ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.AddLine(new PointF(rc.Right, rc.Bottom - ry));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Right - rx, rc.Bottom), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.AddLine(new PointF(rc.Left + rx, rc.Bottom));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Left, rc.Bottom - ry), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.AddLine(new PointF(rc.Left, rc.Top + ry));
path.AddArc(new ArcSegment() { Point = new PointF(rc.Left + rx, rc.Top), SweepDirection = SweepDirection.Clockwise, Size = sz });
path.EndFigure(FigureEnd.Closed);
return path;
}
}
}