Android Question Combine an imageview and panel and save the result as image

Sub7

Active Member
Licensed User
Longtime User
Hello,
i have a panel with a png background and an imageview where is possible to load an image from android gallery, how i can save the result (like a screenshot) of both panel and imageview and save it as single file, the result should be similar to attached example. I dont know how to caputure the two views and merge them.

Thank you!
 

Attachments

  • result.png
    result.png
    2.3 KB · Views: 426

LucaMs

Expert
Licensed User
Longtime User
Hello,
i have a panel with a png background and an imageview where is possible to load an image from android gallery, how i can save the result (like a screenshot) of both panel and imageview and save it as single file, the result should be similar to attached example. I dont know how to caputure the two views and merge them.

Thank you!

uhmmm someone will give you a better advice, but in the meantime ...!

On the site you can find how to get the full screenshot, then you should "cut" the image using the canvas and save.

But maybe you can copy directly the panel.

I lose a few minutes and I'll try :)
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
In fact, this should work (I have not tried it):

B4X:
Sub btnSave_Click
    Dim cnv As Canvas
    cnv.Initialize(Panel1)
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirDefaultExternal, "PnlImg.png", False)
    cnv.Bitmap.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub

Not, it don't works, saves only the panel image.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Similar to LucaMS answer, but after cnv.Initialize(Panel1) add:

B4X:
Dim DestRect As Rect
Destrect.Initialize(0,0,ImageView.Width,ImageView.Height)
cnv.DrawBitmap(Imageview1.Bitmap,null,DestRect)

That should copy the bitmap from your imageview to the canvas cnv.

Use DestRect to position and size the copied the image.

Then write to outputstream
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Similar to LucaMS answer, but after cnv.Initialize(Panel1) add:

B4X:
Dim DestRect As Rect
Destrect.Initialize(0,0,ImageView.Width,ImageView.Height)
cnv.DrawBitmap(Imageview1.Bitmap,null,DestRect)

That should copy the bitmap from your imageview to the canvas cnv.

Use DestRect to position and size the copied the image.

Then write to outputstream


I was doing almost like you, but it does not work, it also saves only the panel!
B4X:
Sub btnSave_Click
    Dim cnvPnl As Canvas
    cnvPnl.Initialize(Panel1)
   
'    Dim SrcRect As Rect
'    SrcRect.Initialize(0,0, ImageView1.Width, ImageView1.Height)
   
    Dim DestRect As Rect
    DestRect.Initialize(ImageView1.Left, ImageView1.Top, ImageView1.Width, ImageView1.Height)
    cnvPnl.DrawBitmap(ImageView1.Bitmap, Null, DestRect)

    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirDefaultExternal, "PnlImg.png", False)
    cnvPnl.Bitmap.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
In fact, this should work (I have not tried it):

B4X:
Sub btnSave_Click
    Dim cnv As Canvas
    cnv.Initialize(Panel1)
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirDefaultExternal, "PnlImg.png", False)
    cnv.Bitmap.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub

Not, it don't works, saves only the panel image.
Thanks for your time Lucas, i have reached the same solution by myself but it only save the panel and i need to save the panel, an imageview and one label!
ty
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
It's all in the DestRect setup, it's Left,Top,Right,Bottom and not Left,Top,Width,Height. So if you set 50,50,50,50 you won't get an image as left and right and top and bottom are set the same.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
Sub btnSave_Click
    ' Take a screenshot.
    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Dim now, i As Long
    Dim dt As String
    DateTime.DateFormat = "yyMMddHHmmss"
    now = DateTime.now
    dt = DateTime.Date(now) ' e.g.: "110812150355" is Aug.12, 2011, 3:03:55 p.m.
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
'    bmp.InitializeMutable(Activity.Width, Activity.Height)
    bmp.InitializeMutable(Panel1.Width, Panel1.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)
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirDefaultExternal, dt & ".png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub

ScreenShot from the manual.
Changed the "target", it works
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It's all in the DestRect setup, it's Left,Top,Right,Bottom and not Left,Top,Width,Height. So if you set 50,50,50,50 you won't get an image as left and right and top and bottom are set the same.


You're right.

This works too:
B4X:
Sub btnSave_Click
    Dim cnvPnl As Canvas
    cnvPnl.Initialize(Panel1)
   
    Dim DestRect As Rect
   
    DestRect.Initialize(ImageView1.Left, ImageView1.Top, _
        ImageView1.Left + ImageView1.Width, ImageView1.Top + ImageView1.Height)
   
    cnvPnl.DrawBitmap(ImageView1.Bitmap, Null, DestRect)

    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirDefaultExternal, "PnlImg.png", False)
    cnvPnl.Bitmap.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub
 
Upvote 0

Sub7

Active Member
Licensed User
Longtime User
Similar to LucaMS answer, but after cnv.Initialize(Panel1) add:

B4X:
Dim DestRect As Rect
Destrect.Initialize(0,0,ImageView.Width,ImageView.Height)
cnv.DrawBitmap(Imageview1.Bitmap,null,DestRect)

That should copy the bitmap from your imageview to the canvas cnv.

Use DestRect to position and size the copied the image.

Then write to outputstream
Perfect thanks all of you
 
Upvote 0

Wembly

Member
Licensed User
Longtime User
B4X:
Sub btnSave_Click
    ' Take a screenshot.
    Dim Obj1, Obj2 As Reflector
    Dim bmp As Bitmap
    Dim c As Canvas
    Dim now, i As Long
    Dim dt As String
    DateTime.DateFormat = "yyMMddHHmmss"
    now = DateTime.now
    dt = DateTime.Date(now) ' e.g.: "110812150355" is Aug.12, 2011, 3:03:55 p.m.
    Obj1.Target = Obj1.GetActivityBA
    Obj1.Target = Obj1.GetField("vg")
'    bmp.InitializeMutable(Activity.Width, Activity.Height)
    bmp.InitializeMutable(Panel1.Width, Panel1.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)
    Dim Out As OutputStream
    Out = File.OpenOutput(File.DirDefaultExternal, dt & ".png", False)
    bmp.WriteToStream(Out, 100, "PNG")
    Out.Close
End Sub

ScreenShot from the manual.
Changed the "target", it works

Hi - can anyone please explain how this works.

As I see it all what has changed is the dimensions of bmp is now determined by Panel1. But how is the handling of the area to 'screenshot' handled?

Thanks.
 
Upvote 0
Top