CreateThumbnails.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 resize (downscale) an existing image
    // to create a thumbnail, using different interpolation modes.
    public class DownscaleImage
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var origImagePath = Path.Combine("Resources", "Images", "minerva.jpg");
            // Create and fill the target bitmap:
            var targetBmp = new GcBitmap(pixelWidth, pixelHeight, true, dpiX, dpiY);
            using (var g = targetBmp.CreateGraphics(Color.FromArgb(unchecked((int)0xff004d99))))
            {
                // creating a graphics with a fill color does what we need
            }
            const int fontSize = 16, fpad = 4, xpad = 10, ypad = 3;
            TextFormat tf = new TextFormat
            {
                ForeColor = Color.FromArgb(unchecked((int)0xffffcc00)),
                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);

                // Bitwise copy the original image 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(fpad, origBmp.Height + fpad));
                int dx = (int)origBmp.Width + xpad;

                // Scale down the image using the various interpolation modes,
                // and render those on the right of the original:
                float f = (origBmp.Height - ypad * 3) / origBmp.Height / 4;
                int twidth = (int)Math.Round(origBmp.Width * f);
                int theight = (int)Math.Round(origBmp.Height * f);

                using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.NearestNeighbor))
                    targetBmp.BitBlt(bmp, dx, 0);
                drawCaption("InterpolationMode.NearestNeighbor", dx, 0);

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

                using (var bmp = origBmp.Resize(twidth, theight, InterpolationMode.Cubic))
                    targetBmp.BitBlt(bmp, dx, (theight + ypad) * 2);
                drawCaption("InterpolationMode.Cubic", dx, (theight + ypad) * 2);

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