Class ImageGrayscaleMask

ImageGrayscaleMask class

Describes a grayscale image mask.

public class ImageGrayscaleMask : IImageMask

Constructors

NameDescription
ImageGrayscaleMask(RasterImage)Initializes a new instance of the ImageGrayscaleMask class with the size of the specified existing RasterImage. Specified RasterImage will be stored as source image.
ImageGrayscaleMask(int, int)Initializes a new instance of the ImageGrayscaleMask class with the specified width and height.

Properties

NameDescription
Bounds { get; }Gets the bounds, in pixels, of this mask.
Height { get; }Gets the height, in pixels, of this mask.
Item { get; set; }Gets or sets the opacity of the specified pixel.
SelectionBounds { get; }Gets the bounds of the selected part of the mask, in pixels.
Source { get; }Gets the source image used to create this mask, if exists.
Width { get; }Gets the width, in pixels, of this mask.

Methods

NameDescription
Apply()Applies current mask to the RasterImage source, if exists.
ApplyTo(RasterImage)Applies current mask to the specified RasterImage.
Clone()Creates a new object that is a copy of the current instance.
Crop(Rectangle)Crops mask with the specified rectangle.
Crop(Size)Crops mask with the specified size.
Crop(int, int)Crops mask with the specified width and height.
ExclusiveDisjunction(ImageGrayscaleMask)Gets the exclusive disjunction of current mask with provided.
GetByteOpacity(int, int)Gets the opacity of the specified pixel with byte precision.
Intersect(ImageGrayscaleMask)Gets the intersection of current mask with provided.
Invert()Gets the inversion of the current mask.
IsOpaque(int, int)Checks if the specified pixel is opaque.
IsTransparent(int, int)Checks if the specified pixel is transparent.
Subtract(ImageGrayscaleMask)Gets the subtraction of the provided mask from current.
Union(ImageGrayscaleMask)Union of two masks.
operator +Union of two masks.
operator ^Exclusive disjunction of two masks.
operator !Inverts mask.
operator *Intersection of two masks.
operator -Subtract second mask from first.

Examples

The example shows how to select a complicated area of an image using Magic Wand tool and the ability to interact with masks (invert, union, substract).

[C#]

var imageFilePath = "input.png"; 
using (RasterImage image = (RasterImage)Image.Load(inputFilePath))
{
    // Create a new mask using magic wand tool based on tone and color of pixel (845, 128)
    MagicWandTool.Select(image, new MagicWandSettings(845, 128))
        // Union the existing mask with the specified one created by magic wand tool
        .Union(new MagicWandSettings(416, 387))
        // Invert the existing mask
        .Invert()
        // Subtract the specified mask created by magic wand tool with specified threshold from the existing one 
        .Subtract(new MagicWandSettings(1482, 346) { Threshold = 69 })
        // Subtract four specified rectangle masks from the existing mask one by one
        .Subtract(new RectangleMask(0, 0, 800, 150))
        .Subtract(new RectangleMask(0, 380, 600, 220))
        .Subtract(new RectangleMask(930, 520, 110, 40))
        .Subtract(new RectangleMask(1370, 400, 120, 200))
        // Feather mask with specified settings
        .GetFeathered(new FeatheringSettings() { Size = 3 })
        // Apply mask to the image
        .Apply();
        
    // Save image
    image.Save(outputFilePath);
}

See Also