forum.vdsworld.com Forum Index forum.vdsworld.com
Visit VDSWORLD.com
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


GET pixel size without loading a pic? hmm

 
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help
View previous topic :: View next topic  
Author Message
vtol
Valued Contributor
Valued Contributor


Joined: 05 Feb 2004
Posts: 656
Location: Eastern Indiana

PostPosted: Thu Dec 28, 2006 1:19 am    Post subject: GET pixel size without loading a pic? hmm Reply with quote

GET pixel size without loading a pic?

I would like to be able to get the pixel width and pixel height of an imagefile within my VDS5 program without having to get this information inside a paint program.

EXAMPLE:
That way I can adjust the picture to fit in a frame(vds shape) in my vds5 dialog by way of one of the vds DLLs.

Thanks for looking.. and appreciate any input at all Very Happy
Back to top
View user's profile Send private message Visit poster's website
vtol
Valued Contributor
Valued Contributor


Joined: 05 Feb 2004
Posts: 656
Location: Eastern Indiana

PostPosted: Thu Dec 28, 2006 1:53 am    Post subject: Reply with quote

I havent messed with binary for years, but heres something I found, the 4th and 5th byte is where the imagefile tells its pixel size.

Below code:
This example shows how to get size information from a GIF or JPG file without loading the whole picture in Visual Basic 6.

Subroutine GetGIFInfo gets the GIF file's size and then reads its first five bytes. The first three bytes should be "GIF" if this is a GIF file.

The next two bytes gives the image's width and the two after that give its height.

Code:
Private Function GetGIFInfo(ByVal FileName As String) As _
    BITMAPINFO
    Dim bChar As Byte
    Dim i As Integer
    Dim dotPos As Integer
    Dim Header As String
    Dim blExit As Boolean
    Dim a As String, b As String
    Dim ImgSize As String
    Dim fnum As Integer
    Dim ImgWidth As Integer
    Dim ImgHeight As Integer

    On Error Resume Next
    fnum = FreeFile
    Open FileName For Binary As #fnum

    ImgSize = LOF(fnum) / 1024

    dotPos = InStr(ImgSize, ",")
    ImgSize = Left(ImgSize, dotPos - 1)

    For i = 0 To 5
        Get #fnum, , bChar
        Header = Header + Chr(bChar)
    Next i

    If Left(Header, 3) <> "GIF" Then
        MsgBox FileName & ": not a GIF file"
        Close #fnum
        Exit Function
        End
    End If

    Get #fnum, , bChar
    a = a + Chr(bChar)
    Get #fnum, , bChar
    a = a + Chr(bChar)

    ImgWidth = CInt(Asc(Left(a, 1)) + 256 * Asc(Right(a, _
        1)))

    Get #fnum, , bChar
    b = b + Chr(bChar)
    Get #fnum, , bChar
    b = b + Chr(bChar)

    ImgHeight = CInt(Asc(Left(b, 1)) + 256 * Asc(Right(b, _
        1)))

    Close #fnum

    With ImageInfo
        .Width = ImgWidth
        .Height = ImgHeight
    End With

    GetGIFInfo = ImageInfo
End Function


Hope this helps Rolling Eyes
Back to top
View user's profile Send private message Visit poster's website
vtol
Valued Contributor
Valued Contributor


Joined: 05 Feb 2004
Posts: 656
Location: Eastern Indiana

PostPosted: Thu Dec 28, 2006 2:59 am    Post subject: Reply with quote

Heres what it looks like below in an example sunset.gif pic:

The pixel width and height should be in the highlighted box 4 and 5.
I can't remember how to read binary Laughing


Last edited by vtol on Thu Dec 28, 2006 8:40 pm; edited 1 time in total
Back to top
View user's profile Send private message Visit poster's website
DaveR
Valued Contributor
Valued Contributor


Joined: 03 Sep 2005
Posts: 413
Location: Australia

PostPosted: Thu Dec 28, 2006 3:39 am    Post subject: Reply with quote

You can ease up on the extra info Cool

I've got some example code here that I'm just tidying up and will post shortly...

BTW the width is the 2003 on the first line, and the height is the 5802 also on the first line.

_________________
cheers

Dave
Back to top
View user's profile Send private message
DaveR
Valued Contributor
Valued Contributor


Joined: 03 Sep 2005
Posts: 413
Location: Australia

PostPosted: Thu Dec 28, 2006 3:53 am    Post subject: Reply with quote

Ok, here's what I've got. But it's only working on small gif sizes. I'm not 100% today so I'm trouble concentrating (you'll have to get someone else to fill in the blanks).

Code:

  #DEFINE FUNCTION,Hex2Dec

