SoftEdges.vb
''
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
''
Imports System
Imports System.IO
Imports System.Drawing
Imports System.Collections.Generic
Imports System.Linq
Imports System.Numerics
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' このサンプルでは、DsImaging を使用してソフトエッジ効果を作成する方法を示します。
'' ソフトエッジ効果は、画像内のすべての非透明領域を指定した量だけ縮小した後、
'' 境界を滑らかにするためにガウスぼかしを適用します。
'' これは、例えば MS Word に搭載されている効果の一つです。
''
'' DsImaging でこの効果を実現するために、
'' ソフトエッジを適用したいカラー画像から作成した透明度マスクに対して
'' GrayscaleBitmap.ApplyGlow() メソッドを使用し、
'' 本コードで示しているいくつかの簡単な手順を組み合わせています。
''
'' 同じ GrayscaleBitmap.ApplyGlow() メソッドは、
'' Glow および GlowAlt の各例で示されているように、
'' グロー効果を実現するためにも使用されています。
Public Class SoftEdges
Private _font As GCTEXT.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSans.ttf"))
Public Function GenerateImage(
ByVal pixelSize As Size,
ByVal dpi As Single,
ByVal opaque As Boolean,
Optional ByVal sampleParams As String() = Nothing) As GcBitmap
'' 透明なビットマップ上にテキストとロゴを描画します。
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, False)
bmp.Clear(Color.Transparent)
drawTextAndLogo(bmp, pixelSize, dpi)
'' イメージを透明度マスクに変換します。
Using gs = bmp.ToGrayscaleBitmap(ColorChannel.Alpha)
'' ソフトエッジ(縁ぼかし)効果を適用します。グロー(光彩)の場合は拡張半径が正の値になりますが、
'' ソフトエッジの場合は負の値にする必要があります。ここでは-6に設定しています(9はぼかしの半径です)。
gs.ApplyGlow(-6, 9)
'' 生成されたGrayscaleBitmapを透明度マスクとして使用し、テキストとロゴを再度描画します。
bmp.Clear(Color.Gray)
bmp.EnsureRendererCreated().TransparencyMaskBitmap = gs
'' 最後に、準備した背景の上にテキストとロゴをもう一度描画します。
drawTextAndLogo(bmp, pixelSize, dpi)
'' 画像ファイルを保存します。
Return bmp
End Using
End Function
'' テキストとロゴを描画するためのヘルパーです。
Sub drawTextAndLogo(ByRef bmp As GcBitmap, pixelSize As Size, dpi As Single)
DrawText(bmp.EnsureRendererCreated(), pixelSize, dpi)
DrawLogo(bmp.EnsureRendererCreated(), New PointF(pixelSize.Width / 2, CInt(pixelSize.Height * 0.8)), pixelSize.Width / 2)
End Sub
Private Sub DrawText(ByVal renderer As BitmapRenderer, ByVal pixelSize As Size, ByVal dpi As Single)
Dim f1 = New TextFormat With {
.Font = _font,
.FontSize = pixelSize.Height / 7,
.ForeColor = Color.DarkOrchid,
.FontBold = True
}
Dim f2 = New TextFormat(f1) With {
.ForeColor = Color.White,
.StrokePen = New GCDRAW.Pen(Color.DarkOrchid, 3)
}
Dim tl = New TextLayout(dpi) With {
.MaxWidth = pixelSize.Width,
.MaxHeight = pixelSize.Height,
.ParagraphAlignment = ParagraphAlignment.Near,
.TextAlignment = TextAlignment.Center,
.MarginTop = dpi * 2,
.LineSpacingScaleFactor = 0.7F
}
tl.AppendLine("MESCIUS", f1)
tl.AppendLine("DioDocs", f2)
'' テキストを描画します。
renderer.SlowAntialiasing = True
renderer.DrawTextLayout(tl, 0, 0)
End Sub
Private Sub DrawLogo(renderer As BitmapRenderer, center As PointF, width As Single)
Dim pb As New PathBuilder()
Dim refCenterX As Single = 400.0F
Dim refCenterY As Single = 300.0F
Dim outerRadius As Single = 250.0F
Dim innerRadius As Single = 100.0F
Dim points As Integer = 5
For i As Integer = 0 To (points * 2) - 1
Dim angle As Double = i * Math.PI / points - Math.PI / 2
Dim r As Single = If(i Mod 2 = 0, outerRadius, innerRadius)
Dim x As Single = refCenterX + CSng(Math.Cos(angle) * r)
Dim y As Single = refCenterY + CSng(Math.Sin(angle) * r)
If i = 0 Then
pb.BeginFigure(x, y)
Else
pb.AddLine(x, y)
End If
Next
pb.EndFigure(True)
Dim gpFill = pb.ToPath()
Dim gpStroke = gpFill.Widen(New GCDRAW.Pen(Color.Black, 20))
' 基本となるサイズは 800 x 600 ピクセルです。
Dim scale As Single = width / 800.0F
renderer.Transform = Matrix3x2.CreateScale(scale) *
Matrix3x2.CreateTranslation(center.X - 400.0F * scale, center.Y - 300.0F * scale)
renderer.FillPath(gpFill, Color.CornflowerBlue)
renderer.FillPath(gpStroke, Color.DarkOrchid)
renderer.Transform = Matrix3x2.Identity
End Sub
End Class