B4J Question snapshot.writetostream halt

le_toubib

Active Member
Licensed User
Longtime User
hi all
i created 2 panes :
1- pane1 is brought to front and contains a "button1" , this button takes a snapshot of pane2
2- pane2 is behind

B4X:
sub Button1_Click
        Dim fn As String =  "filename" & "_" & DateTime.Date(DateTime.Now) & ".jpg"
        Dim Out As OutputStream = File.OpenOutput(File.Dirdata("appname") & "\PDFs\"  , fn ,False)
        pane2.Snapshot.WriteToStream(Out)
        Out.Flush
        Out.Close
end sub

snapshot is taken correctly and saved , but after that i cannot access button1 .. it is still on top and enabled but "unclickable" , actually all other controls on pane1 are inaccessible.
p.s : removing the highlighted line : ( pane2.Snapshot.WriteToStream(Out)) would make pane1 accessible again
your help is greatly appreciated
 

teddybear

Well-Known Member
Licensed User
The code looks right. you'd better post a small project.
By your description,it seems to be stuck, did you open the pic to see?or can you delete the pic?
 
Upvote 0

PaulMeuris

Active Member
Licensed User
The filename contains a formatted date as in: filename_10/04/2023.jpg
B4X:
Pane2.Snapshot.WriteToStream(Out)
This code expects a "PNG" format.
Here's how i would do it:
B4X:
    xui.SetDataFolder("testenvironment42")
    Dim bmp As B4XBitmap = Pane2.Snapshot
    Dim fn As String =  "filename" & "_" & DateTime.Now & ".jpg"
    Dim Out As OutputStream
    Out = File.OpenOutput(xui.DefaultFolder, fn, False)
    bmp.WriteToStream(Out, 100, "JPEG")
    Out.Flush
    Out.Close
The Button1 on Pane1 remains clickable.
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
The filename contains a formatted date as in: filename_10/04/2023.jpg
B4X:
Pane2.Snapshot.WriteToStream(Out)
This code expects a "PNG" format.
Here's how i would do it:
B4X:
    xui.SetDataFolder("testenvironment42")
    Dim bmp As B4XBitmap = Pane2.Snapshot
    Dim fn As String =  "filename" & "_" & DateTime.Now & ".jpg"
    Dim Out As OutputStream
    Out = File.OpenOutput(xui.DefaultFolder, fn, False)
    bmp.WriteToStream(Out, 100, "JPEG")
    Out.Flush
    Out.Close
The Button1 on Pane1 remains clickable.
Date was already formated , also I changed to png , and tried the above code , but sadly no change.
The created files are OK , and can be deleted while the form is hanging.
I forgot to Mension that the whole form is hanging right after writing the snapshot to stream .
i just found this error in red color in the logs :
error log:
Outstanding resource locks detected:
J2D Texture Pool: 9,180,566 used (14.1%), 32,440,320 target (50.0%), 64,880,640 max
10 total resources being managed
average resource age is 2.2 frames
0 resources at maximum supported age (0.0%)
1 resources marked permanent (10.0%)
1 resources have had mismatched locks (10.0%)
1 resources locked (10.0%)
4 resources contain interesting data (40.0%)
0 resources disappeared (0.0%)
Outstanding resource locks detected:
J2D Texture Pool: 9,303,446 used (14.3%), 32,440,320 target (50.0%), 64,880,640 max
13 total resources being managed
average resource age is 1.5 frames
0 resources at maximum supported age (0.0%)
1 resources marked permanent (7.7%)
4 resources have had mismatched locks (30.8%)
3 resources locked (23.1%)
4 resources contain interesting data (30.8%)
0 resources disappeared (0.0%)
any idea ?
 
Last edited:
Upvote 0

teddybear

Well-Known Member
Licensed User
Which views are there on the pane2?perhaps you can remove someone to try finding the error.
 
Upvote 0

PaulMeuris

Active Member
Licensed User
resource locks detected... resources have mismatched locks...
Now it's up to you to find those locked resources and unlock them...
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
resource locks detected... resources have mismatched locks...
Now it's up to you to find those locked resources and unlock them...
Is there a way to find the resource among hundreds of them?
Shouldn't it be the snapshot itself ? That is locking ?
And what's the meaning of mismatched lock and how to "match" it correctly?
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
Difficult to say without seeing the project code. There are more than 1 resource lock according to the log.

Can you answer the question from @teddybear ?
Can you make a small testproject with the panes like i did in message #3?
To answer @teddybear , there are 3 panes
Containing around 30 View each .
Each pane should take a snapshot sequentially through a timer . After the 3rd time tick , the timer is disabled. . Pane1 is present on top of those 3 panes. .
 
Upvote 0

PaulMeuris

Active Member
Licensed User
So now there are 3 panes! In your first message you stated that there are 2 panes.
Show some code with the 3 panes, the timer and the snapshot subroutine and then maybe we can find a solution!
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
So now there are 3 panes! In your first message you stated that there are 2 panes.
Show some code with the 3 panes, the timer and the snapshot subroutine and then maybe we can find a solution!
i'm sorry i tried to simplify the problem , maybe it was a wrong call.
here is the code :

Code for timer:
Private Sub btnPrint_Click
    Timer2Counter=0
    If File.Exists(File.DirData("appname") & "\PDFs\" , "" )=False Then File.MakeDir(File.DirData("appname") , "PDFs")
    If lblDate.IsInitialized Then    lblDate.RequestFocus
    Timer2.Enabled=True
   
End Sub

Private Sub Timer2_Tick
    'upload and print
    Timer2Counter=Timer2Counter+1
    If Timer2Counter=lo.Size Then Timer2.Enabled=False
    Dim x As Int =Timer2Counter-1
    Prints(x)
   
  
End Sub

Public Sub Prints(i As Int)
    For Each n As Node In PanelsforPages(i).GetAllViewsRecursive
        If n Is TextArea Then
            Dim ta As TextArea = n
            CSSUtils.SetBackgroundColor(ta, fx.Colors.white)
            Dim Content As Node = n.As(JavaObject).RunMethod("lookup",Array(".content"))
            CSSUtils.SetBackgroundColor(Content, fx.Colors.White)
            Dim Content1 As Node = n.As(JavaObject).RunMethod("lookup",Array(".viewport"))
            CSSUtils.SetBackgroundColor(Content1, fx.Colors.White)
            CSSUtils.SetStyleProperty(ta, "-fx-control-inner-background", "white")
            CSSUtils.SetStyleProperty(ta, "-fx-focus-color", "white")
        else If n Is TextField Then
            Dim tf As TextField = n
            CSSUtils.SetBackgroundColor(tf, fx.Colors.White)
        End If
    Next
  
    '''''''''''''''''''''''''''''''''''''''''''take snapshot
    Dim tdt As String = DateTime.Date(DateTime.Now) & "_" & DateTime.Time(DateTime.now)
    tdt=tdt.Replace("-","_").Replace(":","")
    Dim fn As String =  person.Get("CivilID")& "_"  & tdt & "_" & i & ".png"
    Dim Out As OutputStream = File.OpenOutput(File.Dirdata("appname") & "\PDFs\"  , fn ,False)

    PanelsforPages(i).Snapshot.WriteToStream(Out)

    Out.Flush
    Out.Close
return null
End Sub

the whole form hangs , and stops responding at the highlighted line 42
however , the 3 snapshots are taken correctly .
timer ticks every 1000ms


your help is very much appreciated
 
Upvote 0

PaulMeuris

Active Member
Licensed User
To make your code work i had to add a few things to my testproject:
B4X:
    Private Pane1 As Pane
    Private Pane2 As Pane
    Private Pane3 As Pane
    Private PanelsforPages(3) As Pane
    Private Timer2Counter As Int
    Private    Timer2 As Timer
    Private btnPrint As Button
And some initializations in B4XPage_Created:
B4X:
    Pane1.As(B4XView).BringToFront
    Timer2.Initialize("Timer2",1000)
    PanelsforPages(0) = Pane1
    PanelsforPages(1) = Pane2
    PanelsforPages(2) = Pane3
In the Timer2_Tick subroutine i used this code:
B4X:
    'upload and print
    Timer2Counter=Timer2Counter+1
    'If Timer2Counter=lo.Size Then Timer2.Enabled=False
    If Timer2Counter > 2 Then Timer2.Enabled = False
    Dim x As Int =Timer2Counter-1
    Log("Timer2Counter: " & x)
    Prints(x)
The highlighted line 3 might not stop the Timer2. What is the value of lo.Size ?
The rest of the code runs if you use a good filename (and the person object is set) :
B4X:
    'Dim fn As String =  person.Get("CivilID")& "_"  & tdt & "_" & i & ".png"
    Dim fn As String =  "CivilID_"  & DateTime.Now & "_" & i & ".png"
The ticks can be converted to datetime value using the datetime/dateutils methods.
Conclusion: is the timer stopped or does it tick endlessly?
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
To make your code work i had to add a few things to my testproject:
B4X:
    Private Pane1 As Pane
    Private Pane2 As Pane
    Private Pane3 As Pane
    Private PanelsforPages(3) As Pane
    Private Timer2Counter As Int
    Private    Timer2 As Timer
    Private btnPrint As Button
And some initializations in B4XPage_Created:
B4X:
    Pane1.As(B4XView).BringToFront
    Timer2.Initialize("Timer2",1000)
    PanelsforPages(0) = Pane1
    PanelsforPages(1) = Pane2
    PanelsforPages(2) = Pane3
In the Timer2_Tick subroutine i used this code:
B4X:
    'upload and print
    Timer2Counter=Timer2Counter+1
    'If Timer2Counter=lo.Size Then Timer2.Enabled=False
    If Timer2Counter > 2 Then Timer2.Enabled = False
    Dim x As Int =Timer2Counter-1
    Log("Timer2Counter: " & x)
    Prints(x)
The highlighted line 3 might not stop the Timer2. What is the value of lo.Size ?
The rest of the code runs if you use a good filename (and the person object is set) :
B4X:
    'Dim fn As String =  person.Get("CivilID")& "_"  & tdt & "_" & i & ".png"
    Dim fn As String =  "CivilID_"  & DateTime.Now & "_" & i & ".png"
The ticks can be converted to datetime value using the datetime/dateutils methods.
Conclusion: is the timer stopped or does it tick endlessly?
thank you for ur support
timer stops correctly after the 3rd tick
i already tried a plane file name .
and the files are created correctly , and are not locked.
incidentally , why did u use b4xpages ? should that make a difference ?
 
Upvote 0

PaulMeuris

Active Member
Licensed User
The code you shared so far is working fine. So the cause of the error is in some other code from your project.
Can you post your project that produces the error?
The log mentions "J2D Texture Pool:". Does this mean that you are using a special library?
 
Upvote 0

le_toubib

Active Member
Licensed User
Longtime User
The code you shared so far is working fine. So the cause of the error is in some other code from your project.
Can you post your project that produces the error?
The log mentions "J2D Texture Pool:". Does this mean that you are using a special library?
i donno J2D may be related to javafx print library
the project is huge and cannot be posted .
p.s : i did a lame workaround : removed pane from parent ---> take snapshot ---> put it back again .
this way everything looks fine .
but this screen lock is still unexplained
 
Upvote 0
Top