HatchStyles.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 System.Collections.Generic;
using System.Linq;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;

namespace GcImagingWeb.Samples
{
    // This sample demonstrates the different hatch brush styles.
    // Many of those styles implement MS Excel pattern styles.
    public class HatchStyles
    {
        public GcBitmap GenerateImage (int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            // HatchStyle
            var hatchStyles = Enum.GetValues(typeof(HatchStyle));
            int COLS = 4;
            var w = pixelWidth / COLS;
            var h = pixelHeight / ((hatchStyles.Length + COLS -1) / COLS);

            var tf = new TextFormat
            {
                Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                FontSize = 12,
            };

            var bmp = new GcBitmap(pixelWidth, pixelHeight, true, dpiX, dpiY);
            using (var g = bmp.CreateGraphics(Color.White))
            {
                int row = 0, col = 0;
                foreach (HatchStyle hs in hatchStyles)
                {
                    // Hatch:
                    var rc = new RectangleF(col * w, row * h, w, h);
                    var b = new HatchBrush(hs)
                    {
                        ForeColor = Color.Black,
                        BackColor = Color.White
                    };
                    g.FillRectangle(rc, b);
                    // Caption:
                    var cap = hs.ToString();
                    var scap = g.MeasureString(cap, tf);
                    var rcap = new RectangleF(
                        rc.X + (rc.Width - scap.Width) / 2,
                        rc.Y + (rc.Height - scap.Height) / 2,
                        scap.Width, scap.Height);
                    rcap.Inflate(3, 2);
                    g.FillRectangle(rcap, Color.White);
                    g.DrawString(cap, tf, rcap, TextAlignment.Center, ParagraphAlignment.Center);
                    // Move on:
                    if (++col >= COLS)
                    {
                        col = 0;
                        ++row;
                    }
                }
            }
            return bmp;
        }
    }
}