EnlargeImage.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 shows how to enlarge a loaded image
    // using different interpolation modes.
    public class EnlargeImage
    {
        public GcBitmap GenerateImage (int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var origImagePath = Path.Combine("Resources", "Stock", "woman-window-small.jpg");
            // Create and clear the target bitmap:
            var targetBmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            targetBmp.Clear(Color.Transparent);

            const int fontSize = 16, fpad = 4, xpad = 4, ypad = 3;
            TextFormat tf = new TextFormat
            {
                Font = Font.FromFile(Path.Combine("Resources", "Fonts", "times.ttf")),
                FontSize = fontSize,
            };
            using (var origBmp = new GcBitmap())
            {
                using (var stm = new FileStream(origImagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    origBmp.Load(stm);

                // Make sure opaqueness of the original bitmap matches the target:
                origBmp.Opaque = targetBmp.Opaque;

                // Draw the original with the original size:
                targetBmp.BitBlt(origBmp, 0, 0);
                using (var g = targetBmp.CreateGraphics(null))
                    g.DrawString($"⟵ Original image ({origBmp.Width} by {origBmp.Height} pixels)", tf, new PointF(origBmp.Width + fpad, fpad));
                int dy = (int)origBmp.Height + ypad;

                // Enlarge image so that we can have 4 enlarged tiled images preserving aspect ratio:
                var f = Math.Min(
                    (targetBmp.Width - xpad) / origBmp.Width / 2,
                    (targetBmp.Height - ypad - dy) / origBmp.Height / 2);
                int twidth = (int)(origBmp.Width * f);
                int theight = (int)(origBmp.Height * f);

                // Enlarge and draw 4 copies using the 4 available interpolation modes:
                using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor))
                    targetBmp.BitBlt(bmp, 0, dy);
                drawCaption("InterpolationMode.NearestNeighbor", 0, dy);

                using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Linear))
                    targetBmp.BitBlt(bmp, 0, dy + theight + ypad);
                drawCaption("InterpolationMode.Linear", 0, dy + theight + ypad);

                using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic))
                    targetBmp.BitBlt(bmp, twidth + xpad, dy);
                drawCaption("InterpolationMode.Cubic", twidth + xpad, dy);

                using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Downscale))
                    targetBmp.BitBlt(bmp, twidth + xpad, dy + theight + ypad);
                drawCaption("InterpolationMode.Downscale", twidth + xpad, dy + theight + ypad);
                //
                void drawCaption(string caption, float x, float y)
                {
                    using (var g = targetBmp.CreateGraphics(null))
                        g.DrawString(caption, tf, new RectangleF(x + fpad, y + theight - fontSize - fpad * 2, twidth - fpad * 2, fontSize + fpad * 2));
                }
            }
            return targetBmp;
        }
    }
}