在 VBA 中获取图像大小?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12282753/
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 17:35:57  来源:igfitidea点击:

Getting image size in VBA?

imagevbaexcel-vbaword-vbaexcel

提问by Austin R

In a word document that I've been building some VBA code for, I need to be able to display images on a userform, and also resize the userform to fit whatever image it's presenting.

在我一直在为其构建一些 VBA 代码的 Word 文档中,我需要能够在用户表单上显示图像,并且还需要调整用户表单的大小以适合它呈现的任何图像。

I was very confused when I tried something like this in the Immediate Box (the image file is actually only 675 pixels in width):

当我在 Immediate Box(图像文件实际上只有 675 像素宽)中尝试这样的操作时,我感到非常困惑:

? LoadPicture("C:\Users\arose\Desktop\Security Control Doc\Images\AC1.bmp").Width
  17859 

Eventually I discovered that this is due to VBA using some unit other than pixels. Twips, or something like that. But I really need to have image sizes in pixels, so I mucked around, found roughly what the conversion factor is, and I'm using this:

最终我发现这是由于 VBA 使用了像素以外的其他单位。Twips,或类似的东西。但我真的需要以像素为单位的图像大小,所以我乱七八糟,大致找到了转换因子是什么,我正在使用这个:

Function TwipsToPixels(Twips As Long) As Long

    TwipsToPixels = Twips / 25.477

End Function

This has helped, but it is not particularly accurate. So my question is, does anyone have a more elegant and precise way of getting image sizes in VBA?

这有帮助,但它不是特别准确。所以我的问题是,有没有人有更优雅、更精确的方法来在 VBA 中获取图像大小?

回答by vulkanino

The default unit of measurement in VBA is twips: screen-independent unit of measurement that is equal to 1/20 of a point, or 1/1,440 of an inch. There are 567 twips in a centimeter.

VBA 中的默认测量单位是:与屏幕无关的测量单位,等于点的 1/20 或 1/1,440 英寸。一厘米有 567 缇。

Pixels are screen dependent units, so the two functions/properties TwipsPerPixelXand TwipsPerPixelYshould do the trick.

像素是屏幕相关的单位,所以这两个功能/性能TwipsPerPixelXTwipsPerPixelY应该做的伎俩。