在 PowerShell 中调整动画 Gif 的大小

问题描述 投票:0回答:0

我正在尝试在 PowerShell 中编写一个辅助函数来获取图像并调整它的大小。

这对于静态图像来说很简单,但我在处理 Gif 时遇到了麻烦。如果我对动画图像(> 1 帧)使用相同的逻辑,结果是只生成第一个(活动)帧。

我已经搜索了几个小时,并进行了多次尝试但无济于事(或者只是导致出现故障或全黑图像的代码)。

我正在用独立的东西写这个,所以我不能依赖任何外部图像库(如 imagemagick),除了 PowerShell 默认可用的任何东西,如 .NET。

这是我的想法:

function Resize-Image {
    param (
        [System.Drawing.Image]$originalImage,
        [int]$newHeight,
        [int]$newWidth
    )

    # If it's a Gif
    if ($originalImage.RawFormat.Guid -eq [System.Drawing.Imaging.ImageFormat]::Gif.Guid) {

        # Get dimension and frame count
        $dimension = $originalImage.FrameDimensionsList[0]
        $frameCount = $originalImage.GetFrameCount($dimension)

        # Create a memory stream to use here?


        # Resize each frame and push into new bitmap
        foreach($frameIndex in 0..($frameCount-1)) {
            # Go to the frame in the original image
            [void]$originalImage.SelectActiveFrame($dimension, $frameIndex)
            
            # How do I now take the active frame of $originalImage and push into a Gif?
            # I assume I use some encoders and use a combination of .Save and .SaveAdd, but nothing has worked or not resulted in GDI+ errors
        }

    }
    # If it's not a Gif
    else {
        # Just resize ?
        $resizedImage = New-Object System.Drawing.Bitmap -ArgumentList $originalImage, $newWidth, $newHeight
    }

    return $resizedImage
}

我已经可以检测图像是否是 Gif,但是在我的

foreach
中,我如何调整
$originalImage
的每个活动帧的大小并将其推入一个全新的位图中,我可以在最后返回?我还想保留 alpha/透明度。

.net powershell image-resizing system.drawing
© www.soinside.com 2019 - 2024. All rights reserved.