
Loaded image in Image or PictureBox control
In the Help file index, look up "Scrollable graphics viewport sample
application". Since the child picture box's (Picture2) AutoSize property is
set to True, you can read the size directly from the ScaleWidth and
ScaleHeight properties.
You can also independently examine a file's header to find the original
image size regardless of whether or not the image is even loaded:
Option Explicit
Private Type udtBmpInfo
identifier As String * 2
filesize As Long
reserved As Long
bmpdataoffset As Long
bmpheadersize As Long
bmpwidth As Long
bmpheight As Long
End Type
Private Type udtJPEGinfo
Width As Integer
Height As Integer
BitsPerPixel As Integer
ColorPlanes As Integer
JPEGTYPE As Integer
End Type
Private Type udtGIFInfo
Signature As String * 6
Width As Integer
Height As Integer
End Type
Private m_Width As Long
Private m_Height As Long
'=======================
Private Sub Command1_Click()
Dim strx As String
'File to examine
strx = "C:\pics\misc\test.gif" 'Use your own file
If GetImageSize(strx) Then
'Sucessful
strx = "Image is " & iWidth & " x " & iHeight & " pixels."
Else
'Error
strx = "Could not read the file,"
End If
MsgBox strx, vbInformation Or vbOKOnly
End Sub
Private Function GetImageSize(ByVal sFile As String) As Boolean
Dim ext As String
Dim BmpInfo As udtBmpInfo
Dim JPGinfo As udtJPEGinfo
Dim GifInfo As udtGIFInfo
Dim C1 As Integer, C2 As Integer, Marker As Integer
Dim Length As Long
Dim FileNum As Long
GetImageSize = False
'Type of File
ext = LCase(Right$(sFile, 3))
FileNum = FreeFile
On Error GoTo OpenError
Open sFile For Binary Access Read As FileNum
Select Case ext
'If successful, assign size to PublicProperty "iWidth", and
"iHeight"
Case "bmp"
Get FileNum, 1, BmpInfo
With BmpInfo
If .identifier = "BM" Then
'Valid Bitmap
Let iWidth = .bmpwidth
Let iHeight = .bmpheight
End If
End With
GetImageSize = True
Case "jpg", "jpe", "jpeg"
C1 = GetC(FileNum): C2 = GetC(FileNum)
If C1 = 255 And C2 = 216 Then
'Valid JPEG
Do
C1 = GetC(FileNum)
Marker = GetC(FileNum)
Select Case Marker
Case 192 To 195, 197 To 207
Length = GetInt(FileNum)
With JPGinfo
.BitsPerPixel = GetC(FileNum)
Let iHeight = GetInt(FileNum)
Let iWidth = GetInt(FileNum)
End With
Exit Do
Case 216 To 218, -1
Exit Do
Case Else
' skip field
Length = GetInt(FileNum)
If Length < 2 Then Exit Do 'error
Seek #FileNum, Seek(FileNum) + Length - 2
End Select
Loop
GetImageSize = True
End If
Case "gif"
Get FileNum, , GifInfo
With GifInfo
If .Signature = "GIF87a" Or .Signature = "GIF89a" Then
'Valid GIF
Let iWidth = .Width
Let iHeight = .Height
GetImageSize = True
End If
End With
End Select
Close FileNum
Exit Function
OpenError:
Close FileNum
End Function
Private Function GetC(ByVal FileNum As Long) As Long
Dim strx As String * 1
Get #FileNum, , strx
GetC = Asc(strx)
End Function
Private Function GetInt(ByVal FileNum As Long) As Long
Dim C1 As Integer
Dim C2 As Integer
C1 = GetC(FileNum)
C2 = GetC(FileNum)
GetInt = CLng(C1) * 256 + C2
End Function
Public Property Get iWidth() As Long
iWidth = m_Width
End Property
Public Property Let iWidth(ByVal NewValue As Long)
m_Width = NewValue
End Property
Public Property Get iHeight() As Long
iHeight = m_Height
End Property
Public Property Let iHeight(ByVal NewValue As Long)
m_Height = NewValue
End Property
Quote:
> Hello,
> If I load an image to either PictureBox or Image control, how can I get
> that image's width and height in pixels?
> Another question. Is it possible anyhow to add scrollbars to the
> mentioned controls, so the user could scroll the picture larger than the
> control's size, to see it all?
> Thanks
> --