JPEG

时间:2017-07-02 16:35:42

标签: vba image excel-vba userform excel

此错误已经发布了很多帖子,但我似乎无法找到解决方案;当以编程方式(使用LoadPicture()函数)将图片加载到用户窗体上的图像控件时,我收到此错误:

  

运行时错误481 - 图片无效

和类似的

  

图片无效

手动加载时的消息。

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

在我的申请中,我的照片是

  • jpg(LoadPicture()文章备注部分MSDN lists as an acceptable format

  • 没有损坏(至少我可以用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

当我单步执行时,一切都按预期运行

  • 我的UserForm
  • 上出现空(无图片)图像控件
  • 我的临时文件夹中出现了一个名为temp.jpg的文件
    • 此文件似乎未损坏

然后代码执行中断了错误。这一点特别令人惊讶,因为代码对于小缩略图图像工作正常,只是这些全分辨率图像似乎无法正常工作。

2 个答案:

答案 0 :(得分:4)

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

说过这是实现你想要的一种方式;)

<强>逻辑

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

    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
    

    <强>截图

    ![enter image description here

答案 1 :(得分:0)

正如@Siddharth Rout指出的那样,即使对于Windows资源管理器我下载的文件看起来像一个jpeg,它确实仍然是一个导致错误的png。相信我,当我说我的实际代码不如我在这里发布的那么清晰,所以文件的png扩展名被隐藏了,更难以发现!

无论如何,他提供了一个让png进入图像控件的答案。我要采取的另一个选择是:

  if (a==find) { r.getCell(i, j).setValue(repl);}

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 对象需要引用Windows Vista或更高版本中可用的Microsoft Windows Image Acquisition Library v2.0