Documents for Imaging .NET Edition
Features / Apply Transparency Mask
In This Topic
    Apply Transparency Mask
    In This Topic

    Transparency masks are used in imaging to hide some portion of the image while retaining rest of the image. The mask is either an image that already has transparency set on it or it is a bilevel/grayscale image which can serve the purpose because in that case, the black or white pixels are used as a mask. 

    In GcImaging, the transparency mask can be defined using BilevelBitmap or GrayscaleBitmap class. The image to be used as a transparency mask is loaded in a GcBitmap instance and converted to a BilevelBitmap or GrayscaleBitmap by using the ToBilevelBitmap or ToGrayscaleBitmap methods of the GcBitmap class. To use the defined mask, you need to draw the image on which the mask is to be applied on the target GcBitmap and then apply a mask using the ApplyTransparencyMask method of the GcBitmap class.

    Base Image Mask
    A beautiful house A black lizard
    Output Image
    Transparency mask applied on the house with the lizard's image

    Set Transparency

    To set the transparency mask:

    1. Initialize an instance of the GcBitmap class to load the semi-transparent image which is to be applied as a mask.
    2. Convert this GcBitmap to GrayscaleBitmap which will be used as the image mask, using the ToGrayscaleBitmap method of the GcBitmap class.
    3. Initialize another instance of the GcBitmap class to load the image on which the transparency mask is to be applied.
    4. Apply the transparency mask to the result bitmap using the ApplyTransparencyMask method of the GcBitmap class.
    5. Convert the result bitmap to an opaque image with specified background color using the ConvertToOpaque method of the GcBitmap class.
      C#
      Copy Code
      //Initialize bitmap for generating mask image
      GcBitmap mask = new GcBitmap("logo.png");
      
      //Draw image to which the tranparency mask has to be applied
      GcBitmap bmp = new GcBitmap("tudor.jpg");
      
      //Define the transparency mask using mask image
      GrayscaleBitmap grayscaleMask = mask.ToGrayscaleBitmap(ColorChannel.Blue, true);
      
      //Apply the transparency mask to the result bitmap
      bmp.ApplyTransparencyMask(grayscaleMask);
      
      //Convert the result bitmap to opaque
      bmp.ConvertToOpaque(Color.Beige);
      
      //Save the result bitmap to save transparent image
      bmp.SaveAsJpeg("TransparentImg.jpg");
      

    Back to Top

    Set Background Color for Semi-transparent Images

    To set the background color:

    1. Initialize an instance of the GcBitmap class and load an image in the GcBitmap object.
    2. Check whether the image contains transparent or semi-transparent pixels using HasTransparentPixels method of the GcBitmap class.
    3. Convert the image with transparent or semi-transparent pixels to a fully opaque image with specified background color using the ConvertToOpaque method of the GcBitmap class.
      C#
      Copy Code
      //Initialize GcBitmap by passing the image path
      var imgPath = Path.Combine("Tranparent1.png");
      GcBitmap origBmp = new GcBitmap(imgPath);
      
      //Check for transarent pixels and convert to opaque
      if (origBmp.HasTransparentPixels())
      {
          origBmp.ConvertToOpaque(Color.LightBlue);
          origBmp.SaveAsJpeg("NoTransparent.jpg");
      }
      else
          Console.WriteLine("No transparent pixels");
      

    Back to Top