DescriptionSometimes we need to work with images which have transparent areas (as transparent background). For example, some effects such as shadow or glow should have transparent area around the objects to mark its bounds. When we applied such an effect we must save the image into file. However suddenly the problem arises: after saving our shadow looks very ugly:

instead of

The same situation occurs when we draw antialiased figures or text on transparent background.
ReasonTransparency is stored as so-called alpha channel - each pixel stores its opacity in addition to red, green, and blue component. But most of image formats does not support transparency information (it store only RGB triple instead of ARGB quad). So, for example when you save to JPEG, encoder discards alpha values and writes only color. What happens with our shadow? As it has only one color and all the hues of shadow are stored as alpha values, the output file contains solid gray color instead of gradient.
SolutionNevertheless we can workaround the limitation of these formats. When we save file, we should set the color which will be considered as background (for example white). In this case all the semi-transparent areas will be blended with this color before encoding. The shadow will be correct in this case.
To specify background color, you should use
BackgroundColor option in
EncoderOptions property of the
RasterObject:
Code:[VB Script]
<!-- METADATA TYPE="typelib" UUID="{9AAC3E0E-8B1D-4D3A-9EF9-465BA8D31B12}"-->
<%
Option Explicit
Dim objRaster
Set objRaster = Server.CreateObject("GraphicsProcessor2002.RasterObject")
' Load image with transparent areas
objRaster.LoadFile Server.MapPath("source.png")
' Apply shadow
objRaster.Shadow 5,5
objRaster.EncoderOptions("BackgroundColor") = gpWhite
objRaster.SaveStream Response
objRaster.SaveFile Server.MapPath("result.jpg")
Set objRaster = Nothing
%>
So when you assume that the image contains transparent areas you should provide background color which will replace the transparent background. However if your image is fully opaque, avoid assigning BackgroundColor option in order to reduce CPU burden during image encoding.
Best regards,
Fedor Skvortsov