2017-07-02 44 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中可用或更高版本的參考。