Image VBA

benkingery

Registered User.
Local time
Today, 02:30
Joined
Jul 15, 2008
Messages
153
I have a module like this:

Code:
Public Function DisplayImage(ctlBrowserControl As Control, _
strImagePath As Variant)

On Error GoTo Err_DisplayImage

Dim strDatabasePath As String
Dim intSlashLocation As Integer

With ctlBrowserControl
    If IsNull(strImagePath) Then
    ElseIf Left(strImagePath, 4) = "http" Then
        .Navigate (strImagePath)
    Else
        If InStr(1, strImagePath, "\") = 0 Then
            ' Path is relative
            strDatabasePath = CurrentProject.FullName
            intSlashLocation = InStrRev(strDatabasePath, "\", Len(strDatabasePath))
            strDatabasePath = Left(strDatabasePath, intSlashLocation)
            strImagePath = strDatabasePath & strImagePath
        End If
        .Navigate (strImagePath)
    End If
End With
    
Exit_DisplayImage:
    Exit Function

Err_DisplayImage:
    Select Case Err.Number
        Case Else
            MsgBox Err.Number & " " & Err.Description
            Resume Exit_DisplayImage:
    End Select
End Function

I also call this module from my form to display an HTTP image:

Code:
Private Sub Form_AfterUpdate()
    CallDisplayImage
End Sub

Private Sub Form_Current()
    CallDisplayImage
End Sub

Private Sub txtImageName_AfterUpdate()
    CallDisplayImage
End Sub

Private Sub CallDisplayImage()
    DisplayImage Me.WebBrowser, Me.txtImageName
End Sub


This works perfectly to display the image I'd like through WebBrowser control I put onto the form....HOWEVER --and here's my question--the image size is not bound to the size bounds of the control size itself.

So if my control is 2 inches by 2 inches and the image is 16 inches by 16 inches, then the image doesn't fully show within the control.

I'd like to get the control to show the whole image (sized down as appropiate, or sized up as appropriate) such that the whole image is contained within the control.

I know how to limit the display of any image in HTML, wondering if I can do something like this here.

Thanks in advance!
 
You can programmatically write an html page and load that in the browser.

And your code can refactor (excluding err handling) down to . . .
Code:
Public Sub DisplayImage(ctlBrowserControl As Control, strImagePath As Variant)

    If Not IsNull(strImagePath) Then
        If Left(strImagePath, 4) = "http" Then
            ctlBrowserControl.Navigate strImagePath
        ElseIf InStr(1, strImagePath, "\") = 0 Then
            ctlBrowserControl.Navigate CurrentProject.Path & "\" & strImagePath
        End If
    End If
    
End Sub
. . . because CurrentProject also exposes a .Path property, which you go through some trouble to extract from CurrentProject.Fullname.
 

Users who are viewing this thread

Back
Top Bottom