EnlargeImage.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 shows how to enlarge a loaded image
    // using different interpolation modes.
    public class EnlargeImage
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            var origImagePath = Path.Combine("Resources", "Stock", "woman-window-small.jpg");
            // Create and clear the target bitmap:
            var targetBmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            targetBmp.Clear(Color.Transparent);

            const int fontSize = 16, fpad = 4, xpad = 4, ypad = 3;
            TextFormat tf = new TextFormat
            {
                Font = GCTEXT.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;
        }
    }
}