PSD 到圖像 C# 2

Photoshop 文檔 (PSD) 是 Adobe Photoshop 使用的一種眾所周知的文件格式,用於以圖層的形式保存數據。這些層組合在一起以獲得所需的藝術品或圖像。 Adobe Photoshop 允許設計人員將 PSD 圖層導出為 PNG、JPG、GIF、TIFF 和其他格式的單個圖像。

如果您想在沒有 Photoshop 的情況下實現將 PSD 文件轉換或導出為光柵圖像格式的功能,您肯定會尋求一些自動化。對於這種情況,我將演示如何使用 C# 以編程方式將 PSD 轉換為 PDF、JPG、PNG、BMP 或其他光柵圖像格式。此外,您還將學習如何在 PSD 中轉換或導出所需的圖層/圖層組。本文介紹了 C# 中的以下 PSD 轉換:

  • PSD 轉 JPG
  • PSD 轉 PNG
  • PSD 轉 BMP
  • PSD 轉 TIFF
  • PSD轉GIF
  • PSD 轉 JP2
  • PSD 轉 PDF
  • PSD 圖層/圖層組到圖像

C# PSD 轉換器 API - Aspose.PSD for .NET

為了將 PSD 轉換為 PDF 和圖像格式,我們將利用 Aspose.PSD for .NET 的功能,它是簡單易用的 .NET API,用於處理和轉換 Photoshop 文件 (PSD)。除了轉換功能外,該 API 還允許您創建和編輯 Photoshop PSD 文件、更新圖層屬性、插入水印以及對 PSD 文件執行各種與圖形相關的操作。 Aspose.PSD for .NET 可以從 NuGet 安裝,也可以從 下載 部分下載為 DLL。

在 C# 中將 PSD 轉換為 JPG

以下是在 C# 中將 PSD 文件轉換為 JPG 圖片的步驟:

以下代碼示例顯示瞭如何在 C# 中將 PSD 文件轉換為 JPG:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 JpegOptions 類的實例
	JpegOptions jpegOptions = new JpegOptions();
	jpegOptions.Quality = 100;
	// 將 PSD 轉換為 JPG
	image.Save("PSD-to-JPG.jpeg", jpegOptions);
}

文件

PSD 轉 JPG

轉換後的 JPG 圖像

PSD 轉 PNG

在 C# 中將 PSD 轉換為 PNG

以下是在 C# 中將 PSD 文件轉換為 PNG 圖像的步驟:

以下代碼示例顯示瞭如何在 C# 中將 PSD 文件轉換為 PNG:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 PngOptions 類的實例
	PngOptions pngOptions = new PngOptions();
	// 將 PSD 轉換為 PNG
	image.Save("PSD-to-PNG.PNG", pngOptions);
}

在 C# 中將 PSD 轉換為 BMP

以下是在 C# 中將 PSD 文件轉換為 BMP 圖片的步驟:

以下代碼示例顯示瞭如何在 C# 中將 PSD 文件轉換為 BMP:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 BmpOptions 類的實例
	BmpOptions options = new BmpOptions();
	// 將 PSD 轉換為 BMP
	image.Save("PSD-to-BMP.bmp", options);
}

在 C# 中將 PSD 轉換為 TIFF

以下是在 C# 中將 PSD 文件轉換為 TIFF 圖像的步驟:

以下代碼示例顯示瞭如何在 C# 中將 PSD 文件轉換為 TIFF:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 TiffOptions 類的實例
	TiffOptions options = new TiffOptions(FileFormats.Tiff.Enums.TiffExpectedFormat.Default);
	// 將 PSD 轉換為 Tiff
	image.Save("PSD-to-Tiff.tiff", options);
}

在 C# 中將 PSD 轉換為 GIF

以下是在 C# 中將 PSD 文件轉換為 GIF 圖片的步驟:

以下代碼示例在 C# 中將 PSD 文件轉換為 GIF:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 GifOptions 類的實例
	GifOptions options = new GifOptions();
	// 將 PSD 轉換為 GIF
	image.Save("PSD-to-GIF.gif", options);
}

在 C# 中將 PSD 轉換為 JP2

以下是在 C# 中將 PSD 文件轉換為 JP2 圖片的步驟:

以下代碼示例在 C# 中將 PSD 文件轉換為 JP2:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 Jpeg2000Options 類的實例
	Jpeg2000Options options = new Jpeg2000Options();
	// 將 PSD 轉換為 JP2
	image.Save("PSD-to-JP2.jp2", options);
}

在 C# 中將 PSD 轉換為 PDF

以下是在 C# 中將 PSD 文件轉換為 PDF 文檔的步驟:

以下代碼示例在 C# 中將 PSD 文件轉換為 PDF 文檔:

// 將現有的 PSD 圖像加載為圖像
using (Image image = Image.Load("Photoshop.psd"))
{
	// 創建 PdfOptions 類的實例
	PdfOptions options = new PdfOptions();
	// 將 PSD 轉換為 PDF
	image.Save("PSD-to-PDF.PDF", options);
}

在 C# 中轉換 PSD 的圖層或圖層組

您還可以將 PSD 文件中的特定圖層或圖層組轉換為光柵圖像格式(JPG、PNG 等)。以下是執行此操作的步驟。

  • 創建 PsdImage 類的對象並加載 PSD 文件。
  • 使用 PsdImage.Layers 數組訪問圖層或圖層組。
  • 將圖層渲染為圖像。

下面的代碼示例顯示瞭如何在 C# 中將 PSD 的圖層組轉換為 PNG。

// 將現有的 PSD 圖像加載為圖像
using (PsdImage image = (PsdImage)Image.Load("Photoshop.psd"))
{
	LayerGroup formats = (LayerGroup)image.Layers[1]; 
	formats.Save("formats.png", new PngOptions()); 
	Console.ReadKey();
}

了解有關 Aspose.PSD for .NET 的更多信息

您可以使用 文檔 探索更多關於 Aspose.PSD for .NET 的信息。

相關文章)