PowerPoint 在使用 VBA 插入的 .png 图像上留下蓝色背景

PowerPoint Leaving Blue Background on .png Images Inserted With VBA

提问人:javery 提问时间:9/30/2023 最后编辑:tallerjavery 更新时间:9/30/2023 访问量:42

问:

我有这个插入图像的powerpoint宏的片段:

Sub InsertRandomPictures()
    Dim fso As Object
    Dim folder As Object
    Dim file As Object
    Dim pptSlide As Slide
    Dim pic As shape
    Dim folderPath As String
    Dim randomWidth As Single, randomHeight As Single
    Dim aspectRatio As Single
    Dim tempPic As shape
    Dim counter As Integer
    
    ' Set the path to the folder containing the pictures
    folderPath = "my/path/to/folder"
    
    ' Set up FileSystemObject
    Set fso = CreateObject("Scripting.FileSystemObject")
    Set folder = fso.GetFolder(folderPath)
    
    ' Reference slide 2
    Set pptSlide = ActivePresentation.Slides(2)
    
    ' Loop through each file in the folder
    For Each file In folder.Files
        ' Check if the file is a picture by looking at its extension
        If LCase(Right(file.Name, 3)) = "jpg" Or LCase(Right(file.Name, 3)) = "png" Or LCase(Right(file.Name, 3)) = "gif" Then        
            
            ' Insert the picture onto slide 2
            Set pic = pptSlide.Shapes.AddPicture( _
                fileName:=folderPath & file.Name, _
                LinkToFile:=msoFalse, _
                SaveWithDocument:=msoTrue, _
                Width:= 100, _
                Height:= 100)
                
            ' Set a random transparency value
            pic.Fill.Transparency = Rnd() * 0.9         
        End If
    Next file
End Sub

但是,它使我的 .png 图像和 .gif 具有透明性,并带有这种丑陋的蓝色填充。当我手动插入同一张图片时,它看起来不错。

(see picture)

我该如何摆脱它?

VBA 图像 PowerPoint

评论

0赞 taller 9/30/2023
如果图像是具有透明背景的 png,则来自它。删除代码,然后重试。pic.Fill.Transparency = Rnd() * 0.9
0赞 javery 9/30/2023
这奏效了。但是,有没有办法以编程方式调整透明度而不会造成这种情况?
0赞 taller 9/30/2023
AFAIK VBA 不支持它。PowerPoint VBA 中的图片透明度对象
0赞 javery 9/30/2023
啊,好吧。如果你把这些东西作为答案发布,我会接受的
0赞 taller 10/1/2023
我已经发布了

答:

0赞 taller 9/30/2023 #1

在处理具有透明背景的 PNG 图像时,它来自以下代码:

pic.Fill.Transparency = Rnd() * 0.9

截至目前,尚无可用于使用 VBA 代码管理图像透明度的属性。

该帖子也证实了这一点。

PowerPoint VBA 中的图片透明度对象