Android Question Saving specific region from canvas

Fusseldieb

Active Member
Licensed User
Hi there,
I have drawed a Bitmap with Canvas and then saved it.
But when I save, it captures the entire Screen. I only want the "DestRect".
Here's my code:
B4X:
Dim C As Canvas
Dim Bitmap1 As Bitmap
Dim Bitmap2 As Bitmap
Dim DestRect As Rect
SetFont("Font.ttf")
C.Initialize(Activity)

Bitmap1.Initialize(File.DirAssets,"Img1.png")
Bitmap2.Initialize(File.DirAssets,"Img2.png")
DestRect.Initialize(0,0,240,180)

C.DrawRect(DestRect,Colors.Blue,True,1)
C.DrawBitmap(Bitmap2,Null,DestRect)
C.DrawBitmap(Bitmap1,Null,DestRect)
C.DrawText("Text1", 20,30,default_font,5,Colors.white,"LEFT")
C.DrawText("Text2", 20,45,default_font,5,Colors.white,"LEFT")
C.DrawText("Text3",DestRect.Right- 20,30,default_font,5,Colors.white,"RIGHT")
C.DrawText("Text4",DestRect.Right- 20,DestRect.Bottom- 20,default_font,5,Colors.white,"RIGHT")

Dim Out As OutputStream
Out = File.OpenOutput(File.DirInternalCache, "TempImage.png", False)
C.Bitmap.WriteToStream(Out, 90, "JPEG")
Out.Close
I have tried clipping and it doesn't work :c
 

klaus

Expert
Licensed User
It depends on what you want to do.
As your canvas is initialized to the Activity Canvas.Bitmap is the whole screen.
You could define a Panel with the dimensions of DestRect, set the canvas to this panel.
Draw on this canvas and save it.
 

Fusseldieb

Active Member
Licensed User
As your canvas is initialized to the Activity Canvas.Bitmap is the whole screen.
You could define a Panel with the dimensions of DestRect, set the canvas to this panel.
Draw on this canvas and save it.
Thanks, but i have tried this now and it doesn't work...
It continues to save the entire screen

B4X:
......
Dim Panel1 As Panel

Panel1.Initialize("")
Activity.AddView(Panel1,0,0,240,180)

SetFont("Font.ttf")
C.Initialize(Panel1)
.....
EDIT: It worked!!!
The image ws open and my (stupid) cellphone has cached it. So I closed and reopened the image. Now it's just fine.
Thanks so much :)
 
Last edited:
Top