vba LoadPicture 错误无效图片

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18661681/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 23:11:52  来源:igfitidea点击:

LoadPicture error invalid picture

vbapowerpoint-vbaloadimage

提问by Iban Arriola

I am trying to take an image from a folder to check its width after that. To do it I am using the following peace of code:

我正在尝试从文件夹中获取图像以检查其宽度。为此,我使用以下代码和平:

         Dim pic As IPictureDisp
         Dim var As Variant
         var = "C:\Myfolder\Animage" & animationNum + 1 & ".png"
         pic = LoadPicture(var)

It is giving me "invalid picture" error. I tried also just using the following line:

它给了我“无效图片”错误。我也试过只使用以下行:

        width = LoadPicture("C:\Myfolder\Animage" & animationNum + 1 & ".png").width

But it also gave me the same error. How can I load the picture?

但它也给了我同样的错误。我怎样才能加载图片?

EDIT

编辑

I try with a jpg image and it works... Does this function some issues with png files?

我尝试使用 jpg 图像,它可以工作...此功能是否与 png 文件有关?

采纳答案by Aaron Thomas

From msdn.microsoft.com:

msdn.microsoft.com

Graphics formats recognized by Visual Basic include bitmap (.bmp) files, icon (.ico) files, cursor (.cur) files, run-length encoded (.rle) files, metafile (.wmf) files, enhanced metafiles (.emf), GIF (.gif) files, and JPEG (.jpg) files.

Visual Basic 识别的图形格式包括位图 (.bmp) 文件、图标 (.ico) 文件、光标 (.cur) 文件、运行长度编码 (.rle) 文件、图元文件 (.wmf) 文件、增强图元文件 (.emf) )、GIF (.gif) 文件和 JPEG (.jpg) 文件。

.pngis not supported.

不支持.png

回答by Frzorp

I was searching for this exact issue and found a solution on another site, so I thought I would share it here.

我正在寻找这个确切的问题并在另一个网站上找到了解决方案,所以我想我会在这里分享。

I did not come up with this code, it is from Experts Exchange here: http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26980514.html

我没有想出这个代码,它来自这里的专家交流:http: //www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/Excel/Q_26980514.html

If you import the .basfile you can use LoadPictureGDI()in place of LoadPicture()which converts the picture prior to loading it into your userform or sheet.

如果导入的.bas文件,你可以使用LoadPictureGDI()代替LoadPicture()它之前加载到您的用户窗体或表的图片转换。

回答by FarkhemHall

LoadPicture command does not seem to like filenames greater than 35 characters in length when operating in later versions of Office. I can load a picture to a userform in Windows 7 Office 2010 and it seems to handle any length of filename, but once you move to Windows 10 / Office 2013 onwards if you go over the 35 character length the Err.Description simply says: "Path/Access error".

在更高版本的 Office 中操作时,LoadPicture 命令似乎不喜欢长度超过 35 个字符的文件名。我可以将图片加载到 Windows 7 Office 2010 中的用户表单,它似乎可以处理任何长度的文件名,但是一旦您移动到 ​​Windows 10/Office 2013 以后,如果超过 35 个字符的长度,Err.Description 只会说:“路径/访问错误”。

Limit your filename to 35 chars and you should be fine.

将您的文件名限制为 35 个字符,您应该没问题。