2017-07-02 100 views
2

这个错误已经有大量的帖子,但我似乎无法找到解决方案; (与LoadPicture()功能)编程加载图片上的用户窗体的图像控制当出现此错误:加载JPEG图片的错误

Run-time error 481 - Invalid picture

和类似

时,它的手动加载

Invalid picture

消息。

从我的网络搜索中,我发现了关于这个错误的一整套建议;大多数帖子往往是因为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 fine)

  • 相对较小(因为它是〜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的文件出现在我的临时文件夹中
    • 此文件不出现损坏

但随后的错误代码执行中断。这是特别令人惊讶的,因为代码对于小的缩略图图像一直工作正常,只是这些全分辨率图像似乎没有工作。

+1

这是一个PNG文件。将它重命名为jpg将无济于事:) –

回答

4

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

如此说来这里是为了实现你想要的一种方式;)

逻辑

  1. 插入一个临时表
  2. 插入图像直接到工作表
  3. 插入a图表
  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 

截图

![enter image description here

+0

这比一些[API方法]更简单(或至少更容易遵循)方法(http://www.vbforums.com/showthread.php?582741-RESOLVED-最小代码 - 绘制 - PNG-GDI&p = 3598346#post3598346)我见过。我看到我出错的地方,我想我试图避免使用内置的'LoadPicture()'以外的任何东西,我只是假设因为文件*显示*,它已被成功转换为jpg(而不仅仅是*更名为*)。我还发现可能有一个更直接的方法(但与您的执行时间不兼容/执行时间可能更长) – Greedo

0

由于@Siddharth溃败指出,即使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对象需要对Microsoft Windows Image Acquisition Library v2.0这是Windows Vista中可用或更高版本的参考。