Fade and shape images with soft masks
View as MarkdownThis 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
Chicago.jpg
Stamp.png
Output
ApplySoftMask.pdf
DownloadApplySoftMask.png
Used APIs
- Aurigma.GraphicsMill.Bitmap.Transforms
- Aurigma.GraphicsMill.Codecs.PdfWriter
- Aurigma.GraphicsMill.Codecs.PdfWriter.AddPage(Single,Single,Single,Single)
- Aurigma.GraphicsMill.Codecs.PdfWriter.GetGraphics
- Aurigma.GraphicsMill.Drawing.ColorStop
- Aurigma.GraphicsMill.Drawing.ColorStop.Color
- Aurigma.GraphicsMill.Drawing.ColorStop.Position
- Aurigma.GraphicsMill.Drawing.GradientBrush.ColorStops
- Aurigma.GraphicsMill.Drawing.Graphics.DrawContainer(GraphicsContainer,Single,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.DrawImage(Bitmap,Single,Single)
- Aurigma.GraphicsMill.Drawing.Graphics.FillRectangle(Brush,RectangleF)
- Aurigma.GraphicsMill.Drawing.GraphicsContainer
- Aurigma.GraphicsMill.Drawing.GraphicsContainer.GetGraphics
- Aurigma.GraphicsMill.Drawing.GraphicsContainer.Items
- Aurigma.GraphicsMill.Drawing.ImageItem
- Aurigma.GraphicsMill.Drawing.ImageItem.Image
- Aurigma.GraphicsMill.Drawing.ImageItem.Position
- Aurigma.GraphicsMill.Drawing.Item.SoftMask
- Aurigma.GraphicsMill.Drawing.LinearGradientBrush
- Aurigma.GraphicsMill.Drawing.LinearGradientBrush.EndPoint
- Aurigma.GraphicsMill.Drawing.LinearGradientBrush.StartPoint
- Aurigma.GraphicsMill.Drawing.SoftMask
- Aurigma.GraphicsMill.Drawing.SoftMask.Method
- Aurigma.GraphicsMill.Drawing.SoftMaskMethod
- Aurigma.GraphicsMill.Transforms.ResizeInterpolationMode
- Aurigma.GraphicsMill.Transforms.ResizeMode
- Aurigma.GraphicsMill.TransformsProvider.Resize(Int32,Int32,ResizeInterpolationMode,ResizeMode)