Class ImageMask

Class ImageMask

Namespace: Aspose.Imaging.MagicWand.ImageMasks
Assembly: Aspose.Imaging.dll (25.2.0)

Describes a binary image mask.

public abstract class ImageMask : IImageMask, ICloneable

Inheritance

objectImageMask

Derived

CircleMask, EmptyImageMask, ImageBitMask, RectangleMask

Implements

IImageMask, ICloneable

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

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).```csharp [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);
                                                                                                                                                              }

## Constructors

### <a id="Aspose_Imaging_MagicWand_ImageMasks_ImageMask__ctor_System_Int32_System_Int32_"></a> ImageMask\(int, int\)

Initializes a new instance of the Aspose.Imaging.MagicWand.ImageMasks.ImageMask class with the specified width and height.

```csharp
protected ImageMask(int width, int height)

Parameters

width int

Width of the mask.

height int

Height of the mask.

Properties

Bounds

Gets the bounds, in pixels, of this mask.

public Rectangle Bounds { get; }

Property Value

Rectangle

Height

Gets the height, in pixels, of this mask.

public int Height { get; protected set; }

Property Value

int

SelectionBounds

Gets the bounds of the selected part of the mask, in pixels.

public abstract Rectangle SelectionBounds { get; }

Property Value

Rectangle

Source

Gets the source image used to create this mask, if exists.

public RasterImage Source { get; protected set; }

Property Value

RasterImage

Width

Gets the width, in pixels, of this mask.

public int Width { get; protected set; }

Property Value

int

this[int, int]

Gets the opacity of the specified pixel.

public abstract bool this[int x, int y] { get; }

Property Value

bool

Methods

Apply()

Applies current mask to the Aspose.Imaging.RasterImage source, if exists.

public void Apply()

Examples

The example shows how to select a simple area of an image based on tone and color of any pixel using Magic Wand tool.```csharp [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 (120, 100) with custom threshold equal to 150
                                                                                                                                MagicWandTool
                                                                                                                                    .Select(image, new MagicWandSettings(120, 100) { Threshold = 150 })
                                                                                                                                    // Apply mask to the image
                                                                                                                                    .Apply();

                                                                                                                                // Save image with forced transparency color type option
                                                                                                                                image.Save(outputFilePath, new ImageOptions.PngOptions()
                                                                                                                                {
                                                                                                                                    ColorType = PngColorType.TruecolorWithAlpha
                                                                                                                                });
                                                                                                                            }

#### Exceptions

 [NullReferenceException](https://learn.microsoft.com/dotnet/api/system.nullreferenceexception)

Thrown when the source image is not defined.

### <a id="Aspose_Imaging_MagicWand_ImageMasks_ImageMask_ApplyTo_Aspose_Imaging_RasterImage_"></a> ApplyTo\(RasterImage\)

Applies current mask to the specified Aspose.Imaging.RasterImage.

```csharp
public void ApplyTo(RasterImage image)

Parameters

image RasterImage

Image to apply mask to.

Exceptions

ArgumentNullException

Thrown when the image is not defined.

Clone()

Creates a new object that is a copy of the current instance.

public abstract object Clone()

Returns

object

A new object that is a copy of this instance.

Crop(Size)

Crops mask with the specified size.

public ImageMask Crop(Size size)

Parameters

size Size

The specified size.

Returns

ImageMask

An ImageMask.

Crop(int, int)

Crops mask with the specified width and height.

public ImageMask Crop(int width, int height)

Parameters

width int

The specified width.

height int

The specified height.

Returns

ImageMask

An ImageMask.

Crop(Rectangle)

Crops mask with the specified rectangle.

public abstract ImageMask Crop(Rectangle rectangle)

Parameters

rectangle Rectangle

The specified rectangle.

Returns

ImageMask

An ImageMask.

ExclusiveDisjunction(ImageMask)

Gets the exclusive disjunction of current mask with provided.

public ImageBitMask ExclusiveDisjunction(ImageMask mask)

Parameters

mask ImageMask

Provided mask

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

ExclusiveDisjunction(MagicWandSettings)

Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the source of the mask.

public ImageBitMask ExclusiveDisjunction(MagicWandSettings settings)

Parameters

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Exceptions

ArgumentNullException

Thrown when the source image is not defined in mask.

ExclusiveDisjunction(RasterImage, MagicWandSettings)

Gets the exclusive disjunction of the current mask with the result of magic wand selection applied to the provided image.

public ImageBitMask ExclusiveDisjunction(RasterImage image, MagicWandSettings settings)

Parameters

image RasterImage

Image for magic wand.

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

GetByteOpacity(int, int)

Gets the opacity of the specified pixel with byte precision.

public byte GetByteOpacity(int x, int y)

Parameters

x int

The x-coordinate of the pixel.

y int

The y-coordinate of the pixel.

Returns

byte

Byte value, representing the opacity of the specified pixel.

GetFeathered(FeatheringSettings)

Gets grayscale mask with the border feathered with the specified settings.

public ImageGrayscaleMask GetFeathered(FeatheringSettings settings = null)

Parameters

settings FeatheringSettings

Feathering settings.

Returns

ImageGrayscaleMask

Aspose.Imaging.MagicWand.ImageMasks.ImageGrayscaleMask with feathered border.

Inflate(int)

