通过 Aspose.PSD for .NET,您可以在一次呼叫中将 PSD 转换为 静态 GIF ,而当 PSD 中包含动画时间线时,可以直接从该的时间线中导出 动作 Gif

本文展示了两个场景,包含完整的内线代码。

原則

  • .NET 6 或更高版本(或 .Net Framework 4.6.2+)
  • NuGet 包: Aspose.PSD
dotnet add package Aspose.PSD

将 PSD 转换为静态 GIF

使用 Image.Load 开启PSD,发送到 PsdImage和拯救与 GifOptions这将PSD转化为单框GIF。

// File: Program.cs
using System;
using System.IO;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.ImageOptions;

class Program
{
    static void Main()
    {
        var inputPsd  = "input.psd";
        var outputGif = "output-static.gif";

        if (!File.Exists(inputPsd))
        {
            Console.WriteLine("Input PSD not found.");
            return;
        }

        using (var psd = (PsdImage)Image.Load(inputPsd))
        {
            // Export as a single-frame GIF
            var gifOptions = new GifOptions();
            psd.Save(outputGif, gifOptions);
        }

        Console.WriteLine("Saved: " + Path.GetFullPath(outputGif));
    }
}

注意事項

  • 静态出口不需要在PSD的时间线。
  • 如果您需要不同的文件大小或外观,在出口之前优化源艺术作品(平板,简化颜色)。

以时间线为基础的PSD转换为动画GIF

如果您的 PSD 包含动画时间表,您可以使用时间线 API 将其导出为动态 GIF. 这保留了在 PSD 中存储的框架顺序和延迟。

// File: Program.cs
using System;
using System.IO;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.ImageOptions;

class Program
{
    static void Main()
    {
        var inputPsd   = "animated.psd";
        var outputAgif = "output-animated.gif";

        if (!File.Exists(inputPsd))
        {
            Console.WriteLine("Input PSD not found.");
            return;
        }

        using (var psd = (PsdImage)Image.Load(inputPsd))
        {
            if (psd.Timeline != null)
            {
                // Export PSD timeline as animated GIF
                var gifOptions = new GifOptions();
                psd.Timeline.Save(outputAgif, gifOptions);
                Console.WriteLine("Saved animated GIF: " + Path.GetFullPath(outputAgif));
            }
            else
            {
                // Fallback to static export if the PSD has no timeline
                var gifOptions = new GifOptions();
                psd.Save(outputAgif, gifOptions);
                Console.WriteLine("PSD has no timeline. Saved static GIF: " + Path.GetFullPath(outputAgif));
            }
        }
    }
}

注意事項

  • 此路径符合 PSD 时间表中定义的框架顺序和每框延迟。
  • 如果您的 PSD 没有时间线数据,则代码将返回单框 GIF。

可选 Batch 转换

下面的用途将每个 PSD 在一个文件夹中转换为 GIF. 动画的 PSD 通过时间线出口,当可用时,其他人作为静态GIF出口。

// File: BatchPsdToGif.cs
using System;
using System.IO;
using Aspose.PSD;
using Aspose.PSD.FileFormats.Psd;
using Aspose.PSD.ImageOptions;

public static class BatchPsdToGif
{
    public static void Run(string inputDir, string outputDir)
    {
        Directory.CreateDirectory(outputDir);
        var psdFiles = Directory.GetFiles(inputDir, "*.psd", SearchOption.AllDirectories);

        foreach (var psdPath in psdFiles)
        {
            var rel   = Path.GetRelativePath(inputDir, psdPath);
            var outFn = Path.ChangeExtension(rel, ".gif");
            var outFp = Path.Combine(outputDir, outFn);

            Directory.CreateDirectory(Path.GetDirectoryName(outFp) ?? outputDir);

            try
            {
                using var psd = (PsdImage)Image.Load(psdPath);
                var gifOptions = new GifOptions();

                if (psd.Timeline != null)
                {
                    psd.Timeline.Save(outFp, gifOptions);
                }
                else
                {
                    psd.Save(outFp, gifOptions);
                }

                Console.WriteLine($"OK  {rel} -> {outFn}");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"ERR {rel}: {ex.Message}");
            }
        }
    }
}

呼叫它从 Main:

// inside Main
// BatchPsdToGif.Run(@"C:\in-psd", @"C:\out-gif");

最佳实践

  • ** 颜色限制**: GIF 使用高达 256 种颜色的索引板。 对于复杂的艺术作品,请考虑在 PSD 中简化层或色彩,以获得更好的和更小的文件。
  • 透明度: GIF 支持 1 位透露度. 如果您需要完整的 Alpha,请使用 PNG 用于静态图像和 APNG 在兼容的环境中进行动画。
  • ** 文件大小**:动画 GIF 可以很大. 减少框架数量、框尺寸或艺术作品细节,以保持尺度实用。
  • 验证:始终在目标环境中预测输出,以确认时间、旋转和弹性行为。

使用这些模式,您可以在 .NET 工作流中自动化静态和动画 PSD 到 GIF 出口。

More in this category