我在嘗試獲取 MS Access 表單中的 Web 瀏覽器控制元件以正確顯示來自 URL 的影像時遇到問題。
我正在使用 .navigate (strImagePath) 將影像放入 WebBrowser1。這很好用。影像是 jpg,我有完整的路徑和影像檔案名。
問題是影像以 100% 的比例顯示,這大于瀏覽器的大小。我可以使用縮放 (OLECMDID_OPTICAL_ZOOM) 來縮放影像,但這只有在我知道影像的大小時才有效,而我無法獲得正確的縮放系數。
理想情況下,我希望影像適合視窗而無需確定影像大小。
這是不可能的,另一種選擇是確定影像大小,然后設定適當的縮放比例。我還沒有想出一種方法來確定影像大小而不將其保存在本地。這將是一個很大的開銷,并且會為表單顯示添加不可接受的延遲,特別是在瀏覽記錄時。這里有什么理想嗎?
謝謝
uj5u.com熱心網友回復:
我能夠按照 Thomas 的代碼運行它。
有最終代碼。
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
在那里呼叫函式
Me!txtFaceResults = DisplayImage(Me!WebBrowser1, strFullImageName)
雖然它可以將影像縮放到統一大小,但縮放仍然關閉。但是將影像加載到 ActiveX Web 瀏覽器物件的一般程序是有效的。讀取影像大小并計算縮放系數,然后應用縮放。等待影像加載很重要,否則您將收到錯誤或之前加載的影像的大小。
我不能像 Thomas 那樣使用 HtmlImg 型別,所以我單獨獲取了元素。我使用了 Mozilla 上的檔案:https : //developer.mozilla.org/en-US/docs/Web/API/Element 來查看可用的元素。
uj5u.com熱心網友回復:
如果您只加載了影像,以下代碼將為您提供該影像的寬度和高度。
Dim img As HtmlImg
Set img = Me.WebBrowser1.Document.images(0)
Debug.Print img.naturalHeight, img.naturalWidth
但是,我無法真正計算出正確的縮放系數,也無法將可變縮放系數傳遞給ExecWB-method - 我總是遇到運行時錯誤,無論我使用的是哪種資料型別(常量值正在作業)。
另外,至少在我的測驗中,影像顯示有(白色)邊框,這也會影響縮放系數。無論如何,寬度和高度正是我正在使用的測驗影像的尺寸。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/317177.html
上一篇:鞍山市的搜索起點
下一篇:需要Sum查詢以包含零總數
