Search code examples
heightwidthpowerbuilder

How to get width and height of an image in PB


I'm using PB8.0 and I want to get the width and height of a picture to resize my picture in DW. Do you know how to get it?

I search an example to get width and height of a bitmap file. It seems correct with bitmap file but not for other such as .jpg, .gif

Please help me

//Getting a Bitmaps Width and Height 
int     li_job
blob    b
string  ls_ext

ls_ext = lower(Right(as_filename, 3))
CHOOSE CASE ls_ext
  CASE 'bmp'
    li_job = FileOpen( "filename.bmp", StreamMode!, Read! )
    IF li_job > 0 THEN
      FileRead( li_job, b )

      arl_Width  = Long(Integer( BlobMid( b, 19, 2 ) ), &
                   Integer( BlobMid( b, 21, 2) ) )
      arl_Height = Long(Integer( BlobMid( b, 23, 2 ) ), &
                   Integer( BlobMid( b, 25, 2) ) )
      FileClose( li_job )
    ELSE
      RETURN -1
    END IF
  CASE ELSE
    RETURN -1
END CHOOSE
RETURN 1

Thanks


Solution

  • Here are the code to get the width and height of JPG:

    //JPG
    
    li_file = FileOpen(as_file_name, StreamMode!, Read! )
    If li_file = -1 Then Return 0
    
    li_read = FileRead (li_file, lb_data)  
    
    If String (BlobMid (lb_Data, 1,3)) = Char (255) + Char (216) + Char (255) Then //JPG
    
        ll_DataLen = Len(lb_Data)   
        ll_DataPos = 3
        ll_FilePos = 3
        lb_LoopFlag = true
        Do While lb_LoopFlag
            ll_DataPos = ll_DataPos + 1
            ll_FilePos = ll_FilePos + 1
            li_Char1 = Asc (String (BlobMid(lb_Data, ll_DataPos, 1)))
            li_Char2 = Asc (String (BlobMid(lb_Data, ll_DataPos + 1, 1)))
    
            If li_Char1 = 255 And li_Char2 <> 255 Then
                If li_Char2 >= 192 And li_Char2 <= 195 Then
                    al_width = Asc (String (BlobMid (lb_Data, ll_DataPos + 7,1))) * 256 + Asc (String (BlobMid (lb_Data, ll_DataPos + 8,1)))
                    al_height= Asc (String (BlobMid (lb_Data, ll_DataPos + 5,1))) * 256 + Asc (String (BlobMid (lb_Data, ll_DataPos + 6,1))) 
                    lb_LoopFlag= False
                Else   
                    ll_FilePos = ll_FilePos + Asc (String (BlobMid(lb_Data, ll_DataPos + 3, 1))) + Asc (String (BlobMid(lb_Data, ll_DataPos + 2, 1))) + 1
                    If ll_FilePos > ll_FileLength Then   
                        Exit   
                    Else
                        FileSeek(li_file, ll_FilePos)   
                        FileRead(li_file, lb_Data)   
                        ll_DataLen = Len(lb_Data)   
                        ll_DataPos = 0   
                    End If   
                End If   
            End If
    
            If ll_DataPos = ll_DataLen - 9 And lb_LoopFlag = True Then   
                ll_FilePos = ll_FilePos - 9   
                FileSeek(li_file, ll_FilePos)   
                FileRead(li_file, lb_Data)   
                ll_DataLen = Len(lb_Data)   
                ll_DataPos = 0   
            End If
            If ll_FilePos >= ll_FileLength Then   
                lb_LoopFlag = False   
            End If   
        Loop
    End If
    

    For GIF:

    al_width = Asc(String (BlobMid (lb_Data, 7,1))) + Asc (String (BlobMid (lb_Data, 8,1))) * 256
    al_height = Asc (String (BlobMid (lb_Data, 9,1))) + Asc (String (BlobMid (lb_Data, 10,1))) * 256