RoundCLip.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 how to make a round clipping of a part of an image
    // and render it into a GcBitmap.
    public class RoundCLip
    {
        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));
            const int bottom = 144, pad = 36;
            int minSize = Math.Min(pixelWidth, pixelHeight) / 6, maxSize = (int)(Math.Min(pixelWidth, pixelHeight) / 1.5);

            // Randomize some parameters of the sample:
            var rnd = new Random();

            // It is a good idea to dispose bitmaps which are no longer needed,
            // so we are 'using' all bitmaps except the one returned:
            using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "Stock", "woman-brick-wall.jpg")))
            {
                // Make sure source and target opacity match:
                bmpSrc.Opaque = opaque;

                // Coordinates and size of the clipping in the source image:
                const int x = 143, y = 0, w = 655, h = 655;

                // Create a clipping region excluding all outside of the specified circle:
                var rgn = new Region(new RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight));
                var ellipse = new EllipticFigure(x, y, w, h);
                rgn.CombineWithRegion(new Region(ellipse), RegionCombineMode.Exclude, false);

                // To clip using a Region, we need to use the BitmapRenderer.
                // NOTE (new in v2sp2): the renderer is not created by default,
                // we must call EnsureRendererCreated() prior to using it:
                bmpSrc.EnsureRendererCreated();
                var renderer = bmpSrc.Renderer;
                renderer.ClipRegion = rgn;
                renderer.Clear(Color.Transparent);
                var size = rnd.Next(minSize, maxSize);
                using (var bmpRound = bmpSrc.Clip(new Rectangle(x, y, w, h)))
                using (var bmpSmall = bmpRound.Resize(size, size))
                {
                    var bmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
                    bmp.Clear(Color.Transparent);
                    bmp.BitBlt(bmpSmall,
                        rnd.Next(pad, pixelWidth - pad - bmpSmall.PixelWidth),
                        rnd.Next(pad, pixelHeight - pad - bottom - bmpSmall.PixelHeight));
                    return bmp;
                }
            }
        }
    }
}