【问题标题】:Load Picture error for JPEGJPEG 的加载图片错误
【发布时间】:2017-07-03 01:45:25
【问题描述】:

已经有很多关于这个错误的帖子,但我似乎找不到解决方案;当以编程方式(使用LoadPicture() 函数)将图片加载到用户窗体上的图像控件时,出现此错误:

运行时错误 481 - 无效图片

和类似的

图片无效

手动加载时的消息。

从我的互联网搜索中,我发现了很多关于这个错误的建议;大多数帖子往往是因为user is uploading a png 或其他无效图像(such as a corrupt jpg),some suggest 我的temp 文件夹已满,others think 病毒是原因(我怀疑最后一个) .

在我的应用程序中,我的图片是

  • 一张jpg(MSDN lists as an acceptable format在他们LoadPicture()文章的备注部分)

  • 没有损坏(至少我可以用windows photoviewer/Chrome/Paint正常查看)

  • 相对较小(例如,它是 ~1MPx 和我测试加载的 200MPx jpeg,没有任何错误)

唯一有点不寻常的是,我是使用以下代码直接从网上下载的:

Public Declare Function URLDownloadToFile Lib "urlmon" _
    Alias "URLDownloadToFileA" _
    (ByVal pCaller As Long, _
    ByVal szURL As String, _
    ByVal szFileName As String, _
    ByVal dwReserved As Long, _
    ByVal lpfnCB As Long) As Long 'api to download files

Sub setPicTest()
    Dim tmpImg As MSForms.Image 'create an image control on UserForm1 at runtime
    Set tmpImg = UserForm1.Controls.Add("Forms.Image.1")
    tmpImg.Picture = getImg("https://s-media-cache-ak0.pinimg.com/originals/22/e2/4d/22e24d3b5703a1c1cc43df5b13f53fd2.png")
End Sub

Function getImg(url As String) As IPictureDisp 'loads a picture from a url
    Dim strFile As String 'file save location
    strFile = Environ("Temp") & "\Temp.jpg" 'save *as jpeg* in %temp% folder
    Debug.Print URLDownloadToFile(0, url, strFile, 0, 0) 'download file to temp folder
    Set getImg = LoadPicture(strFile) 'load image -error-
    Kill strFile 'clean up temp file
End Function

当我逐步完成时,一切都按预期运行

  • 一个空的(没有图片)图像控件出现在我的用户窗体上
  • 一个名为temp.jpg 的文件出现在我的临时文件夹中
    • 此文件似乎没有损坏

但随后代码执行会因错误而中断。这尤其令人惊讶,因为代码对于小缩略图图像一直运行良好,只是这些全分辨率图像似乎不起作用。

【问题讨论】:

  • 这是一个png文件。将其重命名为 jpg 将无济于事:)

标签: vba image excel userform


【解决方案1】:

这是一个png文件。将其重命名为 jpg 将无济于事。 URLDownloadToFile 下载文件。它不会改变文件类型。

话虽如此,这是实现您想要的一种方法;)

逻辑

  1. 插入临时工作表
  2. 将图像直接插入工作表中
  3. 插入图表
  4. 将图像复制到图表并导出为.Jpg
  5. 使用LoadPicture加载图像
  6. 删除对象和我们创建的临时文件。

代码

Sub setPicTest()
    Dim wsTemp As Worksheet
    Dim tmpImg As MSForms.Image
    Dim PicPath As String, tmpPath As String
    Dim oCht As Chart

    Set tmpImg = UserForm1.Controls.Add("Forms.Image.1")

    Set wsTemp = ThisWorkbook.Sheets.Add

    '~~> This is the .PNG image
    PicPath = "https://s-media-cache-ak0.pinimg.com/originals/22/e2/4d/22e24d3b5703a1c1cc43df5b13f53fd2.png"
    '~~> This will be the .JPG image
    tmpPath = Environ("Temp") & "\Temp.jpg"

    With wsTemp
        .Pictures.Insert(PicPath).ShapeRange.LockAspectRatio = msoTrue
        DoEvents
        Set oCht = Charts.Add
        .Shapes(1).CopyPicture xlScreen, xlBitmap

        With oCht
            .Paste
            .Export Filename:=tmpPath, Filtername:="JPG"
        End With
        DoEvents

        tmpImg.Picture = LoadPicture(tmpPath)

        '~~> Clean Up
        On Error Resume Next
        Application.DisplayAlerts = False
        .Delete
        oCht.Delete
        Application.DisplayAlerts = True
        On Error GoTo 0
    End With

    '~~> Delete the temp image
    Kill tmpPath
End Sub

编辑

出于测试目的,我使用了这些设置

Set tmpImg = UserForm1.Controls.Add("Forms.Image.1")

With tmpImg
    .Left = 20        '~~> This property does not shown in Intellisense
    .Width = 300      '~~> This property does not shown in Intellisense
    .Height = 300     '~~> This property does not shown in Intellisense
    .Top = 10         '~~> This property does not shown in Intellisense

    .PictureAlignment = fmPictureAlignmentCenter
    .PictureSizeMode = fmPictureSizeModeStretch
End With

截图

【讨论】:

  • 这是一种比我见过的一些API approaches 更简单(或至少更容易遵循)的方法。我知道我哪里出错了,我想我试图避免使用内置 LoadPicture() 以外的任何东西,我只是假设因为文件 显示,它已成功转换为 jpg (不只是重命名)。我还发现了一种可能更直接的方法(但兼容性较差/可能比您的执行时间更长)
【解决方案2】:

正如@Siddharth Rout 指出的那样,即使在 Windows 资源管理器中我下载的文件看起来像 jpeg,它确实仍然是导致错误的 png。相信我,我的实际代码比我在此处发布的要清晰得多,因此文件的 png 扩展名被隐藏并且更难发现!

无论如何,他为将 png 放入图像控件提供了一个答案。我要使用的另一个选择是:

Function getImg(url As String) As IPictureDisp 'loads a picture from a url
    Dim strFile As String 'file save location
    strFile = Environ("Temp") & "\Temp.png" 'save with more extensions like png in %temp% folder
    Debug.Print URLDownloadToFile(0, url, strFile, 0, 0) 'download file to temp folder
    With New WIA.ImageFile
        .LoadFile strFile
        Set getImgPng = .FileData.Picture 'return same thing as LoadPicture() would
    End With
    Kill strFile 'clean up temp file
End Function

WIA.ImageFile 对象需要对 Windows Vista 或更高版本中可用的 Microsoft Windows Image Acquisition Library v2.0 的引用。

【讨论】:

    猜你喜欢
    • 2022-01-26
    • 1970-01-01
    • 2020-09-03
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    相关资源
    最近更新 更多