Exporting PSD to GIF is a common handoff for web graphics, email assets, and lightweight animations. With Aspose.PSD for .NET, you can convert a PSD into a static Gif in one call, และเมื่อ PSD contains an animation timeline, คุณสามารถส่งออก a animated Gifi* directly from that time line.
บทความนี้แสดงสถานการณ์ทั้งสองด้วยรหัสแบบเต็มรูปแบบ
ข้อกําหนด
- .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
- แผ่น GIF จะสร้างขึ้นโดยอัตโนมัติ หากคุณต้องการขนาดไฟล์หรือรูปร่างที่แตกต่างกันให้เพิ่มประสิทธิภาพการทํางานศิลปะพื้นฐาน (เรียบง่ายสี) ก่อนที่จะส่งออก
แปลง PSD ขึ้นอยู่กับระยะเวลาเป็น GIF ที่เคลื่อนไหว
หาก PSD ของคุณมีตารางเวลาการเคลื่อนไหวคุณสามารถส่งออกเป็น GIF ที่เคลื่อนย้ายได้โดยใช้ API ของชั่วคราว ซึ่งจะช่วยรักษาคําสั่งของกรอบและการล่าช้าที่เก็บไว้ใน 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 วงกลมเดียว
การแปลงแบทช์ทางเลือก
เครื่องมือต่อไปนี้จะแปลงแต่ละ 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 บิต หากคุณต้องการอัลฟาเต็มรูปแบบใช้ PNG สําหรับภาพเสถียรภาพและ APNG เพื่อ animation ในสภาพแวดล้อมที่เข้ากันได้
- ขนาดไฟล์: GIFs ที่เคลื่อนไหวสามารถขนาดใหญ่ ลดจํานวนกรอบขนาดหรือรายละเอียดงานศิลปะเพื่อให้ขนาดมีประสิทธิภาพ
- การตรวจสอบ: มักจะตรวจสอบผลลัพธ์ในสภาพแวดล้อมเป้าหมายเพื่อยืนยันการชั่วคราวการหมุนและพฤติกรรมของแพลต
ด้วยรูปแบบเหล่านี้คุณสามารถอัตโนมัติทั้ง static และ animated PSD ไปยัง GIF exports ภายใน .NET workflows ของคุณ