Inflates this mask by the specified amount.

public abstract ImageMask Inflate(int size)

Parameters

size int

The amount to inflate this mask.

Returns

ImageMask

An ImageMask.

Intersect(ImageMask)

Gets the intersection of current mask with provided.

public ImageBitMask Intersect(ImageMask mask)

Parameters

mask ImageMask

Provided mask

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Intersect(MagicWandSettings)

Gets the intersection of the current mask with the result of magic wand selection applied to the source of the mask.

public ImageBitMask Intersect(MagicWandSettings settings)

Parameters

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Exceptions

ArgumentNullException

Thrown when the source image is not defined in mask.

Intersect(RasterImage, MagicWandSettings)

Gets the intersection of the current mask with the result of magic wand selection applied to the provided image.

public ImageBitMask Intersect(RasterImage image, MagicWandSettings settings)

Parameters

image RasterImage

Image for magic wand.

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Invert()

Gets the inversion of the current mask.

public ImageBitMask Invert()

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

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).```csharp [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);
                                                                                                                                                              }

### <a id="Aspose_Imaging_MagicWand_ImageMasks_ImageMask_IsOpaque_System_Int32_System_Int32_"></a> IsOpaque\(int, int\)

Checks if the specified pixel is opaque.

```csharp
public bool IsOpaque(int x, int y)

Parameters

x int

The x-coordinate of the pixel.

y int

The y-coordinate of the pixel.

Returns

bool

true if the specified pixel is opaque; otherwise, false.

IsTransparent(int, int)

Checks if the specified pixel is transparent.

public bool IsTransparent(int x, int y)

Parameters

x int

The x-coordinate of the pixel.

y int

The y-coordinate of the pixel.

Returns

bool

true if the specified pixel is transparent; otherwise, false.

Subtract(ImageMask)

Gets the subtraction of the provided mask from current.

public ImageBitMask Subtract(ImageMask mask)

Parameters

mask ImageMask

Provided mask

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

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).```csharp [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);
                                                                                                                                                              }

### <a id="Aspose_Imaging_MagicWand_ImageMasks_ImageMask_Subtract_Aspose_Imaging_MagicWand_MagicWandSettings_"></a> Subtract\(MagicWandSettings\)

Gets the result of magic wand selection applied to the source of the current mask subtracted from the mask.

```csharp
public ImageBitMask Subtract(MagicWandSettings settings)

Parameters

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Exceptions

ArgumentNullException

Thrown when the source image is not defined in mask.

Subtract(RasterImage, MagicWandSettings)

Gets the result of magic wand selection applied to the provided image subtracted from the current mask.

public ImageBitMask Subtract(RasterImage image, MagicWandSettings settings)

Parameters

image RasterImage

Image for magic wand.

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Union(ImageMask)

Gets the union of the current mask with provided.

public ImageBitMask Union(ImageMask mask)

Parameters

mask ImageMask

Provided mask

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

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).```csharp [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);
                                                                                                                                                              }

### <a id="Aspose_Imaging_MagicWand_ImageMasks_ImageMask_Union_Aspose_Imaging_MagicWand_MagicWandSettings_"></a> Union\(MagicWandSettings\)

Gets the union of the current mask with the result of magic wand selection applied to the source of the mask.

```csharp
public ImageBitMask Union(MagicWandSettings settings)

Parameters

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Exceptions

ArgumentNullException

Thrown when the source image is not defined in mask.

Union(RasterImage, MagicWandSettings)

Gets the union of the current mask with the result of magic wand selection applied to the provided image.

public ImageBitMask Union(RasterImage image, MagicWandSettings settings)

Parameters

image RasterImage

Image for magic wand.

settings MagicWandSettings

Magic wand settings.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

Operators

operator +(ImageMask, ImageMask)

Union of two masks.

public static ImageBitMask operator +(ImageMask a, ImageMask b)

Parameters

a ImageMask

The first mask.

b ImageMask

The second mask.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

operator ^(ImageMask, ImageMask)

Exclusive disjunction of two masks.

public static ImageBitMask operator ^(ImageMask a, ImageMask b)

Parameters

a ImageMask

The first mask.

b ImageMask

The second mask.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

explicit operator ImageGrayscaleMask(ImageMask)

Aspose.Imaging.MagicWand.ImageMasks.ImageGrayscaleMask casting operator.

public static explicit operator ImageGrayscaleMask(ImageMask mask)

Parameters

mask ImageMask

The mask value.

Returns

ImageGrayscaleMask

The new Aspose.Imaging.MagicWand.ImageMasks.ImageGrayscaleMask.

operator !(ImageMask)

Inverts mask.

public static ImageBitMask operator !(ImageMask a)

Parameters

a ImageMask

The mask to be inverted.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

operator *(ImageMask, ImageMask)

Intersection of two masks.

public static ImageBitMask operator *(ImageMask a, ImageMask b)

Parameters

a ImageMask

The first mask.

b ImageMask

The second mask.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.

operator -(ImageMask, ImageMask)

Subtract second mask from first.

public static ImageBitMask operator -(ImageMask a, ImageMask b)

Parameters

a ImageMask

The first mask.

b ImageMask

The second mask.

Returns

ImageBitMask

New Aspose.Imaging.MagicWand.ImageMasks.ImageBitMask.