Results 1 to 7 of 7

Thread: Get BMP width and height

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Posts
    601

    Get BMP width and height

    how can I get a BMP's width and height threw API?

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Get BMP width and height

    I think you can do it using this GetBitmapBits API

    HTH
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  3. #3
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Get BMP width and height

    If you don't want to load all the bitmap data, then you could use the alternative function GetObject
    VB Code:
    1. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" ( _
    2.     ByVal hObject As Long, _
    3.     ByVal nCount As Long, _
    4.     lpObject As Any _
    5. ) As Long
    6.  
    7. Private Type BITMAP '14 bytes
    8.         bmType As Long
    9.         bmWidth As Long
    10.         bmHeight As Long
    11.         bmWidthBytes As Long
    12.         bmPlanes As Integer
    13.         bmBitsPixel As Integer
    14.         bmBits As Long
    15. End Type
    16.  
    17. Dim bm As BITMAP
    18. GetObject Picture1.Image, Len(bm), bm

    ...

  4. #4
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Get BMP width and height

    What do you need GetObject for if image is already loaded in the picturebox - simply get ScaleWidth/Height:
    VB Code:
    1. Private Sub Command1_Click()
    2. '=============================
    3. Dim pct As Picturebox
    4.  
    5.     Set pct = Me.Controls.Add("VB.Picturebox")
    6.     pct.Autosize = Tue
    7.     pct.Picture = LoadPicture(App.Path & "\my_image.bmp")
    8.     Debug.Print pct.ScaleWidth
    9.     Debug.Print pct.ScaleHeight
    10.     Set pct = Nothing
    11.  
    12. End Sub

  5. #5

    Thread Starter
    Fanatic Member
    Join Date
    Aug 2003
    Posts
    601

    Re: Get BMP width and height

    all these examples use a picture box. i need to get width/height without picture box.

  6. #6
    Old Member moeur's Avatar
    Join Date
    Nov 2004
    Location
    Wait'n for Free Stuff
    Posts
    2,712

    Re: Get BMP width and height

    What do you need GetObject for if image is already loaded in the picturebox - simply get ScaleWidth/Height:
    Sure, if you want to do it the easy way
    Make sure you're using pixels however
    VB Code:
    1. wid = Picture1.ScaleX(Picture1.ScaleWidth, Picture1.ScaleMode, vbPixels)
    2. hgt = Picture1.ScaleY(Picture1.ScaleHeight, Picture1.ScaleMode, vbPixels)

    But now we're not allowed to use a picture box
    VB Code:
    1. Option Explicit
    2.  
    3. Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" ( _
    4.     ByVal hInst As Long, _
    5.     ByVal lpsz As String, _
    6.     ByVal un1 As Long, _
    7.     ByVal n1 As Long, _
    8.     ByVal n2 As Long, _
    9.     ByVal un2 As Long _
    10. ) As Long
    11.  
    12. Const IMAGE_BITMAP = 0
    13. Const IMAGE_ICON = 1
    14. Const IMAGE_CURSOR = 2
    15. Const LR_LOADFROMFILE = &H10
    16.  
    17. Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" ( _
    18.     ByVal hObject As Long, _
    19.     ByVal nCount As Long, _
    20.     lpObject As Any _
    21. ) As Long
    22.  
    23. Private Type BITMAP '14 bytes
    24.         bmType As Long
    25.         bmWidth As Long
    26.         bmHeight As Long
    27.         bmWidthBytes As Long
    28.         bmPlanes As Integer
    29.         bmBitsPixel As Integer
    30.         bmBits As Long
    31. End Type
    32.  
    33. Sub GetBMP(strImageFile As String)
    34.  Dim hWndIMG
    35.  Dim bm As BITMAP
    36.  hWndIMG = LoadImage(0, strImageFile, 0, 0, 0, LR_LOADFROMFILE)
    37.  GetObject hWndIMG, Len(bm), bm
    38.  Debug.Print bm.bmWidth, bm.bmHeight
    39. End Sub

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Get BMP width and height

    Quote Originally Posted by psychotomus
    all these examples use a picture box. i need to get width/height without picture box.
    Yes, but in my sample I create picturebox control at run time, it's hidden and may get removed from controls collection immediately after reading width/height so I don't see any reasons not to use it - it's simple, fast and clean:
    VB Code:
    1. Option Explicit
    2. Dim intPicWidth As Integer
    3. Dim intPicHeight As Integer
    4.  
    5. Private Sub Command1_Click()
    6. '=============================
    7. Dim pct As Picturebox
    8.  
    9.     Set pct = Me.Controls.Add("VB.Picturebox", "pctTemp")
    10.     pct.Autosize = Tue
    11.     pct.Picture = LoadPicture(App.Path & "\my_image.bmp")
    12.     intPicWidth = pct.ScaleWidth
    13.     intPicHeight = pct.ScaleHeight
    14.     Me.Controls.Remove "pctTemp"
    15.     Set pct = Nothing
    16.  
    17. End Sub

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width