BrightnessContrast.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 System.Numerics;
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 use the BrightnessContrastEffect.
    public class BrightnessContrast
    {
        public GcBitmap GenerateImage(Size pixelSize, float dpi, bool opaque, string[] sampleParams = null)
        {
            opaque = false;
            var bmp = new GcBitmap(pixelSize.Width, pixelSize.Height, opaque, dpi, dpi);
            using (var origBmp = new GcBitmap())
            {
                // Load a sample photo:
                var imagePath = Path.Combine("Resources", "Stock", "woman-window.jpg");
                using (var stm = new FileStream(imagePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, FileOptions.RandomAccess))
                    origBmp.Load(stm);

                origBmp.SetAlphaTo255();
                origBmp.Opaque = false;

                // Resize the original photo so we can place 4 samples of it
                // on the resulting bitmap:
                int w = pixelSize.Width / 2;
                int h = pixelSize.Height / 2;
                using (var sizedBmp = origBmp.Resize(w, h, InterpolationMode.Cubic))
                {
                    // Copy the resized original into 4 quadrants of the resulting bitmap:
                    bmp.BitBlt(sizedBmp, 0, 0);
                    bmp.BitBlt(sizedBmp, w, 0);
                    bmp.BitBlt(sizedBmp, 0, h);
                    bmp.BitBlt(sizedBmp, w, h);
                }

                // Keep the pixels in top left quadrant intact,
                // apply effects to the other 3 quadrants:
                var lineh = 2;
                bmp.ApplyEffect(BrightnessContrastEffect.Get(40, 0), new Rectangle(w + lineh, 0, w - lineh, h - lineh));
                bmp.ApplyEffect(BrightnessContrastEffect.Get(0, 40), new Rectangle(0, h + lineh, w - lineh, h - lineh));
                bmp.ApplyEffect(BrightnessContrastEffect.Get(40, 40), new Rectangle(w + lineh, h + lineh, w - lineh, h - lineh));

                // Add borders between the quadrants, and captions for each:
                using (var g = bmp.CreateGraphics(null))
                {
                    var foreColor = Color.Yellow;
                    var backColor = Color.Blue;
                    var font = GCTEXT.Font.FromFile(Path.Combine("Resources", "Fonts", "cour.ttf"));
                    g.DrawLine(w, 0, w, h * 2, new GCDRAW.Pen(Color.Gray, lineh * 2));
                    g.DrawLine(0, h, w * 2, h, new GCDRAW.Pen(Color.Gray, lineh * 2));
                    var tf = new TextFormat() { Font = font, FontSize = 18, ForeColor = foreColor, BackColor = backColor, FontBold = true };
                    var th = g.MeasureString("QWERTY", tf).Height;
                    g.DrawString(" Original image ", tf, new PointF(0, h - th + lineh));
                    g.DrawString(" Brightness +40 ", tf, new PointF(w + lineh, h - th + lineh));
                    g.DrawString(" Contrast +40 ", tf, new PointF(0, h * 2 + lineh - th + lineh));
                    g.DrawString(" Brightness +40, Contrast +40 ", tf, new PointF(w + lineh, h * 2 + lineh - th + lineh));
                }
            }
            return bmp;
        }
    }
}