Know Image Size

i have this code

  Dim p As Picture
  Dim f As FolderItem
  f = SpecialFolder.Desktop.Child("logo.png")
  p = Picture.Open(f)

how do i check the size, in Mb, of the picture?

by “size” I assume you mean file size on disk, as the in memory size won’t do you much good (the image is expanded, and compressed depending on GIF vs PNG vs JPG etc)…

  Dim p As Picture
  Dim f As FolderItem
  f = SpecialFolder.Desktop.Child("logo.png")
  p = Picture.Open(f)
  dim size as int64 = f.length

awesome! thanks

The length is in Bytes so you’ll have to do some math to get to Mb.

And, depending on which platform you are on, you might need to calculate MB differently (at least if you want it to match how it is viewed on that platform).

WIndows (And Linux, I’m assuming) use multiples of 1024

Apple switched to multiples of 1000 starting with Snow Leopard.

This is what I’m using:

[code]Function FormatFileSize(extends size as Int64) As String
Dim s as string
Dim KiloSize as integer
KiloSize = 1024

#if TargetMacOS
// If Snow Leopard or greater (10.6), then use 1000
dim sys1, sys2 as Integer
call System.Gestalt(“sys1”, sys1)
call System.Gestalt(“sys2”, sys2)

if ( sys1 >= 10) Then
  // OS 10
  if (sys2 >= 6) Then
    //Mac OSX Snow Leopard (or greater)
    KiloSize = 1000
  end if
end if

#endif

select case true
Case (size < KiloSize)
// Format as Bytes
s = Format(Size, “#.0#” ) + " Bytes"
Case (size < (KiloSize ^2))
// Format as k
s = Format(Size/KiloSize, “#.0#” ) + " k"
Case (size < (KiloSize^3))
// Format as MB
s = Format(Size/(KiloSize ^2), “#.0#” ) + " MB"
Case (size < (KiloSize ^4))
// Format as GB
s = Format(Size/(KiloSize ^3), “#.0#” ) + " GB"
Case else
// Format as TB
s = Format(Size/(KiloSize ^4), “#.0#” ) + " TB"
end select

return s

End Function
[/code]

great responses… very useful!