How do I Extract part of a bitmap?

BarrySumpter

Active Member
Licensed User
Longtime User
I found this post which is pretty much exactly what I want.

I can't seem to find a post on how to do it in b4a?

Copy part of bitmap
http://www.b4x.com/forum/libraries-developers-questions/16701-copy-part-bitmap.html#post95339

Can we do this with the current libraries?

There is java code example script in that post.

Or does someone need to write a new library for this?
Or maybe someone can add this to their existing library?

Any help would be greatly appreciated.
 
Last edited:

BarrySumpter

Active Member
Licensed User
Longtime User
I want to use that clipped graphic for a button icon or a .png file.
But can't seem to workout how to clipNsave that smaller graphic.

I can't limit the size of a canvas.
So the canvas size is the screen size.

So even if I draw into a canvas in the top left 40x40.
The convas is still the screen size.




B4X:
Sub btnTest_Click
    
        Dim Out As OutputStream
        Dim bmp As Bitmap
        Dim fileName As String


'   WORKS PROPERLY for saving a full screen snapshot

'        bmp.Initialize3(cnvCentreCrossHairs.Bitmap)
'
'        fileName = "mapCentreCrossHairs.PNG"
'    Out = File.OpenOutput (File.DirRootExternal, fileName ,  False)
'    bmp.WriteToStream(Out,  100, "PNG")
'    Out.Close

'



'DrawBitmap (SrcBitmap Bitmap1 As android.graphics.Bitmap, SrcRect As android.graphics.Rect, DestRect As android.graphics.Rect)
'Draws a Bitmap.
'SrcRect - The subset of the Bitmap that will be drawn. 
'              If SrcRect isNull Then the complete SrcBitmap will be drawn.
'DestRect - The rectangle that the Bitmap will be drawn To.

'Example:

Dim Bitmap1 As Bitmap
Bitmap1.Initialize3(cnvCentreCrossHairs.Bitmap)

'Bitmap1.Initialize(File.DirRootExternal, "mapCentreCrossHairs.PNG")

'Canvas1.DrawBitmap(Bitmap1,  Null, DestRect) 'draws the bitmap to the destination rectangle.
Dim SrcRect As Rect
                                        '(left, top, right, bottom)
SrcRect.Initialize( 50%x - 25dip, 50%y - 25dip,  50%x + 25dip, 50%y + 25dip)

Dim DestRect As Rect
DestRect.Initialize(0,0, 50dip, 50dip )


Dim Canvas1 As Canvas
  Canvas1.Initialize(Activity)

'Canvas1.DrawBitmap( Bitmap1,  Null, DestRect) 'draws the bitmap to the destination rectangle.
Canvas1.DrawBitmap(Bitmap1, SrcRect, DestRect) 

Activity.Invalidate

        bmp.Initialize3(Canvas1.Bitmap)

        fileName = "mapCentreCrossHairs.PNG"
    Out = File.OpenOutput (File.DirRootExternal, fileName ,  False)
    bmp.WriteToStream(Out,  100, "PNG")
    Out.Close



End Sub
Do I need to use/can I use an imageView to limit the size to 40x40?
B4X:
myImageView.initialize(40x40)
myImageView.DrawBitmap(Bitmap1, SrcRect, DestRect)
save myImageView.bitmap
 
Last edited:
Upvote 0

BarrySumpter

Active Member
Licensed User
Longtime User
Beginners guide - Section 14 - Graphics / Drawing


B4X:
'Activity module
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.

End Sub

Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.

...
  Dim pnl As Panel  ' create a panel - so I can assign a canvas to it - so I can draw on the canvas

End Sub

Sub Activity_Create(FirstTime As Boolean)

...

    pnl.Initialize("pnl")  
    Activity.AddView(pnl, 100%x - 50dip, 0dip, 50dip,  50dip)

End Sub


Sub DrawCenterPosition(xc As Float, yc As Float)
    
  ...
End Sub



Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub btnTest_Click
    
        Dim Out As OutputStream
        Dim bmp As Bitmap
        Dim fileName As String

...
    Dim cvs As Canvas  ' to draw on
    cvs.Initialize(pnl)    ' canvas HAS to be assigned to a view
                               ' here the view is a panel named pnl to limit height and width
                               'a new mutable bitmap is created for my pnl view background, 
                               'the pnl view's background is copied to the new bitmap 
                               'and the canvas is set to draw on the new bitmap

    
    ...

    Dim Bitmap1 As Bitmap
    Bitmap1.Initialize3(cnvCentreCrossHairs.Bitmap)

    Dim SrcRect As Rect
                                            '(left, top, right, bottom)
    SrcRect.Initialize( 50%x - 25dip, 50%y - 25dip,  50%x + 25dip, 50%y + 25dip)

    Dim DestRect As Rect
    DestRect.Initialize(0,0, 50dip, 50dip )

    cvs.DrawBitmap(Bitmap1, SrcRect, DestRect) 

'or may be I dont; need Bitmap1
   ' cvs.DrawBitmap(cnvCentreCrossHairs.Bitmap, SrcRect, DestRect) 
   
    Activity.Invalidate

    fileName = "mapCentreCrossHairs4.PNG"
    Out = File.OpenOutput (File.DirRootExternal, fileName ,  False)
    cvs.Bitmap.WriteToStream(Out,  100, "PNG")
    Out.Close

    
End Sub
 
Last edited:
Upvote 0

Helihead

Member
Licensed User
Longtime User
I need to place a mask of a specific size over a pic selected from the gallery, allow the user to center the mask as they wish and then have it crop out the photo to that fixed size.

I already have my app where it lets the user select a desired picture from the gallery and allows them to resize and place it in a box on the screen. Problem is that they are not constrained to specific dimensions. I need then to only be able to select a 120 wide by 180 tall snippet of the pic.

Any ideas how to do that?

I used examples similar to the one above to resize the pic but it distorts the image height to width ratio.

Thanks
 
Upvote 0
Top