Android Question Bitmap resize

Almora

Active Member
Licensed User
Longtime User
hi..
B4X:
            ...........
            Buffer=c.GetBlob("bitid")
            input.InitializeFromBytesArray(Buffer,0,Buffer.Length)
            Dim bitmap1 As Bitmap
            bitmap1.Initialize2(input)
            input.Close
            imageview1.Bitmap=bitmap1
            imageview1.Gravity = Gravity.CENTER
       '     imageview1.Gravity = Gravity.FILL

I want to view a picture from the database. the picture looks. but I can't see the exact size.
is there a method for exact sizing?
thanks..
 
Last edited:

OliverA

Expert
Licensed User
Longtime User
The Bitmap object has Height and Width properties to determine original size. It also has a Resize method you can use to resize the image (with the option to keep the aspect ratio intact).
 
Upvote 0

Almora

Active Member
Licensed User
Longtime User
B4X:
'    resimv.Bitmap = LoadBitmapResize(dir, filename, resimv.Width, resimv.Height, True)
'    resimv.Gravity = Gravity.CENTER

how can I do similarly.
It needs to maintain its aspect ratio.
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
B4X:
'    resimv.Bitmap = LoadBitmapResize(dir, filename, resimv.Width, resimv.Height, True)
'    resimv.Gravity = Gravity.CENTER

how can I do similarly.
It needs to maintain its aspect ratio.
You can calculate your aspect ratio from your original height / width.

Take your original height. let's say it's 1200 pixels
Take your original width. Say 1600 pixels
Divide the height by the width - 1200 / 1600 = 0.75
Multiply the quotient by the preferred width, e.g. 0.75 * 300 = 225
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
try
B4X:
           Buffer=c.GetBlob("bitid")
            input.InitializeFromBytesArray(Buffer,0,Buffer.Length)
            Dim bitmap1 As Bitmap
            bitmap1.Initialize2(input)
            input.Close
            'Which ever side you want to keep, needs to be the smaller of the two values. For example, if you want to resize to a width of 640 and have
            'the height calculated, then the height must be larger then the width.
            'Here we have a fixed width of 640 and have the system calculate the proper height
            bitmap1 = bitmap1.Resize(640, 700, True)
            'The line below would be used if you want the height to be 400 and have the system calculate the width
            'bitmap1 = bitmap1.Resize(600, 400, True)
            imageview1.Bitmap=bitmap1
            imageview1.Gravity = Gravity.CENTER
Update: Slight coding error fixed. Resize returns the newly resized bitmap. I forgot to assign it back to bitmap1.
 
Last edited:
Upvote 0

Almora

Active Member
Licensed User
Longtime User
I tried the code .. the picture is not fully visible.

..... Gravity.CENTER

SharedScreenshot3.jpg



..... Gravity.FILL

SharedScreenshot1.jpg




must be

SharedScreenshot4.jpg
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I tried the code .. the picture is not fully visible.
1) Make sure that you have the updated code (I forgot to assign the resized image back to bitmap1)
2) You cannot make a square peg fit a round hole. In other words, if the image view is 200x400 and the original source picture is 400x1000 (for example), then you will not be able to fit the source neatly into the image view with the aspect ratio intact. The source picture would convert to 200x500 in this case (with aspect ratio intact). You then either will have an overfill (gravity center) or the picture will be squeezed vertically (fill). In order for neither to happen, your image view needs to be the same aspect ratio as your source image.
 
Upvote 0

Almora

Active Member
Licensed User
Longtime User
I tried the last code you fixed.

my image source is gallery. It can be any size picture.
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Try
B4X:
          Buffer=c.GetBlob("bitid")
            input.InitializeFromBytesArray(Buffer,0,Buffer.Length)
            Dim bitmap1 As Bitmap
            bitmap1.Initialize2(input)
            input.Close
            Dim widthRatio As Float = bitmap1.Width / imageview1.Width
            Dim heightRatio As Float = bitmap1.Height / imageview1.Height
            If widthRatio > heightRatio Then
                   bitmap1 = bitmap1.Resize(imageview1.Width, imageview1.Width * 2, True)
            Else
                   bitmap1 = bitmap1.Resize(imageview1.Height * 2, imageview1.Height, True)
            End If
            imageview1.Bitmap=bitmap1
            imageview1.Gravity = Gravity.CENTER
 
Upvote 0
Top