I am having a problem trying to get a Web Browser control in a MS Access form to display an image from a URL correctly.
I am using .navigate (strImagePath) to place the imaging in to WebBrowser1. That works fine. The images are jpg and I have the full path and image filename.
The problem is the that the image is displayed at 100% scale, which is larger than the size of the browser. I can use zoom (OLECMDID_OPTICAL_ZOOM) to scale the image, but this only works if I know the size of the image, which I don't to get the right zoom factor.
Ideally, I would like to have the image fit to window without having to determine the image size.
It that is not possible, the other option is to determine the image size and then set the appropriate zoom. I have not figured a way to determine the image size without saving it locally. Which would be a big overhead and add a unacceptable lag to the form display, specially when move through records. Any ideals here?
Thanks
CodePudding user response:
I was able to get it running after the code from Thomas.
There is the final code.
Public Function DisplayImage(ctlImageControl As Control, strImagePath As Variant) As String
On Error GoTo Err_DisplayImage
Dim strResult As String
Dim intHeight As Integer
Dim intWidth As Integer
Dim intHeightZoom As Integer
Dim intWidthZoom As Integer
Dim intPageZoom As Integer
With ctlImageControl
If strImagePath = "" Then
.Visible = False
strResult = "No image name specified."
Else
.Visible = True
.Navigate (strImagePath)
Do While .Object.Busy Or .Object.ReadyState <> 4 'wait to imaged loaded
DoEvents
Loop
intHeight = .Document.images(0).clientHeight
intWidth = .Document.images(0).clientWidth
intHeightZoom = CLng(21600) / intHeight
intWidthZoom = CLng(43200) / intWidth
If intHeightZoom < intWidthZoom Then
intPageZoom = intHeightZoom
Else
intPageZoom = intWidthZoom
End If
.Object.ExecWB OLECMDID_OPTICAL_ZOOM, OLECMDEXECOPT_DONTPROMPTUSER, _
CLng(intPageZoom), vbNull
strResult = "success"
End If
End With
Exit_DisplayImage:
DisplayImage = strResult
Exit Function
Err_DisplayImage:
Select Case Err.Number
Case 2220 ' Can't find the picture.
ctlImageControl.Visible = False
strResult = "Can't find image in the specified name."
Resume Exit_DisplayImage:
Case Else ' Some other error.
MsgBox Err.Number & " " & Err.Description
strResult = "An error occurred displaying image."
Resume Exit_DisplayImage:
End Select
End Function
There the function is called by
Me!txtFaceResults = DisplayImage(Me!WebBrowser1, strFullImageName)
While it works to zoom the images to a uniform size, the scaling is still off. But the general process works of loading the images in to the ActiveX Web Browser object. Reads the image size and calculates a zoom factor and then apply the zoom. It is important to wait for the image to load, otherwise you will either get an error or the size of the previously loaded image.
I was not able to use HtmlImg Type as Thomas used, so I got the elements individually. I used the documentation on Mozilla at: https://developer.mozilla.org/en-US/docs/Web/API/Element to see the element available.
CodePudding user response:
If you have only the image loaded, the following code will get you the width and height of that image.
Dim img As HtmlImg
Set img = Me.WebBrowser1.Document.images(0)
Debug.Print img.naturalHeight, img.naturalWidth
However, I was not really able to calculate the correct zoom factor nor to pass a variable zoom factor to the ExecWB
-method - I always got a runtime error, not matter which data type I used (constant values are working).
Plus, at least in my tests, the image was displayed with (white) borders which also would influence the zoom factor. Anyhow, the width and height are exactly the dimensions of the test image I was playing with.