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

            // Randomize some parameters of the sample:
            var rnd = Common.Util.NewRandom();

            // 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 GrapeCity.Documents.Imaging.Region(new RectangularFigure(0, 0, bmpSrc.PixelWidth, bmpSrc.PixelHeight));
                var ellipse = new EllipticFigure(x, y, w, h);
                rgn.CombineWithRegion(new GrapeCity.Documents.Imaging.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(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
                    bmp.Clear(Color.Transparent);
                    bmp.BitBlt(bmpSmall,
                        rnd.Next(pad, pixelSize.Width - pad - bmpSmall.PixelWidth),
                        rnd.Next(pad, pixelSize.Height - pad - bottom - bmpSmall.PixelHeight));
                    return bmp;
                }
            }
        }
    }
}