Android Question How do I save objects on a panel to a png

morphe2011

Member
Licensed User
Longtime User
Hello all. I am brain farting. In B4A I have a panel that I am dynamically adding both an image view and a text box to. I want to merge them and save the results as a png. I did this for another project years ago using the canvas but for the life of me I cannot remember how. Any help would be much appreciated .
 

Myr0n

Active Member
Licensed User
Longtime User
I use this

B4X:
    Wait For (TakeAScreenShot(MainPanel,"ResultsInThisImage")) Complete (Result As Bitmap)
    imgGame.Bitmap=Result
'Or
'imgGame.Bitmap = LoadBitmap(GetDataFolder, "ResultsInThisImage.png")


Sub TakeAScreenShot(pnl As Panel, bmpFileName As String) As ResumableSub
    ' Take a screenshot.
    'Depends on: Reflector, JavaObject
    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim C As Canvas
    Obj1.Target = pnl
    bmp.InitializeMutable(pnl.Width, pnl.Height)
    C.Initialize2(bmp)
    Dim args(1) As Object
    Dim types(1) As String
    Obj2.Target = C
    Obj2.Target = Obj2.GetField("canvas")
    args(0) = Obj2.Target
    types(0) = "android.graphics.Canvas"
    Obj1.RunMethod4("draw", args, types)
    If bmpFileName.Length>0 Then
        Dim Out As OutputStream
        Out = File.OpenOutput(GetDataFolder, bmpFileName.Trim & ".png", False)
        bmp.WriteToStream(Out, 100, "PNG")
        Out.Close
    End If
    Return bmp
End Sub
 
Last edited:
Upvote 0

morphe2011

Member
Licensed User
Longtime User
Thank you both for the replies.

Peacemaker I will checkout the B4XView for another app. I am loving the cross platform objects that are being developed.

Myron your solution was the quick & dirty I was looking for this solution . Most Thanks.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…