Alaska Software Inc. - Showing transparent image
Username: Password:
AuthorTopic: Showing transparent image
Otto TrappShowing transparent image
on Tue, 29 Mar 2016 17:50:03 +0200
Hello,

I'd like to display a .png image in a window with the window's 
background color where it is transparent. The image has transparent 
background, it was exported from a Gimp .xcf image to png. When I open 
the png file from Gimp or from Irfanview the image shows nice, either 
the transparent background is shown (Gimp) or the transparent background 
parts are smoothly replaced with the background color of the window 
(Irfanview).

My question is, what should I modify in the attached sample to display 
this .png image so that its transparent background color is changed 
smoothly to the window's (or more precisely the static's) background 
color the image is shown in (on).

Thanks.

Regards,
Otto


logo.zip
Jim LeeRe: Showing transparent image
on Tue, 29 Mar 2016 22:15:49 +0200
hi,

try this

   oBmp:=XbpBitmap():new():create()
   oBmp:loadFile("logo.png")
oBmp:transparentClr := oBmp:getDefaultBGColor()

Jim
Otto TrappRe: Showing transparent image
on Wed, 30 Mar 2016 10:55:17 +0200
Hello Jim,

> try this
>
>  oBmp:=XbpBitmap():new():create()
>  oBmp:loadFile("logo.png")
>  oBmp:transparentClr := oBmp:getDefaultBGColor()

It helped me to change the background color of the image when I compiled 
the project with Xbase++ 1.9. As far as I can see here every pixel that 
has "transparent color" in the picture is replaced by a color given by 
oBmp:transparentClr while it is being drawn. However, the result is 
quite rough (see the first attachment).

What I've also found that Xbase++ 2.0 now supports transparency of 
images in a more elaborate way. I built my sample with 2.0 without 
changes and it displayed the image very smoothly. It not only replaced 
the fully transparent pixels with the static's background color but also 
applied transparency to all other pixels achieving quite a smooth 
display (see the second attachment).

(After I read the docs at XbpBitmap() and srutinized my logo.png with 
Gimp closely I guess that it works the following way. In my image every 
pixel has transparency information additionally to its color value. The 
inner parts of the letters are opaque but towards the egdes of the 
letters pixels' transparency grows. So when Xbase 2.0 (or Irfanview, 
etc...) draws the picture it calculetes the actual color of the pixels 
from its original color, its level of transparency and the background 
color of the static it is drawn on.)

Jim, thank you for the tip! And thanks to Alaska Team for giving support 
to transparency now in a sophisticated way.

Regards,
Otto


xbase19.jpg
xbase20.jpg
Thomas BraunRe: Showing transparent image
on Wed, 30 Mar 2016 12:34:03 +0200
Otto Trapp wrote:

> So when Xbase 2.0 (or Irfanview, 
> etc...) draws the picture it calculetes the actual color of the pixels 
> from its original color, its level of transparency and the background 
> color of the static it is drawn on.)

Technically, this is not just "transparency", but rather "alpha blending"
or "alpha compositing" = using the information of the alpha channel of the
picture to calculate the actual color values.

also see https://en.wikipedia.org/wiki/Alpha_compositing
https://en.wikipedia.org/wiki/Transparency_%28graphic%29

hth
Thomas