ShowHiClipping.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 System.Numerics;
using GrapeCity.Documents.Drawing;
using GrapeCity.Documents.Text;
using GrapeCity.Documents.Imaging;

namespace GcImagingWeb.Samples
{
    // This sample demonstrates how to find and show
    // pixels with blown out highlights.
    public class ShowHiClipping
    {
        public GcBitmap GenerateImage(int pixelWidth = 1024, int pixelHeight = 1024, bool opaque = true, float dpiX = 96, float dpiY = 96)
        {
            var bmp = new GcBitmap(pixelWidth, pixelHeight, opaque, dpiX, dpiY);
            using (var bmpSrc = new GcBitmap(Path.Combine("Resources", "Images", "tudor.jpg")))
            {
                // BitBlt requires the opacity of both images to be the same:
                bmpSrc.Opaque = opaque;
                // Render source image onto the target bitmap
                // (generally we might want to resize the source image first,
                // but in this case we just know that the source image has
                // the same size as the target, so skip this step):
                bmp.BitBlt(bmpSrc, 0, 0);

                using (var g = bmp.CreateGraphics())
                {
                    for (int i = 0; i < bmp.PixelWidth; ++i)
                        for (int j = 0; j < bmp.PixelHeight; ++j)
                        {
                            uint px = bmp[i, j];
                            // If any of the colors are 0xFF, we change the pixel's color to magenta:
                            if ((px & 0x000000FF) == 0x000000FF || (px & 0x0000FF00) == 0x0000FF00 || (px & 0x00FF0000) == 0x00FF0000)
                                bmp[i, j] = 0xFFFF00FF;
                        }
                    // Draw the original image in the bottom right corner for reference:
                    float sx = pixelWidth / 3, sy = pixelHeight / 3;
                    g.DrawImage(bmpSrc, new RectangleF(sx * 2, sy * 2, sx, sy), null, ImageAlign.StretchImage);
                }
            }
            return bmp;
        }
    }
}