Bug? Canvas Snapshot and transparency

Jim Brown

Active Member
Licensed User
Longtime User
Hi Erel,
I don't really know if I should class this as a bug. I want to snapshot a canvas and retain the alpha/transparent background. Unfortunately I can't seem to capture the transparency from the Snapshot option

Here's an example. The canvas on the left just demonstrates that the image (balloons) has a transparent background. If I draw the balloons (half size) to a new canvas and snapshot it, then draw the results over to the right side canvas you can seen the balloons background is now a solid white.

BEFORE AND AFTER pressing "Snapshot"
can_snap_zps9d2de0b5.png


I have attached a B4J demonstration sample. I have tried filling the new canvas with a transparent rect and also using the style "-fx-background-color: transparent" but no luck.

Thanks
 

Attachments

  • CanvasSnapshot.zip
    70.8 KB · Views: 344

Daestrum

Expert
Licensed User
Longtime User
Not sure if this will help, but was playing around when writing animation library, and had this code.
It doesn't use the B4J node snapshot function.
B4X:
    g.InitializeNewInstance("javafx.scene.SnapshotParameters",Null) ' javaobject
    Dim p As Paint = fx.Colors.Transparent
    g.RunMethod("setFill",Array As Object(p))
    Dim jot As JavaObject
    jot = imageview1 ' contains png with trans background
    im = jot.RunMethod("snapshot",Array As Object(g,Null)) ' im = Image
    Log("wdth: "& im.Width & "ht: "& im.Height)
    canvas1.DrawImage(im,0,0,100,100) ' canvas1 = canvas

And the canvas had Blend Mode set to SRC_OVER

Just tried your code with a few modifications and it gives what you were after.
All I changed was in your button sub.
B4X:
Sub btn_Snapshot_Action
    Dim c As Canvas
    c.Initialize("")
    c.Width=Balloons.Width/2 : c.Height=Balloons.Height/2
    c.Style="-fx-background-color: transparent"
    c.DrawRect(0,0,c.Width,c.Height,fx.Colors.Transparent,True,0)
    c.DrawImage(Balloons,0,0,Balloons.Width/2,Balloons.Height/2)
'    Dim img As Image = c.Snapshot
'    can2.DrawImage(checkeredImage,0,0,pane1.PrefWidth,pane1.PrefHeight)
'    can2.DrawImage(img,20,20,img.Width,img.Height)
    Dim g As JavaObject
    g.InitializeNewInstance("javafx.scene.SnapshotParameters",Null) ' javaobject
    Dim p As Paint = fx.Colors.Transparent
    g.RunMethod("setFill",Array As Object(p))
    Dim jot As JavaObject
    jot = c ' contains png with trans background
    Dim im As Image
    im = jot.RunMethod("snapshot",Array As Object(g,Null)) ' im = Image
    Log("wdth: "& im.Width & "ht: "& im.Height)
    can2.DrawImage(im,0,0,im.Width/2,im.Height/2) ' canvas1 = canvas
End Sub
Will leave it to you to make the code nicer ;-)
 
Last edited:

Jim Brown

Active Member
Licensed User
Longtime User
Hi Daestrum,
Thanks for taking the time out address my issue. Your solution is perfect.
 

hookshy

Well-Known Member
Licensed User
Longtime User
the below statement does not have any effect on the background transparency
just take snapshot using java object as described by Daestrum

B4X:
  c.Style="-fx-background-color: transparent"
c.DrawRect(0,0,c.Width,c.Height,fx.Colors.Transparent,True,0)

B4X:
Sub snapshot(c As Canvas) As Image

  Dim g As JavaObject
    g.InitializeNewInstance("javafx.scene.SnapshotParameters",Null) ' javaobject
    Dim p As Paint = fx.Colors.Transparent
    g.RunMethod("setFill",Array As Object(p))
   
    Dim jot As JavaObject
    jot = c ' contains png with trans background
   
    Return jot.RunMethod("snapshot",Array As Object(g,Null))

End Sub
 
Last edited:

sorex

Expert
Licensed User
Longtime User
I bumped at the same issue.

Thanks @Daestrum & @hookshy for the fix.

@Erel : Is this a bug in your core libs or a limitation of the default java core?

It would be nice if the above sub was added to the core libs but as it requires the javaobjects lib I guess that's hard to do.
 
Top