I have an old Method to convert image to a certain transparency (create disabled button with canvas) and i tried to convert it to API2. My problem, is the original 100% transparency turn black for PNG images… any ideas ?
Heres the old code :
opacity = (1.0 - opacity)
dim x,y as integer
dim thePic as picture
dim c,white as color
white = Color.rgb(255, 255, 255)
if not base.HasAlphaChannel then base.transparent = 1
thePic = new Picture(base.width, base.height,32)
thePic.Graphics.drawpicture(base, 0, 0)
for x = 0 to thePic.width - 1
for y = 0 to thePic.height -1
c = thepic.graphics.pixel(x,y)
if c <> Color.rgb(255,255,255) then
thepic.mask.graphics.pixel(x,y) = Color.rgb(opacity*255, opacity*255, opacity*255)
else
thepic.mask.graphics.pixel(x,y) = Color.rgb(255,255,255)
end if
next
next
if not thePic.HasAlphaChannel then thePic.transparent = 1
return thePic
and here what i think should work … but not…
opacity = 1.0 - opacity // convert to alpha scale like old mask code
Var thePic As New Picture(base.Width, base.Height) // 32bpp w/ alpha
thePic.Graphics.DrawPicture(base, 0.0, 0.0)
Var surf As RGBSurface = thePic.RGBSurface
If surf <> Nil Then
Var white As Color = Color.White
Var a As Integer = CType(opacity * 255.0, Integer) // truncates (0.73→186)
If a < 0 Then a = 0
If a > 255 Then a = 255
For y As Integer = 0 To thePic.Height - 1
For x As Integer = 0 To thePic.Width - 1
Var c As Color = surf.Pixel(x, y)
If c <> white Then
// apply requested opacity to non-white pixels (0=opaque, 255=transparent)
surf.Pixel(x, y) = Color.RGB(c.Red, c.Green, c.Blue, a)
Else
// make white fully transparent (like old mask white)
surf.Pixel(x, y) = Color.RGB(c.Red, c.Green, c.Blue, 255)
End If
Next
Next
End If
Return thePic
any suggestions ?