AspJpeg enables you to set a new alpha channel for the image, and also remove an existing one,
via the methods SetAlpha and RemoveAlpha, respectively.
It also allows you to modify the alpha channel in several ways.
10.3.1 SetAlpha and RemoveAlpha Methods
The SetAlpha method expects two arguments: an instance of the ASPJpeg object
representing the alpha channel, and a Boolean flag specifying whether the alpha values should be inversed.
The image representing the alpha channel must have the same dimensions as the image to which it is
assigned, and be in grayscale colorspace (i.e. 1 byte per pixel.)
If the 2nd argument is False, the color value of 0 (black) of the alpha channel image
corresponds to full transparency, and the color value of 255 (white) to full opacity. If the 2nd
argument is True, 255 corresponds to full transparency and 0 to full opacity.
For example, the following code snippet
creates a red image with a fully transparent round hole by using another image with a filled
circle as the alpha channel:
VB Script:
Set Jpeg = Server.CreateObject("Persits.Jpeg")
Jpeg.New 100, 100, &HFFFF0000 ' red background
Set Alpha = Server.CreateObject("Persits.Jpeg")
Alpha.New 100, 100, &HFFFFFFFF
Alpha.Canvas.DrawCircle 50, 50, 30
Alpha.ToGrayscale (0)
Jpeg.SetAlpha Alpha, False
Jpeg.PNGOutput = True
Jpeg.Save "c:\path\out.png"
|
C#:
IASPJpeg objJpeg = new ASPJpeg();
IASPJpeg objAlpha = new ASPJpeg();
objJpeg.New( 100, 100, 0xFFFF0000 );
objAlpha.New( 100, 100, 0xFFFFFFFF );
objAlpha.Canvas.DrawCircle( 50, 50, 30 );
objAlpha.ToGrayscale(0);
objJpeg.SetAlpha( (ASPJpeg)objAlpha, false );
objJpeg.PNGOutput = 1;
objJpeg.SaveUnique( @"c:\path\out.png" );
|
Here is the output image shown on a blue background to make its transparency more obvious:
If the 2nd argument to SetAlpha is changed to True, the output will be as follows:
The alpha channel image must be in the grayscale color space. That is why the script above calls
the method ToGrayscale (introduced in Version 2.1 also.)
Failure to call this method would result in an exception since images created with the New
method are always RGB.
The SetAlpha method also allows the 1st argument to be Nothing (null.)
In that case, the alpha channel will be set evenly to 255 (full opacity) for all pixels
if the 2nd argument is False, and to 0 (full transparency) if True.
To remove an existing alpha channel, call RemoveAlpha. This method
has no arguments. If the current image has no alpha channel, calling this method has no effect.
To check if the current image has an alpha channel, use the Boolean property AlphaExists.
10.3.2 AlphaPixels Property
You can read and change the individual pixels of the current alpha channel via the
parameterized property AlphaPixels, for example:
val = Jpeg.AlphaPixels(10, 20)
or
Jpeg.AlphaPixels(10, 20) = 20
The property sets and returns values in the range 0 to 255.
10.3.3 Drawing on Images with Alpha Channel
Various Canvas methods such as DrawLine or PrintTextEx can be used on an image
whether it has an alpha channel or not. However, if the graphics or text being
drawn falls on a fully transparent area of the image, it will not be visible on the resultant
PNG.
To ensure the 100%-visibility of the text or graphics being drawn, the alpha channel has to be modified also to provide
an opaque background for the drawing.
The same text or graphics needs to be drawn separately on the alpha channel in white color (which
designates full opacity).
The method AlphaToImage turns the alpha channel of an image into an independent image
with a Canvas object of its own, which can be drawn on.
The following code snippet opens a PNG image with an alpha channel,
separates its alpha channel into a separate ASPJpeg object, draws some text on the original image
and the alpha channel image, and merges the modified alpha channel with the original image,
thus ensuring that the text is fully visible on the resultant PNG.
VB Script:
Set Jpeg = Server.CreateObject("Persits.Jpeg")
Jpeg.Open Server.MapPath("../images/realty.png")
Font = Jpeg.WindowsDirectory & "\Fonts\Arial.ttf"
' Print on image
Jpeg.Canvas.PrintTextEx "Some text", 50, 20, Font
' Print on alpha channel by creating a separate object
Set Alpha = Server.CreateObject("Persits.Jpeg")
Alpha.Open Server.MapPath("../images/realty.png")
Alpha.AlphaToImage ' populate image with alpha channel
Alpha.ToRGB ' make it RGB so that we can print on it
Alpha.Canvas.Font.Color = &HFFFFFFFF ' white color!
Alpha.Canvas.PrintTextEx "Some text", 50, 20, Font
Alpha.ToGrayscale(1) ' turn back into grayscale
Jpeg.SetAlpha Alpha, False
' Output as PNG
Jpeg.PNGOutput = True
' Save
Jpeg.Save Server.MapPath("realty_small.png")
|
C#:
IASPJpeg objJpeg = new ASPJpeg();
string strFont = objJpeg.WindowsDirectory + @"\Fonts\Arial.ttf";
objJpeg.Open( Server.MapPath("../images/realty.png") );
// Print on image
objJpeg.Canvas.PrintTextEx( "Some text", 50, 20, strFont );
// Print on alpha channel by creating a separate object
IASPJpeg objAlpha = new ASPJpeg();
objAlpha.Open( Server.MapPath("../images/realty.png") );
objAlpha.AlphaToImage(); // populate image with alpha channel
objAlpha.ToRGB(); // make it RGB so that we can print on it
objAlpha.Canvas.Font.Color = 0xFFFFFF; // white color!
objAlpha.Canvas.PrintTextEx( "Some text", 50, 20, strFont );
objAlpha.ToGrayscale(1); // turn back into grayscale
objJpeg.SetAlpha( (ASPJpeg)objAlpha, false );
// Output as PNG
objJpeg.PNGOutput = 1;
// Save
objJpeg.Save( Server.MapPath("realty_small.png") );
|
Click the links below to run this code sample:
http://localhost/aspjpeg/manual_10/10_alpha.asp
http://localhost/aspjpeg/manual_10/10_alpha.aspx
10.3.4 Alpha Channel Flattening
As of Version 2.7, AspJpeg offers the method FlattenAlpha which applies
the image's alpha channel to its pixels in combination with the specified background
color and then removes the alpha channel, thus effectively "flattening" the image.
The method has the same effect as drawing the image on top of a monochrome background of equal size.
The FlattenAlpha method expects a single argument, the background
color in the form of a long integer.
If the image has no alpha channel, this method has no effect.
This method is useful for converting arbitrary PNG images (regardless
of their alpha channel presence) to JPEG format.
For example:
Jpeg.FlattenAlpha( &HFFFFFFFF ) ' White background
10.3.5 Alpha Channel for GIF Images
As of Version 2.7.0.6, AspJpeg treats GIF images with a transparency as if they were PNG images with an alpha channel.
That is, when Jpeg.Open/OpenBinary is called on a GIF image which contains a transparency color,
the property Jpeg.AlphaExists returns True and all alpha-related methods described above are available.
This makes it easy to convert a GIF image with a transparency to a PNG image with an alpha channel and also
perform cropping, rotation and other operations on a GIF image while preserving its transparency information.