Get a ScrollView's content as a bitmap

nicholas.jj.taylor

Member
Licensed User
Longtime User
I've been trying to get a ScrollView's Panel content as a bitmap, but Scrollview.panel is reported as "Layout not available". This I think is due to memory management.

I have a function that gets a bitmap from a view:
B4X:
Sub GetBitmapFromView(VwSource As View) As Bitmap
   Dim StrMethod As String = "Sub GetBitmapFromView(VwSource As View) As Bitmap"
   Dim BmpResult As Bitmap = Null
   Try
      Dim IntWidth As Int
      IntWidth = VwSource.Width
      
      Dim IntHeight As Int
      IntHeight = VwSource.Height
      
      Dim Bmp As Bitmap
      Bmp.InitializeMutable(IntWidth, IntHeight)
      
      Dim Cnv As Canvas
      Cnv.Initialize2(Bmp)
      
      Dim Rfl As Reflector
      Rfl.Target = Cnv
      
      Dim AryObjectRef(1) As Object 
      AryObjectRef = Array As Object (Rfl.GetField("canvas"))
      
      Dim AryObjectTypeRef(1) As String
      AryObjectTypeRef = Array As String ("android.graphics.Canvas")
      
      Dim RftView As Reflector
      RftView.Target = VwSource
      
      RftView.RunMethod4("draw", AryObjectRef, AryObjectTypeRef)
      
      BmpResult = Bmp
   
   Catch
      CdException.Show(StrClass, StrMethod, LastException)
   End Try
   Return BmpResult
End Sub
 
Last edited:

nicholas.jj.taylor

Member
Licensed User
Longtime User
Solved: The problem was that I was using the ScrollView.Height instead of ScrollView.Panel.Height

B4X:
Sub GetBitmapFromScrollView(ScrSource As ScrollView) As Bitmap
   Dim StrMethod As String = "Sub GetBitmapFromScrollView(ScrSource As ScrollView) As Bitmap"
   Dim BmpResult As Bitmap = Null
   Try      
      Dim IntWidth As Int
      IntWidth = ScrSource.Width
      
      Dim IntHeight As Int
      IntHeight = ScrSource.Panel.Height
      
      Dim Bmp As Bitmap
      Bmp.InitializeMutable(IntWidth, IntHeight)
      
      Dim Cnv As Canvas
      Cnv.Initialize2(Bmp)
      
      Dim Rfl As Reflector
      Rfl.Target = Cnv
      
      Dim AryObjectRef(1) As Object 
      AryObjectRef = Array As Object (Rfl.GetField("canvas"))
      
      Dim AryObjectTypeRef(1) As String
      AryObjectTypeRef = Array As String ("android.graphics.Canvas")
      
      Dim RftView As Reflector
      RftView.Target = ScrSource
      
      RftView.RunMethod4("draw", AryObjectRef, AryObjectTypeRef)
      
      BmpResult = Bmp
   
   Catch
      CdException.Show(StrClass, StrMethod, LastException)
   End Try
   Return BmpResult
End Sub
 
Last edited:
Upvote 0

Robert Valentino

Well-Known Member
Licensed User
Longtime User
Was going to ask this as a new thread but it seems to me it should be a continuation of this one

I am using the above function to make a bitmap from a ScrollView2D


Is it possible to pass additional parameters to the "draw" function.

I would like to create a bitmap from a ScrollView2D but do not want the complete ScrollView2D.

Is it possible to give a Top and Left position in additional to the Width and Height


BobVal
 
Upvote 0
Top