Android Question Obtain PNG image width and height

JTmartins

Active Member
Licensed User
Longtime User
PNG’s width and height are always bytes 16-24, so I think that if you open the file to read bytes and check bytes 16 to 24 you may be able to get what you want.

I can't remember any other way do check that, but eventualy there will be other methods.
 
Upvote 0

derez

Expert
Licensed User
Longtime User
if you open the file to read bytes and check bytes 16 to 24
That is correct:
B4X:
Dim raf As RandomAccessFile
raf.Initialize(File.DirInternal,"cover.png",True)
Log("width   " & raf.ReadInt(16))
Log("height  " & raf.ReadInt(20))

I believe RAF initialization is more efficient, since it reads the compressed file while bitmap decompress it to a bmp size.
 
Last edited:
Upvote 0

Informatix

Expert
Licensed User
Longtime User
while bitmap decompress it to a bmp size.
Absolutely not. The BitmapPlus solution is probably a lot faster than the solution given above (the RAF library is so slow that it is very easy to outperform it with anything else) and it works with any recognized image format (JPG or PNG). It does not convert or load anything. It just reads and decodes the header of the file.
 
Upvote 0

Baltazar

Member
Licensed User
Longtime User
This one works:
B4X:
Sub Activity_Create(FirstTime As Boolean)
 
    bmp.Initialize(File.DirAssets,"mypng.png")
    Log("BMP width: " &bmp.Width)
    Log("BMP height: " &bmp.Height)
End Sub
 
Upvote 0
Top