TransparencyMask.cs
//
// This code is part of Document Solutions for Imaging demos.
// Copyright (c) MESCIUS inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;
using GCTEXT = GrapeCity.Documents.Text;
using GCDRAW = GrapeCity.Documents.Drawing;

namespace DsImagingWeb.Demos
{
    // 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 GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var h2 = pixelSize.Width / 2;

            // Prepare a linear gradient transparency mask,
            // from 0 (transparent) to 255 (opaque):
            using var mask = new GcBitmap(h2, h2, true);
            using var gmask = mask.CreateGraphics();
            var grad = new LinearGradientBrush(Color.Black, Color.White);
            gmask.FillRectangle(new RectangleF(0, 0, mask.Width, mask.Height), grad);
            // Convert to GrayscaleBitmap to be used as Renderer.TransparencyMaskBitmap:
            using var gsb = mask.ToGrayscaleBitmap();

            // Create a bitmap to draw on, fill it with yellow background:
            using var bmp1 = new GcBitmap(h2, h2, true);
            using var 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:
            var d = h2 / 25f;
            var rc = new RectangleF(0, 0, h2 * 0.7f, h2 * 0.7f);

            var 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 = null;
            FillCircles(g, rc, h2, d);
            bmp.BitBlt(bmp1, h2, 0);

            // Add some explanatory texts below the images:
            var gg = bmp.CreateGraphics();
            rc = new RectangleF(0, h2 + d, h2 - d * 2, h2 - d * 2);
            var tf = new TextFormat()
            {
                Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.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;
        }

        void FillCircles(GcBitmapGraphics g, RectangleF rc, float h2, float d)
        {
            var 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);
        }
    }
}