B4J Question Taking screenshot of part of a control

Dominik H

Member
Licensed User
Longtime User
I'm using this code:
B4X:
Dim joR As JavaObject
        Dim wl As Int = MainForm.WindowLeft + 8
        Dim wt As Int = MainForm.WindowTop + 31
          Dim wh As Int = Template.Height
          Dim ww As Int = Template.Width
        joR.InitializeNewInstance("java.awt.Rectangle", Array As Object(wl, wt, ww, wh))
      
        Dim joRobot As JavaObject         
          joRobot.InitializeNewInstance("java.awt.Robot", Null)  
          Dim joF As JavaObject
        joF.InitializeNewInstance("java.io.File", Array As Object(DirUserDefault, ExportText.Text & expType.ToLowerCase))
        Dim joWI As JavaObject
          joWI.InitializeStatic("javax.imageio.ImageIO")
          joWI.RunMethod("write", Array As Object(joRobot.RunMethod("createScreenCapture", Array(joR)), "PNG", joF))

To take snapshot of an area of the control, however this will only work on windows 10. I would have to write different code for this for each platform which would be a pain. Is there any way that I can change the "8" and the "31" to something that will work for all platforms.

Basically the wl and wt start at the window left and top position, but the numbers afterwards are there to cut out eh title bar and the side border.

Using
B4X:
Template.Left
and
B4X:
Template.Top
instead of those 2 numbers doesn't work at all.
 

Dominik H

Member
Licensed User
Longtime User
Why don't you use Node.Snapshot?

You can combine it with Canvas to draw part of a node / control.
The .Snapshot only takes snapshot of one control, I should've mentioned that there is a combined number of controls on that area.

And what do you mean by combine it with a canvas?
 
Upvote 0

Dominik H

Member
Licensed User
Longtime User
You can put all the controls in a Pane and take a snapshot of the pane.

You can use Canvas.DrawImage2 to draw a part of an image.

https://www.b4x.com/b4j/help/jfx.html#canvas

The problem then, as far as I know is when taking a snapshot, it can only be saved as a jpg. Is this true?

EDIT: I answered my own question, it works in jpg, bmp, and png formats. Thanks for the pane snapshot idea, it works flawlessly!
 
Last edited:
Upvote 0
Top