%%File = C:\Temp\test.gif
 
  if @file(%%File)
    binfile open,1,%%File,READ
    # 0x00 - ID
    %x = @binfile(read,1,TEXT,3)
    if @unequal(%x,GIF)
      warn File is not a GIF image!     ,
    end
    # 0x06 - Width
    binfile seek,1,6
    %x = @binfile(read,1,HEX,1)
rem    %x = @prod(1,@trim(@substr(%x,1,2))@trim(@substr(%x,4,5)))
rem    %x = @trim(@substr(%x,1,2))@trim(@substr(%x,4,5))
    %x = @trim(@substr(%x,1,2))
    %y = @hex2dec(%x)
    %%Width = %y
    # 0x06 - Height
    binfile seek,1,8
    %x = @binfile(read,1,HEX,1)
    %x = @trim(@substr(%x,1,2))
    %y = @hex2dec(%x)
    %%Height = %y
    binfile close,1
    info GIF Width and Height is %%Width x %%Height
  else
    warn %%File not found!     ,
  end
  stop

#-----------------------------------------------------------------------------
#  USER DEFINED FUNCTIONS

:Hex2Dec
  %%hex = %1
  %%decimal = 0
  %%decx = 1
  repeat
    %%decchar = @upper(@substr(%%hex,%%decx,%%decx))
    %%decpos = @fsub(@pos(%%decchar,"0123456789ABCDEF"),1)
    %%decimal = @fadd(@fmul(%%decimal,16),%%decpos)
    %%decx = @succ(%%decx)
  until @greater(%%decx,@len(%%hex))
  exit %%decimal

_________________
cheers

Dave
Back to top
View user's profile Send private message
vtol
Valued Contributor
Valued Contributor


Joined: 05 Feb 2004
Posts: 656
Location: Eastern Indiana

PostPosted: Thu Dec 28, 2006 4:06 am    Post subject: Reply with quote

allright Dave Bin Very Happy awesome

I'll play with it on some .gif and .jpg files in a bit.

Nice job and thanks a godzillion for the great help, this will be nice when all done someday I'm sure.
A DLL would work too, but I couldnt find one that GETs pixel width and height.

Thanks again Dave
Back to top
View user's profile Send private message Visit poster's website
PGWARE
Web Host


Joined: 29 Dec 2001
Posts: 1565

PostPosted: Thu Dec 28, 2006 5:12 am    Post subject: Reply with quote

VDSCONV.dll will get the width/height for bitmap, gif, and jpeg.
Back to top
View user's profile Send private message
vtol
Valued Contributor
Valued Contributor


Joined: 05 Feb 2004
Posts: 656
Location: Eastern Indiana

PostPosted: Thu Dec 28, 2006 8:46 pm    Post subject: Reply with quote

I updated the pic.

Thanks PGWARE, never knew that one would convert and read pixel size.
Thats great, I'll try it today.

Dave, I tried your code and the height worked but the width did not(I know you said you were tired).
I'll tinker with your code and see if I can get the width working, cause it would be fun.

Thank you both very very much Smile
Back to top
View user's profile Send private message Visit poster's website
DaveR
Valued Contributor
Valued Contributor


Joined: 03 Sep 2005
Posts: 413
Location: Australia

PostPosted: Fri Dec 29, 2006 6:48 am    Post subject: Reply with quote

vtol wrote:
Dave, I tried your code and the height worked but the width did not(I know you said you were tired).

The example code I posted would only work with small widths and heights that only used the 1st byte. i.e. 20 instead of 2003. The problem is that, compared to all previous hex files I've coded for, GIFs seem to use big endien (or little endien? - never did really learn the difference) for the file width and height. i.e. Other files I've played with would use '00 A0' for a size of 160 but GIFs use 'A0 00'. If you convert 'A0 00' to decimal you end up with 40960. But if you convert just A0 you get 160. So if the width or height is greater than 249 you need to read the 2 bytes and convert to decimal. But this then gives the wrong answer if the width is less than 250.

Code:
    %x = @binfile(read,1,HEX,2)
    %x = @trim(@substr(%x,1,2))@trim(@substr(%x,4,5))
    %y = @hex2dec(%x)


vtol wrote:
I'll tinker with your code and see if I can get the width working, cause it would be fun.

Yeah, I prefer to do everything in VDS if I can, to avoid the need to included large library files. And at 238KB vdsconv.dll is large by my standards. But the source is included so you 'could' compile a smaller dll with just what you needed.

_________________
cheers

Dave
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    forum.vdsworld.com Forum Index -> General Help All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You can attach files in this forum
You can download files in this forum

Twitter@vdsworld       RSS

Powered by phpBB © 2001, 2005 phpBB Group