Watermark2.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 how to render a watermark on an image
    // at an angle using a rotation transformation on the bitmap graphics.
    public class Watermark2
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            // Note: we can use Color.Transparent instead of a solid background,
            // but the resulting image format must support transparency for this
            // to work as expected:
            var backColor = Color.FromArgb(unchecked((int)0xff0066cc));
            var foreColor = Color.FromArgb(unchecked((int)0xffffcc00));
            float angle = -30;
            var rad = (float)(angle * Math.PI) / 180f;

            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "ImagesBis", "alpamayo-sq.jpg")))
            {
                // BitBlt requires the opacity of both images to be the same:
                bmpSrc.Opaque = opaque;
                // Render source image onto the target bitmap
                // (generally we might want to resize the source image first,
                // but in this case we just know that the source image has
                // the same size as the target, so skip this step):
                bmp.BitBlt(bmpSrc, 0, 0);
            }

            using (var g = bmp.CreateGraphics())
            {
                // Draw watermark text in a loop over all image at an angle:
                g.Transform = Matrix3x2.CreateRotation((float)(angle * Math.PI) / 180f, new Vector2(pixelSize.Width / 2, pixelSize.Height / 2));
                var tf = new TextFormat()
                {
                    Font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
                    FontSize = 14,
                    ForeColor = Color.FromArgb(64, Color.White),

                };
                var tl = g.CreateTextLayout();
                tl.Append("Copyright (c) MESCIUS", tf);
                tl.PerformLayout(true);
                int dx = (int)tl.ContentWidth * 3;
                int dy = (int)tl.ContentHeight * 5;
                int n = 0;
                int offX = -(int)(Math.Cos(rad) * pixelSize.Height / 2); ;
                int offY = (int)(Math.Sin(rad) * pixelSize.Width / 2);
                for (float y = offY; y < pixelSize.Height - offY; y += dy, ++n)
                    for (float x = offX + dx / 2 * (n % 2); x < pixelSize.Width - offX; x += dx)
                        g.DrawTextLayout(tl, new PointF(x, y));
            }
            return bmp;
        }
    }
}