Get Bitmap of a view

francoisg

Active Member
Licensed User
Longtime User
Hi,
is there a "generic" way to get a Bitmap from a View? I know you can use WebView.CaptureBitmap for instance for WebView but is there any way for "generic" views?

I have the following code but it does not seem to work (it seems to get a valid Canvas):

Sub GetBitmap(aView As View) As Bitmap
Dim c As Canvas
c.Initialize(aView)
Return c.BitMap
End Sub
 

francoisg

Active Member
Licensed User
Longtime User
Solved!

Thank you,
I cooked up a function using the code you suggested:

B4X:
Sub viewAsBitmap (aView As View) As Bitmap
    Dim b As Bitmap
    Dim c As Canvas
    b.InitializeMutable(aView.Width, aView.Height) ' Initialize mutable bitmap to contain size of aView ...
    c.Initialize2(b) ' Get bitmap canvas ...
    ' Get B4A.Canvas android.graphics.Canvas (Android native type) field 
    ' We need it as native Android type in order to call the "draw" function below ...
    Dim args(1) As Object
    Dim types(1) As String
    Dim r As Reflector
    r.Target = c
    args(0) = r.GetField("canvas") ' Get android.graphics.Canvas field ...
    types(0) = "android.graphics.Canvas"
    ' Draw aView content onto the bitmap canvas ...
    r.Target = aView
    r.RunMethod4("draw", args, types)
    Return b
End Sub
 
Upvote 0

erfan

Member
Licensed User
Longtime User
i want to save a panel which has some other panels on it.....can i use same function? how should i save it?
 
Last edited:
Upvote 0

Wembly

Member
Licensed User
Longtime User
Is there a way to do this without having to display the view?

I'm wanting generate an image from a view in a service to provide new image update to a widget - is this possible?

Thanks
 
Upvote 0
Top