usetheforce2
03-01-2002, 08:18 PM
hey,
A request came up in the forum for an example that would stretch an image to fit a picture box. I really don't understant why the good old boys at microsoft would included the property for the pictrue box, as the did for the image control?
its fairly simple. In order for this to work you will need to place a picture box on a form and paste the following code in.
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
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 LR_LOADFROMFILE = &H10
Private Sub Load_Image(ByVal pic As PictureBox, ByVal fName As String)
Dim pWidth As Long
Dim pHeight As Long
Dim hWnd As Long
' make sure pic is set to autoredraw
If Not pic.AutoRedraw Then pic.AutoRedraw = True
' get the picture box dimensions in pixels
pWidth = pic.ScaleX(pic.ScaleWidth, pic.ScaleMode, vbPixels)
pHeight = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, vbPixels)
' load picture
hWnd = LoadImage(0, fName, 0, pWidth, pHeight, LR_LOADFROMFILE)
' select the image into the picture box
SelectObject pic.hdc, hWnd
'clean up
DeleteObject hWnd
pic.Refresh
End Sub
Private Sub Form_Load()
' just call the sub and pass the name of the picture box and
' the file path
Load_Image Picture1, App.Path & "\testImage.bmp"
End Sub
This will work a variety of image formats:
Bitmap: .bmp
Icon: .ico
Cursor: .cur
Windows metafile: .wmf
Enhanced metafile: .emf
Gif (graphics interchange fomart): .gif
JPEG (joint Photographic Experts Group): .jpg, .jpeg
enjoy!
Regan
A request came up in the forum for an example that would stretch an image to fit a picture box. I really don't understant why the good old boys at microsoft would included the property for the pictrue box, as the did for the image control?
its fairly simple. In order for this to work you will need to place a picture box on a form and paste the following code in.
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, _
ByVal hObject As Long) As Long
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 LR_LOADFROMFILE = &H10
Private Sub Load_Image(ByVal pic As PictureBox, ByVal fName As String)
Dim pWidth As Long
Dim pHeight As Long
Dim hWnd As Long
' make sure pic is set to autoredraw
If Not pic.AutoRedraw Then pic.AutoRedraw = True
' get the picture box dimensions in pixels
pWidth = pic.ScaleX(pic.ScaleWidth, pic.ScaleMode, vbPixels)
pHeight = pic.ScaleY(pic.ScaleHeight, pic.ScaleMode, vbPixels)
' load picture
hWnd = LoadImage(0, fName, 0, pWidth, pHeight, LR_LOADFROMFILE)
' select the image into the picture box
SelectObject pic.hdc, hWnd
'clean up
DeleteObject hWnd
pic.Refresh
End Sub
Private Sub Form_Load()
' just call the sub and pass the name of the picture box and
' the file path
Load_Image Picture1, App.Path & "\testImage.bmp"
End Sub
This will work a variety of image formats:
Bitmap: .bmp
Icon: .ico
Cursor: .cur
Windows metafile: .wmf
Enhanced metafile: .emf
Gif (graphics interchange fomart): .gif
JPEG (joint Photographic Experts Group): .jpg, .jpeg
enjoy!
Regan