excel - How to fit SVG picture inside fixed area in VBA? - Stack Overflow

You will see that SVG picture doesnt fit inside fixed area when you run following code.Public Sub Macr

You will see that SVG picture doesnt fit inside fixed area when you run following code.

Public Sub Macro1()

'Declaration
Dim Picture1 As String
Picture1 = ".svg"

'Add a picture
With ActiveSheet.Shapes.AddPicture(Filename:=Picture1, LinkToFile:=False, SaveWithDocument:=True, Left:=50, Top:=50, Width:=150, Height:=100)
    .Name = "myPicture"
End With

End Sub

Please note that Width:=150, Height:=100 are changeless.

I want the SVG picture fits inside Width:=150, Height:=100.

Big picture must shrink (zoom) in order to fit inside fixed area.

Please note that there is no problem if I use the following SVG picture.

.svg

You will see that SVG picture doesnt fit inside fixed area when you run following code.

Public Sub Macro1()

'Declaration
Dim Picture1 As String
Picture1 = "https://upload.wikimedia./wikipedia/commons/4/43/Flag_of_Mauritania.svg"

'Add a picture
With ActiveSheet.Shapes.AddPicture(Filename:=Picture1, LinkToFile:=False, SaveWithDocument:=True, Left:=50, Top:=50, Width:=150, Height:=100)
    .Name = "myPicture"
End With

End Sub

Please note that Width:=150, Height:=100 are changeless.

I want the SVG picture fits inside Width:=150, Height:=100.

Big picture must shrink (zoom) in order to fit inside fixed area.

Please note that there is no problem if I use the following SVG picture.

https://upload.wikimedia./wikipedia/en/b/ba/Flag_of_Germany.svg
Share Improve this question edited Mar 29 at 12:59 Danny Coleiro asked Mar 26 at 9:15 Danny ColeiroDanny Coleiro 1811 silver badge9 bronze badges 5
  • What do you mean with does not fit? You mean the proportion? – Foxfire And Burns And Burns Commented Mar 26 at 9:35
  • To place the upper left corner of the flag, e.g. in cell B10, use the code: Left:=Range("B10").Left, Top:=Range("B10").Top – MGonet Commented Mar 26 at 9:41
  • 1 That's odd - it's only importing what looks like a quarter of the image. Changing the dimensions doesn't change that and neither does using Pictures.Insert but using a different picture does display the whole image - tpc.googlesyndication/simgad/14797135765315967065 opened an SO advert image fine. – Darren Bartrup-Cook Commented Mar 26 at 10:11
  • 1 I'm not up on the image types, saving the svg to my pictures folder still only shows a quarter of the image, but saving it as a png type then shows the whole image. – Darren Bartrup-Cook Commented Mar 26 at 10:18
  • 1 @DarrenBartrup-Cook There is no problem if I use the following picture. upload.wikimedia./wikipedia/en/b/ba/Flag_of_Germany.svg – Danny Coleiro Commented Mar 26 at 10:19
Add a comment  | 

1 Answer 1

Reset to default 7

Not really an answer, but this is what I found.

I downloaded both image files locally and inserted them using Insert-Picture and also via code, both with Shapes.AddPicture (as you do) and with Pictures.Insert (as the macro recorder suggested), but no matter what I did, the flag of Mauritania was loaded crippled. Playing with size and with crop properties didn't change that, it seems that Excel loaded only a part. No such issues with the flag of Germany.

As SVG is not a binary file format, you can open the file with a text editor. This is how it looks for the flag of Mauritania:

<svg xmlns="http://www.w3./2000/svg" width="1200" height="800" viewBox=" 0 0 3000 2000">
<path fill="#d01c1f" d="M0 0H3000V2000H0z"/>
<path fill="#00a95c" d="M0 400H3000V1600H0z"/>
<path fill="#ffd700" d="M1299 744h153l48-144 48 144h153l-126 92 51 146-126-90-126 90 51-146zM750 670a 760.092776 628 0 0 0 1500 0 750 730 0 0 1-1500 0z"/></svg>

You can see that there are 3 path fill tags, the first paints the full image red, the second paints the green rectangle and the third draws a golden star and moon. However, that's not the important part.

The viewBox is defining a rectangle as canvas. The numbers define a a coordinate system (3000 x 2000), not a size. The following commands are painting on that canvas.

The width and height properties define the output size of the image, but note that you can zoom in and out SVG files without quality loss as it doesn't contain pixels but commands to draw something.

However it seems that Excel doesn't interpret the width and height values correctly: Instead of zooming the 3000x2000 down to 1200x800, it takes only the first 1200x800 "pixels". When you modify width and height to 3000 and 2000 and save the file, Excel imports it correctly.

<svg xmlns="http://www.w3./2000/svg" width="3000" height="2000" viewBox=" 0 0 3000 2000">

In the image file for the german flag, the viewBox is smaller than the width and height, and here it seems Excel has no problem to handle the zooming.

<svg xmlns="http://www.w3./2000/svg" width="1000" height="600" viewBox="0 0 5 3">
    <desc>Flag of Germany</desc>
    <rect id="black_stripe" width="5" height="3" y="0" x="0" fill="#000"/>
    <rect id="red_stripe" width="5" height="2" y="1" x="0" fill="#D00"/>
    <rect id="gold_stripe" width="5" height="1" y="2" x="0" fill="#FFCE00"/>
</svg>

Best advice I could give you is not to use SVG files, but bitmap file formats like PNG and JPG.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744156074a4560867.html

相关推荐

  • excel - How to fit SVG picture inside fixed area in VBA? - Stack Overflow

    You will see that SVG picture doesnt fit inside fixed area when you run following code.Public Sub Macr

    8天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信