Fade and shape images with soft masks

View as Markdown

DrawingGradientGraphics ContainerPDF

This sample shows how to make parts of an item gradually transparent with a soft mask, the PDF way of masking that keeps both the item and the mask vector. A soft mask is a GraphicsContainer assigned to an item through its SoftMask property. With SoftMaskMethod.Luminosity the brightness of the mask sets the opacity: here a white-to-black gradient fades a photo out to the right. With SoftMaskMethod.Alpha only the transparency of the mask counts and its colours are ignored: here the alpha channel of a PNG gives a second photo the shape of a postage stamp.

Code Snippet

const float width = 800, height = 400, dpi = 72;

using (var venice = new Bitmap("Venice.jpg"))
using (var chicago = new Bitmap("Chicago.jpg"))
using (var stamp = new Bitmap("Stamp.png"))
using (var page = new GraphicsContainer(width, height, dpi, dpi))
using (var fade = new GraphicsContainer(width, height, dpi, dpi))
using (var silhouette = new GraphicsContainer(width, height, dpi, dpi))
{
    venice.Transforms.Resize(360, 360, ResizeInterpolationMode.High, ResizeMode.Fit);
    chicago.Transforms.Resize(360, 360, ResizeInterpolationMode.High, ResizeMode.ImageFill);
    stamp.Transforms.Resize(360, 360, ResizeInterpolationMode.High, ResizeMode.Fit);

    var veniceRect = new RectangleF(20, (height - venice.Height) / 2, venice.Width, venice.Height);
    var stampPosition = new PointF(420, (height - stamp.Height) / 2);

    // Luminosity: white keeps the photo, black hides it, greys in between make it partly transparent.
    using (var graphics = fade.GetGraphics())
    {
        var gradient = new LinearGradientBrush()
        {
            StartPoint = new PointF(veniceRect.Left, 0),
            EndPoint = new PointF(veniceRect.Right, 0),
            ColorStops = new ColorStop[]
            {
                new ColorStop() { Color = RgbColor.White, Position = 0.3f },
                new ColorStop() { Color = RgbColor.Black, Position = 1.0f },
            },
        };

        graphics.FillRectangle(gradient, veniceRect);
    }

    page.Items.Add(new ImageItem()
    {
        Image = venice,
        Position = veniceRect.Location,
        SoftMask = new SoftMask(fade) { Method = SoftMaskMethod.Luminosity },
    });

    // Alpha: only the transparency of the mask counts, the colours of the stamp are ignored.
    using (var graphics = silhouette.GetGraphics())
    {
        graphics.DrawImage(stamp, stampPosition.X, stampPosition.Y);
    }

    page.Items.Add(new ImageItem()
    {
        Image = chicago,
        Position = stampPosition,
        SoftMask = new SoftMask(silhouette) { Method = SoftMaskMethod.Alpha },
    });

    using (var writer = new PdfWriter("ApplySoftMask.pdf"))
    using (var graphics = writer.GetGraphics())
    {
        writer.AddPage(width, height, dpi, dpi);
        graphics.DrawContainer(page, 0, 0);
    }
}

Input

Venice.jpg

Venice.jpg

Chicago.jpg

Chicago.jpg

Stamp.png

Stamp.png

Output

ApplySoftMask.pdf

Download

ApplySoftMask.png

ApplySoftMask.png

Used APIs