how can I get a BMP's width and height threw API?
Printable View
how can I get a BMP's width and height threw API?
I think you can do it using this GetBitmapBits API
HTH
If you don't want to load all the bitmap data, then you could use the alternative function GetObject
VB Code:
Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" ( _ ByVal hObject As Long, _ ByVal nCount As Long, _ lpObject As Any _ ) As Long Private Type BITMAP '14 bytes bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Dim bm As BITMAP GetObject Picture1.Image, Len(bm), bm
...
What do you need GetObject for if image is already loaded in the picturebox - simply get ScaleWidth/Height:
VB Code:
Private Sub Command1_Click() '============================= Dim pct As Picturebox Set pct = Me.Controls.Add("VB.Picturebox") pct.Autosize = Tue pct.Picture = LoadPicture(App.Path & "\my_image.bmp") Debug.Print pct.ScaleWidth Debug.Print pct.ScaleHeight Set pct = Nothing End Sub
all these examples use a picture box. i need to get width/height without picture box.
Sure, if you want to do it the easy way :)Quote:
What do you need GetObject for if image is already loaded in the picturebox - simply get ScaleWidth/Height:
Make sure you're using pixels however
VB Code:
wid = Picture1.ScaleX(Picture1.ScaleWidth, Picture1.ScaleMode, vbPixels) hgt = Picture1.ScaleY(Picture1.ScaleHeight, Picture1.ScaleMode, vbPixels)
But now we're not allowed to use a picture box
VB Code:
Option Explicit Private Declare Function LoadImage Lib "user32" Alias "LoadImageA" ( _ ByVal hInst As Long, _ ByVal lpsz As String, _ ByVal un1 As Long, _ ByVal n1 As Long, _ ByVal n2 As Long, _ ByVal un2 As Long _ ) As Long Const IMAGE_BITMAP = 0 Const IMAGE_ICON = 1 Const IMAGE_CURSOR = 2 Const LR_LOADFROMFILE = &H10 Private Declare Function GetObject Lib "gdi32" Alias "GetObjectA" ( _ ByVal hObject As Long, _ ByVal nCount As Long, _ lpObject As Any _ ) As Long Private Type BITMAP '14 bytes bmType As Long bmWidth As Long bmHeight As Long bmWidthBytes As Long bmPlanes As Integer bmBitsPixel As Integer bmBits As Long End Type Sub GetBMP(strImageFile As String) Dim hWndIMG Dim bm As BITMAP hWndIMG = LoadImage(0, strImageFile, 0, 0, 0, LR_LOADFROMFILE) GetObject hWndIMG, Len(bm), bm Debug.Print bm.bmWidth, bm.bmHeight End Sub
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:Quote:
Originally Posted by psychotomus
VB Code:
Option Explicit Dim intPicWidth As Integer Dim intPicHeight As Integer Private Sub Command1_Click() '============================= Dim pct As Picturebox Set pct = Me.Controls.Add("VB.Picturebox", "pctTemp") pct.Autosize = Tue pct.Picture = LoadPicture(App.Path & "\my_image.bmp") intPicWidth = pct.ScaleWidth intPicHeight = pct.ScaleHeight Me.Controls.Remove "pctTemp" Set pct = Nothing End Sub