I wrote a program that writes the image of a windows window to a file.
Almost made it. The only problem is that the file has "leftovers" on the right, on the left and below (8 pixels, to be exact).
I realized that this only happens for windows created in B4J (form.show).
For other windows the program works as expected.
I need the image to be exactly the size of the window.
Apparently the two lines below are not returning the size correctly:
Code (It depends on JNA):
Could anyone help help me?
Almost made it. The only problem is that the file has "leftovers" on the right, on the left and below (8 pixels, to be exact).
I realized that this only happens for windows created in B4J (form.show).
For other windows the program works as expected.
I need the image to be exactly the size of the window.
Apparently the two lines below are not returning the size correctly:
B4X:
Dim w As Double = awtrect.GetField("width")
Dim h As Double = awtrect.GetField("height")
Code (It depends on JNA):
B4X:
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#AdditionalJar: jna-5.2.0
#AdditionalJar: jna-platform-5.2.0
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
'MainForm.RootPane.LoadLayout("Layout1") 'Load the layout file.
MainForm.Title = "xy"
MainForm.Show
GetWindow
End Sub
Sub GetWindow()
Dim wu As JavaObject
wu.InitializeStatic("com.sun.jna.platform.WindowUtils")
Dim Ls As List = wu.RunMethod("getAllWindows",Array(True)) ' or False if you want to include windows that are not visible
Dim tit As String
For Each JO As JavaObject In Ls
tit = JO.RunMethod("getTitle",Null)
If tit = "xy" Then
Dim awtrect As JavaObject = JO.RunMethod("getLocAndSize", Null)
Dim x As Double = awtrect.GetField("x")
Dim y As Double = awtrect.GetField("y")
Dim w As Double = awtrect.GetField("width")
Dim h As Double = awtrect.GetField("height")
WriteImg(x, y, w, h)
End If
Next
End Sub
Sub WriteImg(x As Int, y As Int, w As Int, h As Int)
Dim bitmap As B4XBitmap = Capturar(x, y, w, h)
Dim out As OutputStream
out = File.OpenOutput(File.DirApp, "window.png", False)
bitmap.WriteToStream(out, 100, "PNG")
out.Close
End Sub
Sub Capturar(x As Int, y As Int, largura As Int, altura As Int) As B4XBitmap
Dim c3po As AWTRobot
c3po.ScreenCurrentRectangleSetAsArbitrary(x, y, largura, altura)
Return BytesToImage(c3po.ScreenCaptureAsByteArray)
End Sub
Public Sub BytesToImage(bytes() As Byte) As B4XBitmap
Dim In As InputStream
In.InitializeFromBytesArray(bytes, 0, bytes.Length)
Dim bmp As Image
bmp.Initialize2(In)
Return bmp
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
Could anyone help help me?
Last edited: