B4J Question transparent view Snapshot

sz4t4n

Member
Licensed User
Longtime User
Hey,

is there a way to make snapshot of a desktop which is visible on transparten background of the view?

I have a form with pane which is transparent ( i can see my desktop thru it)
upload_2018-6-27_13-27-41.png


But if i make snapshot/snapshot2 of that view i have only white image.
 

Attachments

  • upload_2018-6-27_13-25-52.png
    upload_2018-6-27_13-25-52.png
    55.3 KB · Views: 250

stevel05

Expert
Licensed User
Longtime User
No, because the desktop is not contained within the pane, you can see it through the transparent pane.

You could try finding a screen capture tool that can be run using the shell with an appropriate API that will allow you to set the capture rectangle. I don't know if there is one, have a look at snipping tool if you are using windows as a start.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
After a little digging, it appears that the AWTRobot library supports screen capture of an arbitrary rectangle, which is probably what you want.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Out of curiosity I gave it a try and it works well:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private ImageView1 As ImageView
    Private Image1 As Image
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show

    Dim RBt As AWTRobot
    RBt.ScreenCurrentRectangleSetAsArbitrary(100,100,300,300)
    Dim Bytes() As Byte = RBt.ScreenCaptureAsByteArray
    Dim IStream As InputStream
    IStream.InitializeFromBytesArray(Bytes,0,Bytes.Length)
    Image1.Initialize2(IStream)
    ImageView1.SetImage(Image1)
 
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

The layout file just contains an Imageview.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
If you want to use it without installing the Robot Library you can do this:

B4X:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private ImageView1 As ImageView
    Private Image1 As Image
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("1") 'Load the layout file.
    MainForm.Show
    Dim ScreenCap As JavaObject
    ScreenCap.InitializeNewInstance("java.awt.Robot",Null)
  
    Dim Rectangle As Object = NewRectangle(100,100,300,300)
    Dim Bimg As Object = ScreenCap.RunMethod("createScreenCapture",Array(Rectangle))
    Dim Bytes() As Byte = Bimg2Bytes(Bimg)
  
    'Convert the bytes array to an image
    Dim IStream As InputStream
    IStream.InitializeFromBytesArray(Bytes,0,Bytes.Length)
    Image1.Initialize2(IStream)
    'Set the image to an ImageView
    ImageView1.SetImage(Image1)

End Sub

Private Sub NewRectangle(X As Int, Y As Int, Width As Int, Height As Int) As Object
    Dim R As JavaObject
    R.InitializeNewInstance("java.awt.Rectangle",Array(X,Y,Width,Height))
    Return R
End Sub

Private Sub Bimg2Bytes(Bimg As Object) As Byte()
    Dim BAOS As JavaObject
    BAOS.InitializeNewInstance("java.io.ByteArrayOutputStream",Null)
    Dim ImageIO As JavaObject
    ImageIO.InitializeStatic("javax.imageio.ImageIO")
    ImageIO.RunMethod("write",Array(Bimg,"png",BAOS))
    Return BAOS.RunMethod("toByteArray",Null)
End Sub

'Return true to allow the default exceptions handler to handle the uncaught exception.
Sub Application_Error (Error As Exception, StackTrace As String) As Boolean
    Return True
End Sub

Requires the JavaObject Library
 
Upvote 0
Top