Alpha Channel
Pixels of the image can store information about their opacity along with color value. It
allows to create 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, Graphics Mill for .NET does not use it (it uses alpha blending instead). It is
used only when Graphics Mill for .NET saves 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 opaque and transparent area. Another
problem of this approach is that if you appoint some color to be transparent, you cannot
display this color on this image. It will be replaced by transparent pixels.
Due to these disadvantages Graphics Mill for .NET uses another
method: alpha blending.
Alpha Channel
Instead of making some color transparent, each pixel stores information on how opaque is it.
This opacity data is called alpha channel and is typically stored after color channels
of the pixel. The most wide-spread alpha-compliant pixel format is
Format32bppArgb. Each channel (including alpha channel) occupies one byte.
Alpha channel is stored in the high (fourth) byte.
When alpha = 255 (max 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 for .NET
-
Check if alpha channel is supported. Not all the pixel formats support alpha
channel. If you need to check whether 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
AddAlpha(Single) method of the
ChannelsProvider returned by the
Bitmap. Alternatively you can convert
bitmap to a necessary pixel format (e.g. Format32bppArgb) using Convert(PixelFormat) or ConvertToContinuous(ColorSpace, Boolean, Boolean) method
of the ColorManagementProvider of the
Bitmap.
-
Remove alpha channel from the bitmap. If you need to remove alpha channel,
ChannelsProvider has a DiscardAlpha method. You can also
convert bitmap to a certain pixel format (e.g. Format24bppRgb) using
Convert(PixelFormat) or
ConvertToContinuous(ColorSpace, Boolean, Boolean) method
of the ColorManagementProvider returned
through the Bitmap.ColorManagement
property. 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 ellipse with antialiased edges. Antialiasing
is provided with variable alpha channel. If we just discard the alpha channel, we will lose
the antialiasing:
To get correct result, we should blend each pixel with some background color, e.g. white.
For example, you can do it by passing color into DiscardAlpha method. You will get
the following result:
-
Set some color transparent. You can emulate colorkeying by using Transparentize(Color, Single) method of the
ChannelsProvider returned by the
Bitmap. 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 it may transparentize
wrong colors.
-
Blend two bitmaps. To blend two bitmaps, you can use the Draw method 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 CombineMode and Opacity properties which work
in the same way as the corresponding arguments of the Draw method. In fact, this class is used
inside the Draw method.