Watermark2.vb
''
'' このコードは、DioDocs for Imaging のサンプルの一部として提供されています。
'' © MESCIUS inc. All rights reserved.
''
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
'' このサンプルは、ビットマップグラフィックの回転変換を使用して、
'' 画像に透かしを角度を付けてレンダリングする方法を示しています。
Public Class Watermark2
Function GenerateImage(
ByVal pixelSize As Size,
ByVal dpi As Single,
ByVal opaque As Boolean,
Optional ByVal sampleParams As String() = Nothing) As GcBitmap
'' 注意:単色の背景の代わりに Color.Transparent を使用することも可能ですが、
'' 期待通りに動作させるには、出力される画像フォーマットが透明度をサポートしている
'' 必要があります。
Dim backColor = Color.FromArgb(&HFF0066CC)
Dim foreColor = Color.FromArgb(&HFFFFCC00)
Dim angle = -30
Dim rad = (angle * Math.PI) / 180.0F
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi)
Using bmpSrc = New GcBitmap(Path.Combine("Resources", "ImagesBis", "alpamayo-sq.jpg"))
'' BitBltを実行するには、両方の画像の不透明度(Opaque設定)が同じである必要があります。
bmpSrc.Opaque = opaque
'' ソース画像をターゲットのビットマップ上に描画します。
'' (一般的には、最初にソース画像のサイズを変更することが推奨されますが、
'' このケースではソース画像がターゲットと同じサイズであることがわかっているため、
'' そのステップは省略します)。
bmp.BitBlt(bmpSrc, 0, 0)
End Using
Using g = bmp.CreateGraphics()
'' ウォーターマークのテキストを、画像全体にループさせて特定の角度で描画します。
g.Transform = Matrix3x2.CreateRotation((angle * Math.PI) / 180.0F, New Vector2(pixelSize.Width / 2, pixelSize.Height / 2))
Dim tf = New TextFormat() With
{
.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeSansBold.ttf")),
.FontSize = 14,
.ForeColor = Color.FromArgb(64, Color.White)
}
Dim tl = g.CreateTextLayout()
tl.Append("Copyright (c) MESCIUS", tf)
tl.PerformLayout(True)
Dim dx = tl.ContentWidth * 3
Dim dy = tl.ContentHeight * 5
Dim n = 0
Dim offX = -(Math.Cos(rad) * pixelSize.Height / 2)
Dim offY = (Math.Sin(rad) * pixelSize.Width / 2)
For y = offY To pixelSize.Height - offY Step dy
For x = offX + dx / 2 * (n Mod 2) To pixelSize.Width - offX Step dx
g.DrawTextLayout(tl, New PointF(x, y))
Next
n += 1
Next
End Using
Return bmp
End Function
End Class