【问题标题】:Scaling Imaging from URL using Web Broswer Control使用 Web 浏览器控件从 URL 缩放图像
【发布时间】:2021-11-21 14:21:18
【问题描述】:

我在尝试在 MS Access 表单中获取 Web 浏览器控件以正确显示来自 URL 的图像时遇到问题。

我正在使用 .navigate (strImagePath) 将图像放入 WebBrowser1。这很好用。图片是 jpg,我有完整的路径和图片文件名。

问题是图像以 100% 的比例显示,大于浏览器的大小。我可以使用缩放 (OLECMDID_OPTICAL_ZOOM) 来缩放图像,但这只有在我知道图像大小的情况下才有效,我无法获得正确的缩放系数。

理想情况下,我希望图像适合窗口,而无需确定图像大小。

这是不可能的,另一种选择是确定图像大小,然后设置适当的缩放。我还没有想出一种方法来确定图像大小而不将其保存在本地。这将是一个很大的开销,并且会给表单显示添加不可接受的延迟,特别是在浏览记录时。这里有什么理想吗?

谢谢

【问题讨论】:

  • 如果你只是从网络上拉图片,你可能会用这里描述的方法更好:Display pictures directly from an URL
  • 古斯塔夫,感谢您的建议。正如我所提到的,我试图避免下载到本地文件。阅读 API 的说明,文件被下载到本地缓存,然后允许将文件作为本地文件加载。我正在寻找一种直接的方法。

标签: vba ms-access webbrowser-control


【解决方案1】:

我能够在 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 Browser 对象的一般过程是有效的。读取图像大小并计算缩放因子,然后应用缩放。等待图片加载很重要,否则您将收到错误或之前加载的图片的大小。

我无法像 Thomas 那样使用 HtmlImg 类型,所以我单独获取了元素。我在 Mozilla 上使用了以下文档: https://developer.mozilla.org/en-US/docs/Web/API/Element 查看可用的元素。

【讨论】:

    【解决方案2】:

    如果您只加载了图像,以下代码将为您获取该图像的宽度和高度。

    Dim img As HtmlImg
    Set img = Me.WebBrowser1.Document.images(0)
    Debug.Print img.naturalHeight, img.naturalWidth
    

    但是,我无法计算出正确的缩放系数,也无法将可变缩放系数传递给 ExecWB-方法 - 无论我使用哪种数据类型,我总是遇到运行时错误(恒定值有效)。

    另外,至少在我的测试中,图像显示有(白色)边框,这也会影响缩放系数。无论如何,宽度和高度正是我正在玩的测试图像的尺寸。

    【讨论】:

    • 谢谢托马斯。我尝试使用您编写的内容,但对我来说 HtmlImg 不是定义类型。但我认为你在做某事。对 HtmlImg 有什么建议吗?
    • 抱歉,忘记提及您需要添加对 Microsoft HTML 对象库 的引用。或者您将变量声明为 Object(但您没有智能支持)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-18
    • 1970-01-01
    • 2011-03-31
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多