Android Question Help: Lost in capturing panel to file and ImageView


Licensed User
Longtime User

I am planning to capture whole panel and save it to PNG file to archive. Because i am new to B4A (i skip .NET, last programming experience in VB6), i used ilan's codesnippet link below;

It works but not correct because of some reasons. First reason, i am not using panel only. I am using panel inside tabhost. The second problem is, i cant get exact positions of image and i get cropped image or streched image.

Here my Code;

Sub PanelCapture(pnl As Panel)

Dim Obj1, Obj2 As Reflector
Dim bmp As Bitmap
Dim c As Canvas
Obj1.Target = Obj1.GetActivityBA
Obj1.Target = Obj1.GetField("vg")
bmp.InitializeMutable(pnl.left + pnl.Width, pnl.Top + pnl.Height)

Dim args(1) As Object
Dim types(1) As String
Obj2.Target = c
Obj2.Target = Obj2.GetField("canvas")
args(0) = Obj2.Target
types(0) = ""
Obj1.RunMethod4("draw", args, types)


'draw from image to canavas
Dim canvas1 As Canvas
Dim scrt As Rect
scrt.Initialize(pnl.left,,  pnl.Width, pnl.Height)
'scrt.Initialize(0, 0,  680, 998)

Dim rectPanel1 As Rect
rectPanel1.Initialize(pnl.left,,  pnl.Width, pnl.Height)
canvas1.DrawBitmap(bmp, scrt , rectPanel1)

'here writing canvas to file

Dim Out As OutputStream
Dim MyFileName As String

MyFileName = DateTime.Now & ".png"
Out = File.OpenOutput(File.DirDefaultExternal, MyFileName, False)
canvas1.Bitmap.WriteToStream(Out, 100, "PNG")

First screen shot is original emulator screen (taken shot by ddms)

Second one is, capture after code above. As you see my code captures whole screen instead of panel. And crop right side and bottom of panel (because of correct panel sizes). I dont want to see Tabhost headers. Then i tried to change both rectangle lines in the code like below;

scrt.Initialize(pnl.left+10dip,, pnl.Width+10dip, pnl.Height+50dip)
rectPanel1.Initialize(pnl.left+10dip,, pnl.Width+10dip, pnl.Height+50dip)​

Then i got third screen shot. As you see, this doesnt work. Blank top and left. Then I changed rectangle codes like below. Source rectangle changed, target Rectangle same as previous.

scrt.Initialize(pnl.left+10dip,, pnl.Width+10dip, pnl.Height+50dip)
rectPanel1.Initialize(pnl.left,, pnl.Width, pnl.Height)​

Then I got 4th screen this time. As you see, it doesnt work. Now, blank (truncated) right and bottom. And I changed rectangle codes like below this time (original source and modified target)

scrt.Initialize(pnl.left,, pnl.Width, pnl.Height)
rectPanel1.Initialize(pnl.left+10dip,, pnl.Width+10dip, pnl.Height+50dip)​

It doesnt work again and i got 5th screen this time. As you see, this is worst one :) Blank top and left plus unwanted tabhost (and of course truncated right and bottom)

I tried some other variations but they didnt work. Because i am new, i cant modify code much. Actually i dont understand first 10-15 lines of snippet. After long variation tries, i didnt get any correct result.

Now what i want ?

1. If possible, take shot directly from panel, instead of whole screen. Because its hard to find real "plus dips" and this may causes some strech, which is unwanted.

2. If not possible, what is my correct rectangle coordinates. I know its simple "mathematichs" but i am completely screwed up :(

3. Tabhost is important. I cant change it after weeks. But if no way to crop tabhost banner, i can live with it.



ps: player names in the screenshots are "coincidence". I am not sending any message to experts who can help me. Trust me :)


  • device-2014-09-21-235819.png
    107 KB · Views: 221
  • 1411332872480.png
    206.9 KB · Views: 236
  • 1411333388538.png
    194.5 KB · Views: 232
  • 1411333668936.png
    194.7 KB · Views: 237
  • 1411334028563.png
    185.5 KB · Views: 232