TransparencyMask.vb
C# VB
'''' This code is part of Document Solutions for Imaging demos.'' Copyright (c) MESCIUS inc. All rights reserved.''Imports System.IOImports System.DrawingImports GrapeCity.Documents.DrawingImports GrapeCity.Documents.TextImports GrapeCity.Documents.ImagingImports GCTEXT = GrapeCity.Documents.Text
'' This sample demonstrates the use of TransparencyMaskBitmap'' when drawing on a bitmap. The transparency mask used is'' created on the fly by filling an intermediary bitmap'' with a linear gradient brush from 0 (black, transparent)'' to 255 (white, opaque).Public Class TransparencyMask Public Function GenerateImage( ByVal pixelSize As Size, ByVal dpi As Single, ByVal opaque As Boolean, Optional ByVal sampleParams As String() = Nothing) As GcBitmap
Dim h2 As Integer = pixelSize.Width \ 2
'' Prepare a linear gradient transparency mask, '' from 0 (transparent) to 255 (opaque): Using mask As New GcBitmap(h2, h2, True) Using gmask = mask.CreateGraphics() Dim grad = New LinearGradientBrush(Color.Black, Color.White) gmask.FillRectangle(New RectangleF(0, 0, mask.Width, mask.Height), grad) End Using '' Convert to GrayscaleBitmap to be used as Renderer.TransparencyMaskBitmap: Using gsb = mask.ToGrayscaleBitmap()
'' Create a bitmap to draw on, fill it with yellow background: Using bmp1 As New GcBitmap(h2, h2, True) Using g = bmp1.CreateGraphics(Color.Yellow)
'' Apply the transparency mask: g.Renderer.TransparencyMaskBitmap = gsb
'' Fill 3 circles, note how the fill gradually changes '' from transparent to opaque (left to right) along with the gradient: Dim d As Single = CSng(h2) / 25.0! Dim rc As New RectangleF(0, 0, h2 * 0.7F, h2 * 0.7F)
Dim bmp = New GcBitmap(pixelSize.Width, pixelSize.Height, True, dpi, dpi) bmp.Clear(Color.White) FillCircles(g, rc, h2, d) bmp.BitBlt(bmp1, 0, 0)
g.Renderer.TransparencyMaskBitmap = Nothing FillCircles(g, rc, h2, d) bmp.BitBlt(bmp1, h2, 0)
'' Add some explanatory texts below the images: Dim gg = bmp.CreateGraphics() rc = New RectangleF(0, h2 + d, h2 - d * 2, h2 - d * 2) Dim tf = New TextFormat() With { .Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "FreeMono.ttf")), .FontSize = 16 } gg.DrawString( "The circles in the image above were rendered with TransparencyMaskBitmap " & "on the target graphics set to a linear gradient from transparent to opaque.", tf, rc, TextAlignment.Center, ParagraphAlignment.Near)
rc.Offset(h2, 0) gg.DrawString( "The circles in the image above were rendered with no TransparencyMaskBitmap " & "on the target graphics.", tf, rc, TextAlignment.Center, ParagraphAlignment.Near)
Return bmp End Using End Using End Using End Using End Function
Private Sub FillCircles(ByVal g As GcBitmapGraphics, ByVal rc As RectangleF, ByVal h2 As Single, ByVal d As Single) Dim r = rc r.Offset((h2 - rc.Width) / 2, 0) r.Inflate(-d, -d) g.FillEllipse(r, Color.Red)
r = rc r.Offset(h2 - rc.Width, h2 - rc.Height) r.Inflate(-d, -d) g.FillEllipse(r, Color.Green)
r = rc r.Offset(0, h2 - rc.Height) r.Inflate(-d, -d) g.FillEllipse(r, Color.Blue) End SubEnd Class