---
title: "Fade and shape images with soft masks"
canonical: https://www.graphicsmill.com/samples/Drawing/ClippingPaths/ApplySoftMask_cs
product: "Graphics Mill"
source: "Drawing/ClippingPaths/ApplySoftMask.cs"
---

# Fade and shape images with soft masks

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.

## Instruction

Use the drawing API to place two photos into a graphics container and write it to PDF: fade the first photo out with a linear gradient used as a luminosity soft mask, and give the second photo the shape of a transparent PNG used as an alpha soft mask.

## Usings

```csharp
using System.Drawing;
using Aurigma.GraphicsMill.Codecs;
using Aurigma.GraphicsMill.Drawing;
using Aurigma.GraphicsMill.Transforms;
```

## Code Snippet

```csharp
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);
    }
}
```

## Explanation

The code:

- Loads two photos and the transparent "Stamp.png" into ` Bitmap ` objects and resizes them with ` Bitmap.Transforms.Resize `.
- Creates a page-size ` GraphicsContainer ` as the first mask and fills the area of the first photo with a white-to-black ` LinearGradientBrush `.
- Adds an ` ImageItem ` for the first photo with a ` SoftMask ` in ` SoftMaskMethod.Luminosity ` mode, so white keeps the photo and black hides it.
- Creates the second mask by drawing the stamp into another ` GraphicsContainer ` with ` Graphics.DrawImage `.
- Adds an ` ImageItem ` for the second photo with a ` SoftMask ` in ` SoftMaskMethod.Alpha ` mode, so only the transparency of the stamp shapes it.
- Writes the container to "ApplySoftMask.pdf" with ` PdfWriter ` and ` Graphics.DrawContainer `; the masks are written as PDF soft masks.

## Tags

- [Drawing](<https://www.graphicsmill.com/samples/tags/drawing_md>): Drawing vector shapes and text
- [Gradient](<https://www.graphicsmill.com/samples/tags/gradient_md>): Using gradient brushes and fills
- [Graphics Container](<https://www.graphicsmill.com/samples/tags/container_md>): Using graphics containers for grouped items
- [PDF](<https://www.graphicsmill.com/samples/tags/pdf_md>): Importing and exporting PDF documents

## Input

- [Venice.jpg](<https://www.graphicsmill.com/samples/Drawing/compiled/Debug/Venice_jpg>) (preview)
- [Chicago.jpg](<https://www.graphicsmill.com/samples/Drawing/compiled/Debug/Chicago_jpg>) (preview)
- [Stamp.png](<https://www.graphicsmill.com/samples/Drawing/compiled/Debug/Stamp_png>) (preview)

## Output

- [ApplySoftMask.pdf](<https://www.graphicsmill.com/samples/Drawing/compiled/Debug/ApplySoftMask_pdf>) (download)
- [ApplySoftMask.png](<https://www.graphicsmill.com/samples/Drawing/compiled/Debug/ApplySoftMask_png>) (preview)

## Used APIs

- [Aurigma.GraphicsMill.Bitmap.Transforms](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Bitmap_Transforms.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Codecs_PdfWriter.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_AddPage_4_37f6c741.htm.md>)
- [Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Codecs_PdfWriter_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ColorStop](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_ColorStop.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ColorStop.Color](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_ColorStop_Color.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ColorStop.Position](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_ColorStop_Position.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GradientBrush.ColorStops](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GradientBrush_ColorStops.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawContainer(GraphicsContainer,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawContainer_3_098aa7ce.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.DrawImage(Bitmap,Single,Single)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_DrawImage_3_b9413d4f.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Graphics.FillRectangle(Brush,RectangleF)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_Graphics_FillRectangle_2_9763ccb2.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_GraphicsContainer.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.GetGraphics](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_Drawing_GraphicsContainer_GetGraphics.htm.md>)
- [Aurigma.GraphicsMill.Drawing.GraphicsContainer.Items](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_GraphicsContainer_Items.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ImageItem](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_ImageItem.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ImageItem.Image](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_ImageItem_Image.htm.md>)
- [Aurigma.GraphicsMill.Drawing.ImageItem.Position](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_ImageItem_Position.htm.md>)
- [Aurigma.GraphicsMill.Drawing.Item.SoftMask](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_Item_SoftMask.htm.md>)
- [Aurigma.GraphicsMill.Drawing.LinearGradientBrush](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_LinearGradientBrush.htm.md>)
- [Aurigma.GraphicsMill.Drawing.LinearGradientBrush.EndPoint](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_LinearGradientBrush_EndPoint.htm.md>)
- [Aurigma.GraphicsMill.Drawing.LinearGradientBrush.StartPoint](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_LinearGradientBrush_StartPoint.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SoftMask](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_SoftMask.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SoftMask.Method](<https://www.graphicsmill.com/docs/gm/P_Aurigma_GraphicsMill_Drawing_SoftMask_Method.htm.md>)
- [Aurigma.GraphicsMill.Drawing.SoftMaskMethod](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Drawing_SoftMaskMethod.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResizeInterpolationMode.htm.md>)
- [Aurigma.GraphicsMill.Transforms.ResizeMode](<https://www.graphicsmill.com/docs/gm/T_Aurigma_GraphicsMill_Transforms_ResizeMode.htm.md>)
- [Aurigma.GraphicsMill.TransformsProvider.Resize(Int32,Int32,ResizeInterpolationMode,ResizeMode)](<https://www.graphicsmill.com/docs/gm/M_Aurigma_GraphicsMill_TransformsProvider_Resize_4_9f3f3567.htm.md>)
