''
'' This code is part of Document Solutions for Imaging demos.
'' Copyright (c) MESCIUS inc. All rights reserved.
''
Imports System.IO
Imports System.Drawing
Imports GrapeCity.Documents.Drawing
Imports GrapeCity.Documents.Text
Imports GrapeCity.Documents.Imaging
Imports GCTEXT = GrapeCity.Documents.Text
Imports GCDRAW = GrapeCity.Documents.Drawing
'' This sample shows how a color spectrum with fall offs from fully saturated
'' colors to white and black can be easily created using linear gradient brushes
'' and GcBitmap.Renderer.TransparencyMaskBitmap.
Public Class ColorSpectrum
Public Function GenerateImage(
ByVal pixelSize As Size,
ByVal dpi As Single,
ByVal opaque As Boolean,
Optional ByVal sampleParams As String() = Nothing) As GcBitmap
'' Color spectrum gradient brush:
Dim spec = New LinearGradientBrush(Color.Red, Color.Red)
Dim stepVal As Single = 1.0F / 6.0F
spec.GradientStops = New GradientStop() {
New GradientStop(Color.Yellow, stepVal),
New GradientStop(Color.Green, stepVal * 2),
New GradientStop(Color.Cyan, stepVal * 3),
New GradientStop(Color.Blue, stepVal * 4),
New GradientStop(Color.Magenta, stepVal * 5)
}
'' Create the spectrum in a separate temp bitmap:
Dim spectrumSz = New Size(pixelSize.Width, CInt(pixelSize.Height * 0.75F))
'' Falloff brush and mask:
Using tbmp As New GcBitmap(spectrumSz.Width, spectrumSz.Height, True)
Using tgfx = tbmp.CreateGraphics()
'' Black is opaque, White transparent:
Dim falloff = New LinearGradientBrush(Color.Black, PointF.Empty, Color.Black, New PointF(0, 1))
'' Additional gray stops provide a more gradual and pleasing appearance:
falloff.GradientStops = New GradientStop() {
New GradientStop(Color.LightGray, 0.35F),
New GradientStop(Color.White, 0.5F),
New GradientStop(Color.LightGray, 0.65F)
}
'' Fill the mask with the gradient that will define the transparency:
tgfx.FillRectangle(New RectangleF(0, 0, tbmp.Width, tbmp.Height), falloff)
'' Convert mask to grayscale for use as the transparency mask:
Using mask = tbmp.ToGrayscaleBitmap()
'' Reuse the original mask bitmap;
'' Fill it with a gradient from white at the top to black at the bottom:
Dim grad = New LinearGradientBrush(Color.White, PointF.Empty, Color.Black, New PointF(0, 1))
tgfx.FillRectangle(New RectangleF(0, 0, spectrumSz.Width, spectrumSz.Height), grad)
'' Add transparency mask and draw the gradient:
tbmp.Renderer.TransparencyMaskBitmap = mask
tgfx.FillRectangle(New RectangleF(PointF.Empty, spectrumSz), spec)
'' Copy spectrum to the target bitmap:
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi)
bmp.BitBlt(tbmp, 0, 0)
'' Add some explanatory texts below the spectrum:
Dim g = bmp.CreateGraphics()
Dim rc = New RectangleF(0, spectrumSz.Height, spectrumSz.Width, bmp.PixelHeight - spectrumSz.Height)
Dim tf = New TextFormat() With {
.Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMonoBold.ttf")),
.FontSize = 14
}
g.FillRectangle(rc, Color.LightGray)
g.DrawString(
"The color spectrum is drawn using a horizontal linear gradient brush with 7 stops:" & vbLf &
"Red - Yellow - Green - Cyan - Blue - Magenta - Red." & vbLf &
"The background is filled with a vertical gradient from white to black. " &
"A TransparencyMaskBitmap with a vertical gradient that changes from 0 " &
"(opaque) to 1 (transparent) back to 0 (opaque) provides falloffs " &
"from spectrum colors in the center up to white and down to black backgrounds.",
tf, rc, TextAlignment.Center, ParagraphAlignment.Center)
Return bmp
End Using
End Using
End Using
End Function
End Class