Watermark2.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 System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;

namespace GcImagingWeb.Samples
{
    // 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(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            // 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(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            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(pixelWidth / 2, pixelHeight / 2));
                var tf = new TextFormat()
                {
                    Font = Font.FromFile(Path.Combine("Resources", "Fonts", "calibrib.ttf")),
                    FontSize = 14,
                    ForeColor = Color.FromArgb(64, Color.White),

                };
                var tl = g.CreateTextLayout();
                tl.Append("Copyright (c) GrapeCity", 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) * pixelHeight / 2); ;
                int offY = (int)(Math.Sin(rad) * pixelWidth / 2);
                for (float y = offY; y < pixelHeight - offY; y += dy, ++n)
                    for (float x = offX + dx / 2 * (n % 2); x < pixelWidth - offX; x += dx)
                        g.DrawTextLayout(tl, new PointF(x, y));
            }
            return bmp;
        }
    }
}