Sets the alpha level of the image.
Syntax

[C#]

static void SetAlpha(Bitmap bitmap, Nullable<double> alphaValue);
static void SetAlpha(Bitmap bitmap, Bitmap alphaBitmap);

[Visual Basic]

Shared Sub SetAlpha(bitmap As Bitmap, alphaValue As Nullable(Of Double))
Shared Sub SetAlpha(bitmap As Bitmap, alphaBitmap As Bitmap)
Params
Name Description
bitmap The bitmap to which the effect should be applied.
alphaValue A constant alpha opacity value to insert. Zero is fully transparent and 255 is fully opaque. Passing null will result in removal of the alpha channel.
alphaBitmap An image to use as an intensity map. Dark parts are transparent and white ones opaque.
alpha A constant alpha opacity value to insert. Zero is fully transparent and 255 is fully opaque. Passing null will result in removal of the alpha channel.
Notes

This allows you to insert an alpha channel using either a constant value or another image as a matte.

If you pass an image matte and the sizes of the two images do not match the intensity map will be stretched to the correct size.

Passing a value sets the alpha channel to a constant value. However if you intend to use a fully opaque value it is better to remove the alpha channel as this is faster and simpler.

This allows you to insert an alpha channel using either a constant value or another image as a matte.

If you pass an image matte and the sizes of the two images do not match the intensity map will be stretched to the correct size.

Passing a value sets the alpha channel to a constant value. However if you intend to use a fully opaque value it is better to remove the alpha channel as this is faster and simpler.

See Also

Alpha Channels

Alpha Channels

Example

[C#]

using (Bitmap bm = new Bitmap(500, 500))
using (Bitmap mask = (Bitmap)Bitmap.FromFile(Server.MapPath("rez/cloudmask.gif")))
using (Bitmap stamp = new Bitmap(mask.Width, mask.Height)) {
  stamp.SetResolution(mask.HorizontalResolution, mask.VerticalResolution);
  using (Graphics stampGraphics = Graphics.FromImage(stamp)) {
    using (Graphics graphics = Graphics.FromImage(bm)) {
      graphics.Clear(Color.Blue);
      // first draw white cloud;
      stampGraphics.Clear(Color.White);
      Effects.SetAlpha(stamp, mask);
      graphics.DrawImageUnscaled(stamp, new Point(50, 50));
      // ... then light gray cloud;
      stampGraphics.Clear(Color.LightGray);
      Effects.SetAlpha(stamp, mask);
      graphics.DrawImageUnscaled(stamp, new Point(300, 50));
      // ... then dark gray cloud;
      stampGraphics.Clear(Color.Gray);
      Effects.SetAlpha(stamp, mask);
      graphics.DrawImageUnscaled(stamp, new Point(50, 300));
      // ... then black cloud;
      stampGraphics.Clear(Color.Black);
      Effects.SetAlpha(stamp, mask);
      graphics.DrawImageUnscaled(stamp, new Point(300, 300));
    }
  }
  // finally save image;
  bm.Save(Server.MapPath("IG8_Effects_SetAlpha.jpg"));
}


The above draws four solid blocks of color using an alpha channel as a matte. The input and output files are shown below.


cloud.gif


IG8_Effects_SetAlpha.jpg