加载 JPEG 图片错误
Load Picture error for JPEG
已经有很多关于此错误的帖子,但我似乎找不到解决方案;当以编程方式(使用 LoadPicture()
函数)将图片加载到用户窗体上的图像控件时,出现此错误:
Run-time error 481 - Invalid picture
和类似的
Invalid picture
手动加载时的消息。
从我的互联网搜索中,我发现了很多关于这个错误的建议;大多数帖子往往是因为 user is uploading a png or other invalid image (such as a corrupt jpg), some suggest that my temp
folder is full, others think 病毒是原因(我对最后一个持怀疑态度)。
在我的申请中,我的图片是
一张 jpg(MSDN lists as an acceptable format 在他们 LoadPicture()
文章的备注部分)
没有损坏(至少我可以用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
当我逐步执行时,一切都按预期运行
- 我的用户窗体上出现一个空的(无图片)图像控件
- 一个名为
temp.jpg
的文件出现在我的临时文件夹中
- 此文件似乎没有损坏
但是随后代码执行因错误而中断。这尤其令人惊讶,因为代码对于小缩略图一直运行良好,只是这些全分辨率图像似乎不起作用。
这是一个png文件。将其重命名为 jpg 将无济于事。 URLDownloadToFile
下载一个文件。它不会更改文件类型。
话虽如此,这是实现您想要的一种方法 ;)
逻辑:
- 插入临时工作表
- 将图像直接插入工作表
- 插入图表
- 将图像复制到图表并将其导出为
.Jpg
- 使用
LoadPicture
加载图片
- 删除我们创建的对象和临时文件。
代码
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
截图
正如@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
对象需要对 Microsoft Windows Image Acquisition Library v2.0
的引用,后者在 Windows Vista 或更新版本中可用。
已经有很多关于此错误的帖子,但我似乎找不到解决方案;当以编程方式(使用 LoadPicture()
函数)将图片加载到用户窗体上的图像控件时,出现此错误:
Run-time error 481 - Invalid picture
和类似的
Invalid picture
手动加载时的消息。
从我的互联网搜索中,我发现了很多关于这个错误的建议;大多数帖子往往是因为 user is uploading a png or other invalid image (such as a corrupt jpg), some suggest that my temp
folder is full, others think 病毒是原因(我对最后一个持怀疑态度)。
在我的申请中,我的图片是
一张 jpg(MSDN lists as an acceptable format 在他们
LoadPicture()
文章的备注部分)没有损坏(至少我可以用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
当我逐步执行时,一切都按预期运行
- 我的用户窗体上出现一个空的(无图片)图像控件
- 一个名为
temp.jpg
的文件出现在我的临时文件夹中- 此文件似乎没有损坏
但是随后代码执行因错误而中断。这尤其令人惊讶,因为代码对于小缩略图一直运行良好,只是这些全分辨率图像似乎不起作用。
这是一个png文件。将其重命名为 jpg 将无济于事。 URLDownloadToFile
下载一个文件。它不会更改文件类型。
话虽如此,这是实现您想要的一种方法 ;)
逻辑:
- 插入临时工作表
- 将图像直接插入工作表
- 插入图表
- 将图像复制到图表并将其导出为
.Jpg
- 使用
LoadPicture
加载图片
- 删除我们创建的对象和临时文件。
代码
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
截图
正如@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
对象需要对 Microsoft Windows Image Acquisition Library v2.0
的引用,后者在 Windows Vista 或更新版本中可用。