iOS Question Resize Image Sub

Luiz Fernando Orlandini

Active Member
Licensed User
Hi All.

I'm trying to use this code snippet to resize an image, but I don't have success. What's am I doing wrong?

B4X:
Public Sub loadPhoto(photo As String) As Bitmap
    Try
        Dim su As StringUtils
        Dim i As InputStream
        i.InitializeFromBytesArray(su.DecodeBase64(photo), 0, su.DecodeBase64(photo).Length)
        Dim p As Bitmap
        p.Initialize2(i)
        Return p
    Catch
        Dim p As Bitmap
        p.Initialize(File.DirAssets, "avatar-scholastic.png")
        Return p       
    End Try
End Sub


Public Sub ResizeImage (Image As String, Scale As Int) As Bitmap
    Dim PhotoCanvas As Canvas
    Dim PhotoPanel As Panel
    Dim PhotoView As ImageView
    Dim NewImage As Bitmap
 
    PhotoView.Initialize("")
    PhotoView.Bitmap = loadPhoto(Image)

    PhotoPanel.Initialize("")
    PhotoPanel.Height = 500
    PhotoPanel.Width = 500
    PhotoPanel.SetBorder(0, Colors.Transparent, PhotoView.Width/2)
 
    PhotoPanel.AddView(PhotoView,0,0, PhotoPanel.Width / Scale, PhotoPanel.Height / Scale)

    PhotoCanvas.Initialize(PhotoPanel)
    NewImage = PhotoCanvas.CreateBitmap
 
    Return NewImage
End Sub
 

Erel

Administrator
Staff member
Licensed User
You can use this code to resize an image:
B4X:
Sub ResizeBitmap(bmp As Bitmap, scale As Float) As Bitmap
   Dim img As ImageView
   img.Initialize("")
   img.Width = bmp.Width * scale
   img.Height = bmp.Height * scale
   Dim cvs As Canvas
   cvs.Initialize(img)
   cvs.DrawBitmap(bmp, cvs.TargetRect)
   Dim res As Bitmap = cvs.CreateBitmap
   cvs.Release
   Return res
End Sub
 
Last edited:
Top