Adobe Illustrator 文件主要用於表示視覺信息。此外,平面設計師使用 AI 文件將他們的創意轉化為傑作。有時您需要在 C# 中將 AI 轉換為光柵圖像(PNG、JPG 等)或 PDF 文件。所有這些轉換中最重要和最令人興奮的因素是您不需要安裝或配置 Adobe Illustrator 應用程序,因為 Aspose.PSD for .NET API 能夠轉換 AI 文件而不依賴於第 3 方應用程序.讓我們繼續討論這篇博文中的以下用例:

需要注意的是,我們將考慮將兩個不同的 AI 文件作為以下幾個示例的輸入。其中一個輸入 AI 文件具有不同的路徑權重,另一個 AI 文件包含複雜的輪廓。因此,讓我們查看我們將使用的 AI 文件的屏幕截圖:

具有不同路徑權重的 AI 文件

AI轉PNG C#

包含複雜輪廓的 AI 文件

AI 到 JPG C#

使用 C# 將 AI 轉換為 PNG

您可以增強 .NET 應用程序以處理 AI 文件並將其轉換為 PNG 圖像。 PNG 圖像非常有名,通常是在線表格中提交圖像的標準。因此,您可以按照以下步驟將 AI 轉換為 PNG 文件:

  1. 加載源 AI 文件
  2. 設置 PngOptions 類的屬性
  3. 在C#中將AI文件保存為PNG圖像

以下代碼片段基於上述步驟,展示瞭如何使用 C# 將 AI 轉換為 PNG:

string[] sourcesFiles = new string[]
{
    @"34992OStroke",
    @"rect2_color",
};

for (int i = 0; i < sourcesFiles.Length; i++)
{
    string name = sourcesFiles[i];
    string sourceFileName = dataDir + name + ".ai";
    string outFileName = dataDir + name + ".png";

    using (AiImage image = (AiImage)Image.Load(sourceFileName))
    {

        ImageOptionsBase options = new PngOptions() { ColorType = PngColorType.TruecolorWithAlpha };
        image.Save(outFileName, options);

    }
}

使用 C# 將 AI 轉換為 JPG

將 AI 文件轉換為 JPG 圖像很簡單。您需要按照以下步驟操作:

  1. 使用 AiImage 類加載輸入 AI 文件
  2. 設置 ImageOptionsBase 屬性
  3. 保存輸出 JPG 圖像

以下代碼片段顯示瞭如何在 C# 應用程序中將 AI 轉換為 JPG:

string[] sourcesFiles = new string[]
{
    @"34992OStroke",
    @"rect2_color",
};

for (int i = 0; i < sourcesFiles.Length; i++)
{
    string name = sourcesFiles[i];
    string sourceFileName = dataDir + name + ".ai";
    string outFileName = dataDir + name + ".jpg";

    using (AiImage image = (AiImage)Image.Load(sourceFileName))
    {

        ImageOptionsBase options = new JpegOptions() { Quality = 85 };
        image.Save(outFileName, options);

    }
}

使用 C# 將 AI 轉換為 PSD

PSD 文件是包含分層圖像的 photoshop 文檔。您可以在不使用 Adobe Photoshop 或 Adobe Illustrator 的情況下將 AI 轉換為 PSD。請按照以下步驟執行轉換:

  1. 加載 AI 輸入文件
  2. 初始化 PsdOptions 類的對象
  3. 結果保存輸出 PSD 文件

同樣,下面的代碼片段顯示瞭如何在 C# .NET 應用程序中將 AI 轉換為 PSD:

string[] sourcesFiles = new string[]
{
    @"34992OStroke",
    @"rect2_color",
};

for (int i = 0; i < sourcesFiles.Length; i++)
{
    string name = sourcesFiles[i];
    string sourceFileName = dataDir + name + ".ai";
    string outFileName = dataDir + name + ".psd";


    using (AiImage image = (AiImage)Image.Load(sourceFileName))
    {

        ImageOptionsBase options = new PsdOptions();
        image.Save(outFileName, options);

    }
}

使用 C# 將 AI 轉換為 PDF

PDF 文件格式之所以著名,是因為幾乎所有平台都支持 .pdf 文件擴展名。此外,除非文檔作者允許,否則 PDF 文件很容易更改和修改。有時您可能需要與合作夥伴或客戶共享內容,但不允許他們進行任何修改。因此,AI 到 PDF 轉換在這種情況下很有幫助。您需要按照以下步驟執行轉換:

  1. 加載源 AI 文件
  2. 聲明 PdfOptions 類的一個實例
  3. 因此保存輸出 PDF 文件

以下代碼片段顯示瞭如何使用 C# 將 AI 轉換為 PDF:

string[] sourcesFiles = new string[]
{
    @"rect2_color",
};

for (int i = 0; i < sourcesFiles.Length; i++)
{
    string name = sourcesFiles[i];
    string sourceFileName = dataDir + name + ".ai";
    string outFileName = dataDir + name + ".pdf";


    using (AiImage image = (AiImage)Image.Load(sourceFileName))
    {

        ImageOptionsBase options = new PdfOptions();
        image.Save(outFileName, options);
    }
}

結論

簡而言之,可以使用 Aspose.PSD for .NET API 將 AI 文件渲染為不同的文件格式。例如,我們探索瞭如何將 AI 轉換為 PNG、AI 到 JPG、AI 到 PSD 以及 AI 到 PDF 文件格式。因此,您可以訪問 API 文檔API 參考資料 以了解有關 API 的更多信息。此外,我們感謝您在 免費支持論壇 上的反饋或查詢。

也可以看看