CodeItBetter Programming Another VB Programming Blog

How to stretch image in a Picture Box Control

Posted on January 5, 2009
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
'Controls - How to stretch image in a Picture Box Control
Option Explicit
 
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
 
'How can I call this function:
Private Sub Form_Load()
    Call Load_Image(Picture1, App.Path & "\Image.bmp")
End Sub
Filed under: Controls Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


 

No trackbacks yet.