LEADTOOLS (Leadtools assembly) Send comments on this topic. | Back to Introduction - All Topics | Help Version 17.0.3.29
CreateMaskFromRegion Method
See Also 
Leadtools Namespace > RasterImage Class : CreateMaskFromRegion Method



Creates a 1-bit mask image from the region that is defined in the image. Supported in Silverlight, Windows Phone 7

Syntax

Visual Basic (Declaration) 
Public Function CreateMaskFromRegion() As RasterImage
Visual Basic (Usage)Copy Code
Dim instance As RasterImage
Dim value As RasterImage
 
value = instance.CreateMaskFromRegion()
C# 
public RasterImage CreateMaskFromRegion()
C++/CLI 
public:
RasterImage^ CreateMaskFromRegion(); 

Return Value

The newly created RasterImage which will be updated with a 1-bit, black-and-white image, where pixels from the region are white, and all others are black.

Example

This example will load an image, sets a color region using black, and then creates a 1-bit mask image from that image and saves it to disk.

Visual BasicCopy Code
Public Sub CreateMaskFromRegionExample()
      Dim codecs As RasterCodecs = New RasterCodecs()

      Dim srcFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp")
      Dim destFileName As String = Path.Combine(LEAD_VARS.ImagesDir, "Image1_CreateMaskFromRegion.bmp")

      ' Load the image
      Dim image As RasterImage = codecs.Load(srcFileName)

      ' Posterize the image to decrease the number of colors
      Dim posterize As PosterizeCommand = New PosterizeCommand(16)
      posterize.Run(image)

      ' Specify a pixel in the upper left of the displayed image
      Dim pt As LeadPoint = New LeadPoint(image.ImageWidth \ 8, image.ImageHeight \ 8)

      ' Adjust the point in case the view perspective is not TopLeft
      pt = image.PointToImage(RasterViewPerspective.TopLeft, pt)

      ' Get the color of the specified pixel
      Dim regionColor As RasterColor = image.GetPixelColor(pt.Y, pt.X)

      ' Create a region that includes all pixels of that color
      image.AddColorToRegion(regionColor, RasterRegionCombineMode.Set)

      Dim maskImage As RasterImage = image.CreateMaskFromRegion()

      codecs.Save(maskImage, destFileName, RasterImageFormat.Bmp, 1)

      maskImage.Dispose()
      image.Dispose()
      codecs.Dispose()
   End Sub

Public NotInheritable Class LEAD_VARS
   Public Const ImagesDir As String = "C:\Users\Public\Documents\LEADTOOLS Images"
End Class
C#Copy Code
public void CreateMaskFromRegionExample()
   {
      RasterCodecs codecs = new RasterCodecs();

      string srcFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1.cmp");
      string destFileName = Path.Combine(LEAD_VARS.ImagesDir, "Image1_CreateMaskFromRegion.bmp");

      // Load the image
      RasterImage image = codecs.Load(srcFileName);

      // Posterize the image to decrease the number of colors
      PosterizeCommand posterize = new PosterizeCommand(16);
      posterize.Run(image);

      // Specify a pixel in the upper left of the displayed image
      LeadPoint pt = new LeadPoint(image.ImageWidth / 8, image.ImageHeight / 8);

      // Adjust the point in case the view perspective is not TopLeft
      pt = image.PointToImage(RasterViewPerspective.TopLeft, pt);

      // Get the color of the specified pixel
      RasterColor regionColor = image.GetPixelColor(pt.Y, pt.X);

      // Create a region that includes all pixels of that color
      image.AddColorToRegion(regionColor, RasterRegionCombineMode.Set);

      RasterImage maskImage = image.CreateMaskFromRegion();

      codecs.Save(maskImage, destFileName, RasterImageFormat.Bmp, 1);

      maskImage.Dispose();
      image.Dispose();
      codecs.Dispose();
   }

static class LEAD_VARS
{
   public const string ImagesDir = @"C:\Users\Public\Documents\LEADTOOLS Images";
}
SilverlightCSharpCopy Code
public void CreateMaskFromRegionExample(RasterImage image, Stream destStream)
{
   // Posterize the image to decrease the number of colors
   PosterizeCommand posterize = new PosterizeCommand(16);
   posterize.Run(image);
   // Specify a pixel in the upper left of the displayed image
   LeadPoint pt = new LeadPoint(image.ImageWidth / 8, image.ImageHeight / 8);

   // Adjust the point in case the view perspective is not TopLeft
   pt = image.PointToImage(RasterViewPerspective.TopLeft, pt);

   // Get the color of the specified pixel
   RasterColor regionColor = image.GetPixelColor(pt.Y, pt.X);

   // Create a region that includes all pixels of that color
   image.AddColorToRegion(regionColor, RasterRegionCombineMode.Set);

   RasterImage maskImage = image.CreateMaskFromRegion();

   RasterCodecs codecs = new RasterCodecs();
   codecs.Save(maskImage, destStream, RasterImageFormat.Png, 0);

   maskImage.Dispose();
   image.Dispose();
}
SilverlightVBCopy Code
Public Sub CreateMaskFromRegionExample(ByVal image As RasterImage, ByVal destStream As Stream)
   ' Posterize the image to decrease the number of colors
   Dim posterize As PosterizeCommand = New PosterizeCommand(16)
   posterize.Run(image)
   ' Specify a pixel in the upper left of the displayed image
   Dim pt As LeadPoint = New LeadPoint(image.ImageWidth / 8, image.ImageHeight / 8)

   ' Adjust the point in case the view perspective is not TopLeft
   pt = image.PointToImage(RasterViewPerspective.TopLeft, pt)

   ' Get the color of the specified pixel
   Dim regionColor As RasterColor = image.GetPixelColor(pt.Y, pt.X)

   ' Create a region that includes all pixels of that color
   image.AddColorToRegion(regionColor, RasterRegionCombineMode.Set)

   Dim maskImage As RasterImage = image.CreateMaskFromRegion()

   Dim codecs As RasterCodecs = New RasterCodecs()
   codecs.Save(maskImage, destStream, RasterImageFormat.Png, 0)

   maskImage.Dispose()
   image.Dispose()
End Sub

Remarks

For more information, refer to Implementing Transparency.

For more information, refer to Creating a Region.

For more information, refer to Saving A Region.

For more information, refer to Working with the Existing Region.

Requirements

Target Platforms: Silverlight, Windows XP, Windows Server 2003 family, Windows Server 2008 family, Windows Vista, Windows 7, MAC OS/X (Intel Only), Windows Phone 7

See Also