Alpha Channel

Pixels of the image can store information about their opacity along with color value. This allows creating images with transparent or semi-transparent areas. It is very useful when you are creating such imagery as non-rectangular logos or watermarks which you will put on other images. There are two ways to specify the transparency of a pixel - alpha channel and color key.

Color Keys

Color key is a color which is interpreted as transparent when displaying or blending. This technique is widely used in DirectX programming; however, it is not used by Graphics Mill which uses alpha blending instead. Graphics Mill uses color key for saving an indexed image into a format which supports transparency only in color key mode (such as GIF).

The main disadvantage of this method is that it cannot represent semitransparency. That's why colorkeying cannot produce smooth blend on the edge of an opaque or transparent area. Another problem with this approach is that if you appoint a color to be transparent, you cannot display that color on the image. It will be replaced by transparent pixels.

Due to these disadvantages Graphics Mill uses another method: alpha blending.

Alpha Channel

Instead of making a color transparent, each pixel stores information on how opaque it is. This opacity data is called alpha channel and is typically stored after the color channels of the pixel. The most wide-spread alpha-compliant pixel format is Format32bppArgb. Each channel (including the alpha channel) occupies one byte. The alpha channel is stored in the high (fourth) byte.

When alpha = 255 (maximum value for this pixel format), the color is treated as completely opaque, when alpha = 0, the color is completely transparent (i.e. when blending this bitmap with another one, background color is not changed). For alpha values between 0 and 255, color is interpreted as semi-transparent and during blending each channel of the resulting color is calculated using the following formula:

result = (foreground*alpha + background*(max-alpha))/max

where max is 255 for 8 bit per channel pixel format, and 65355 for 16 bits per channel (extended) pixel formats.

Working with Alpha Channel in Graphics Mill

  • Check if alpha channel is supported. Not all the pixel formats support alpha channel. If you need to check whether the current pixel format of the Bitmap class instance supports alpha channel, use HasAlpha property. If it returns true, alpha channel is available, otherwise - it is not.
  • Add alpha channel to the bitmap. To add alpha channel to the image, use the ChannelsProvider.SetAlpha(Single) method. Alternatively you can convert the bitmap to the necessary pixel format (e.g. Format32bppArgb) using ColorManagementProvider.Convert methods.
  • Remove alpha channel from the bitmap. If you need to remove alpha channel, use the RemoveAlpha() method. You can also convert the bitmap to a certain pixel format (e.g. Format24bppRgb) using ColorManagementProvider.Convert methods. Pay attention, when you discard alpha channel, you can either drop alpha without affecting color channels of the pixels, or flatten it by alpha blending each pixel with the specified color.

    For example, let's assume we have an image of an ellipse with antialiased edges. Antialiasing is provided with variable alpha channel. If we just discard the alpha channel, we will lose the antialiasing:

    Ellipse without antialiasing.

    To get the correct result, we should blend each pixel with some background color, e.g. white. For example, you can do it by passing color into ChannelsProvider.RemoveAlpha(Color) method. You will get the following result:

    Ellipse with antialiasing.
  • Set a color to be transparent. You can emulate colorkeying by using the ChannelsProvider.Transparentize(Color, Single) method. You can pass the color you want to make transparent into this method, and it recalculates alpha channel accordingly. Also, this method has the second parameter - tolerance. Tolerance is used to specify the "distance" between the colors to be interpreted as one. For example, RGB colors 0x000000 and 0x000001 look almost the same and you would expect the latter to transparentize too. But be careful with specifying high values of tolerance: in this case you may transparentize the wrong colors.
  • Blend two bitmaps. To blend two bitmaps, you can use Draw and DrawOn methods of the Bitmap class with combine mode argument = Alpha. You can also specify overall opacity of the source bitmap.

    Alternatively you can use special transform class Combiner. It also has the Mode property which work in the same way as the corresponding argument of the Draw method. In fact, this class is used inside the Draw method.