Welcome Guest! You need to login or register to make posts.



Go to last post Go to first unread
#1 Posted : Sunday, July 20, 2003 9:13:00 PM(UTC)

Rank: Advanced Member

Groups: Member, Administration, Moderator
Joined: 7/28/2003(UTC)
Posts: 1,660

Thanks: 5 times
Was thanked: 76 time(s) in 74 post(s)

Sometimes 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.


Transparency 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.


Nevertheless 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:

[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.

Edited by user Wednesday, October 29, 2008 2:06:56 PM(UTC)  | Reason: Not specified

Best regards,
Fedor Skvortsov
#2 Posted : Tuesday, July 11, 2017 2:07:37 AM(UTC)

Rank: Newbie

Groups: Member
Joined: 7/11/2017(UTC)
Posts: 1

What a helpful and informative post it is. Really all the points are very effective and clear to me. Hope all reader nay get a valuable knowledge from this particular post. The code you have shared with this submit will give more understandings about your thoughts on this topic 'Saving semi-transparent images into file formats which does not support alpha channel'. Likewise today I have accessed reviews of essay writing service, which is also very helpful. I am so impressed with their helpful services.
Users browsing this topic
Forum Jump  
You cannot post new topics in this forum.
You cannot reply to topics in this forum.
You cannot delete your posts in this forum.
You cannot edit your posts in this forum.
You cannot create polls in this forum.
You cannot vote in polls in this forum.