在 VBA 中缩放图片
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18258008/
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
Scale a picture in VBA
提问by Patrick Wilson
Currently I am trying to scale a picture in VBA, but cannot seem to get what I need. Every time I run though....
目前我正在尝试在 VBA 中缩放图片,但似乎无法得到我需要的东西。每次我跑虽然....
ActiveSheet.Pictures.Insert("C:\\Logo.bmp").Select
With Selection
.ShapeRange.ScaleWidth 1.4, msoTrue
.ShapeRange.ScaleHeight 0.5, msoFalse
End With
It will scale to the correct width initially, but then when I go to the next line and try to scale the height it changes the width. Can someone help me understand why this happens and suggest a better way of scaling the picture. I need it to be about 125% bigger in length and about 50% smaller in height.
它最初会缩放到正确的宽度,但是当我转到下一行并尝试缩放高度时,它会更改宽度。有人可以帮助我理解为什么会发生这种情况并提出一种更好的缩放图片的方法。我需要它的长度增加约 125%,高度减少约 50%。
Thanks so much.
非常感谢。
回答by varocarbas
Include this line:
包括这一行:
.ShapeRange.LockAspectRatio = msoFalse
To allow both width and height being "unlocked", put it right on the top.
要允许“解锁”宽度和高度,请将其放在顶部。
With Selection
.ShapeRange.LockAspectRatio = msoFalse
.ShapeRange.ScaleWidth 1.4, msoTrue
.ShapeRange.ScaleHeight 0.5, msoFalse
End With
回答by tigeravatar
In the ShapeRange.ScaleWidth and ScaleHeight methods, the RelativeToOriginalSize argument should be msoFalseif you wantto scale it relatively (as in maintain aspect ratio, it will adjust width and height while scaling). So if you don't wantit to scale relatively, just set that argument to msoTruefor both:
在 ShapeRange.ScaleWidth 和 ScaleHeight 方法中,如果你想相对缩放它,RelativeToOriginalSize 参数应该是msoFalse(因为在保持纵横比时,它会在缩放时调整宽度和高度)。因此,如果您不希望它相对缩放,只需将该参数设置为msoTrue 即可:
ActiveSheet.Pictures.Insert("C:\\Logo.bmp").Select
With Selection
.ShapeRange.ScaleWidth 1.4, msoTrue
.ShapeRange.ScaleHeight 0.5, msoTrue
End With