Gradients.cs
//
// This code is part of GrapeCity Documents for Imaging samples.
// Copyright (c) GrapeCity, Inc. All rights reserved.
//
using System;
using System.IO;
using System.Drawing;
using GrapeCity.Documents.Imaging;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Drawing;

namespace GcImagingWeb.Samples
{
    // Sample shows how to create gradient fills using LinearGradientBrush and RadialGradientBrush.
    public class Gradients
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var bmp = new GcBitmap(pixelWidth, pixelHeight, true, dpiX, dpiY);
            var Inch = dpiX;
            using (var g = bmp.CreateGraphics(Color.RoyalBlue))
            {
                var testRectSize = new SizeF(Inch * 6, Inch);
                var dy = Inch / 6;
                // TextLayout to draw labels:
                var tl = g.CreateTextLayout();
                //qq tl.DefaultFormat.Font = StandardFonts.Times;
                tl.DefaultFormat.FontSize = Inch / 6;
                tl.DefaultFormat.ForeColor = Color.Chartreuse;
                tl.MaxWidth = testRectSize.Width;
                tl.MaxHeight = testRectSize.Height;
                tl.TextAlignment = TextAlignment.Center;
                tl.ParagraphAlignment = ParagraphAlignment.Center;
                // Note 1:
                var rc = Common.Util.AddNote("Linear gradients using LinearGradientBrush:", g, new RectangleF(Inch, Inch / 2, 500, 100));
                // Text insertion point:
                PointF ip = new PointF(rc.Left, rc.Bottom + dy);
                // Local action to draw a gradient-filled rectangle:
                Action<Brush, string> drawSwatch = (b_, txt_) =>
                {
                    var rect = new RectangleF(ip, testRectSize);
                    // Fill the rectangle with a gradient brush:
                    g.FillRectangle(rect, b_);
                    // Draw a border, text info etc:
                    g.DrawRectangle(rect, Color.Magenta);
                    tl.Clear();
                    tl.Append(txt_);
                    tl.MaxHeight = testRectSize.Height;
                    tl.MaxWidth = testRectSize.Width;
                    tl.PerformLayout(true);
                    g.DrawTextLayout(tl, ip);
                    ip.Y += rect.Height + dy;
                };
                // LinearGradientBrush:
                // Horizontal gradient:
                LinearGradientBrush linearGradBrush = new LinearGradientBrush(Color.Red, Color.Blue);
                drawSwatch(linearGradBrush, $"Linear gradient\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
                // Vertical gradient:
                linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Green, new PointF(0, 1));
                drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
                // Diagonal gradient (increase swatch height to better show diagonal):
                testRectSize.Height *= 2;
                linearGradBrush = new LinearGradientBrush(Color.Red, new PointF(0, 0), Color.Teal, new PointF(1, 1));
                drawSwatch(linearGradBrush, $"Linear gradient\r\nfrom {linearGradBrush.StartPoint} to {linearGradBrush.EndPoint}");
                // RadialGradientBrush
                rc = Common.Util.AddNote("Radial gradients using RadialGradientBrush:", g, new RectangleF(ip, new SizeF(500, 100)));
                ip.Y = rc.Bottom + dy;
                // Centered:
                // testRectSize.Height *= 2;
                RadialGradientBrush radialGradBrush = new RadialGradientBrush(Color.Orange, Color.Purple);
                drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
                // Center in bottom right corner:
                radialGradBrush = new RadialGradientBrush(Color.OrangeRed, Color.DarkBlue, new PointF(1, 1));
                drawSwatch(radialGradBrush, $"Radial gradient\r\nwith origin at {radialGradBrush.GradientOrigin}");
            }
            // Done:
            return bmp;
        }
    }
}