翻譯|使用教程|編輯:胡濤|2022-09-29 11:25:33.493|閱讀 484 次
概述:在本文中,我們將學習如何在 C# 中為 PSD 添加水印,歡迎查閱!
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
相關鏈接:
Aspose.PSD for .NET 是高級PSD文件格式操作API,沒有任何Adobe Photoshop依賴項。API允許創建或編輯Photoshop文件,并提供更新圖層屬性,添加水印,執行圖形操作或將一種文件格式轉換為另一種文件的功能。
Aspose.PSD for .NET支持PSD和PSB文件格式進行加載和處理,并允許導出為各種光柵圖像格式,例如TIFF,JPEG,PNG,GIF,BMP等。
Adobe 流行的 Photoshop 應用程序使用PSD(Photoshop 文檔)作為原生圖像文件格式。它通常用于創建 PSD 文件包含多個圖層的徽標、小冊子和其他圖像。我們可以通過以編程方式將文本或圖像水印添加到 PSD 文件來輕松保護設計。在本文中,我們將學習如何在 C# 中為 PSD 添加水印。
要在 PSD 文件中添加文本或圖像水印,我們將使用Aspose.PSD for .NET API。它是一個易于使用的 Adobe Photoshop 文件格式操作 API。它允許在 .NET 應用程序中加載和讀取 PSD 和PSB文件。該 API 使我們能夠更新圖層屬性、添加水印、執行壓縮、旋轉、縮放或渲染 PSD 以及其他幾種受支持的文件格式,而無需安裝 Adobe Photoshop。
API的PsdImage類允許加載、編輯和保存 PSD 文件。Graphics類表示圖像中的圖形。我們有這個類的DrawString(string, Font, Brush, RectangleF, StringFormat)方法,它使用指定的畫筆和字體對象在指定的矩形中繪制指定的文本字符串。Layer類表示 PSD 層。該類的DrawImage(Point, RasterImage)方法在圖層上繪制圖像。我們可以使用Save(string, ImageOptionsBase)方法將 PSD 保存到指定的文件位置。
請下載 API 的 DLL或使用NuGet安裝它。
PM> Install-Package Aspose.PSD
// This code example shows how to add a text watermark to a PSD file // Load a PSD file as an image and cast it into PsdImage PsdImage psdImage = (PsdImage)Image.Load(@"C:\Files\SimplePSD.psd"); // Create graphics object to perform draw operations. Graphics graphics = new Graphics(psdImage); // Create font to draw watermark with. Font font = new Font("Arial", 25.0f); // Create a solid brush with color alpha set near to 0 to use watermarking effect. SolidBrush brush = new SolidBrush(Color.FromArgb(80, 128, 128, 128)); // Specify string alignment to put watermark at the image center. StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; sf.LineAlignment = StringAlignment.Center; // Draw watermark using font, partly-transparent brush and rotation matrix at the image center. graphics.DrawString("Sample Watermark Text", font, brush, new RectangleF(0, 0, psdImage.Width, psdImage.Height), sf); // Export the image into PNG file format. psdImage.Save(@"C:\Files\AddWatermark_output.png", new PngOptions());
我們還可以使用以下代碼示例將輸出保存為 PSD 文件:
psdImage.Save(@"C:\Files\AddWatermark_output.psd", new PsdOptions());
我們可以按照以下步驟在 PSD 文件中添加對角線文本水印:
以下代碼示例展示了如何在 C# 中向 PSD 文件添加對角線文本水印。
// This code example shows how to add a diagonal text watermark to a PSD file // Load a PSD file as an image and cast it into PsdImage PsdImage psdImage = (PsdImage)Image.Load(@"C:\Files\SimplePSD.psd"); // Create graphics object to perform draw operations Graphics graphics = new Graphics(psdImage); // Create font to draw watermark with Font font = new Font("Arial", 25.0f); // Create a solid brush with color alpha set near to 0 to use watermarking effect SolidBrush brush = new SolidBrush(Color.FromArgb(80, 128, 128, 128)); // Specify transform matrix to rotate watermark graphics.Transform = new Matrix(); graphics.Transform.RotateAt(45, new PointF(psdImage.Width / 2, psdImage.Height / 2)); // Specify string alignment to put watermark at the image center StringFormat sf = new StringFormat(); sf.Alignment = StringAlignment.Center; // Draw watermark using font, partly-transparent brush at the image center graphics.DrawString("Sample Watermark Text", font, brush, new RectangleF(0, psdImage.Height / 2, psdImage.Width, psdImage.Height / 2), sf); // Export the image into PNG file format psdImage.Save(@"C:\Files\AddDiagnolWatermark_output.png", new PngOptions());
我們還可以按照以下步驟將圖像作為水印添加到 PSD 文件中:
以下代碼示例展示了如何在 C# 中將圖像水印添加到 PSD 文件中。
// This code sample shows how to add an image watermark to a PSD file // Load a PSD file into PsdImage object PsdImage psdImage = (PsdImage)Image.Load(@"C:\Files\SimplePSD.psd"); // Add a new watermark layer var baseLayer = new Layer(); baseLayer.Top = 200; baseLayer.Bottom = 600; baseLayer.Right = 600; baseLayer.Opacity = 50; // Add layer to PSD file psdImage.AddLayer(baseLayer); // Load a watermark image into a layer FileStream ImageWatermarkFile = new FileStream(@"C:\Files\aspose_logo.png", FileMode.Open); Layer ImageWatermarkLayer = new Layer(ImageWatermarkFile); // Add image watermark to base layer baseLayer.DrawImage(new Point(0, 200), ImageWatermarkLayer); // Save final watermarked PSD file psdImage.Save(@"C:\Files\ImageWatermarkedPSD.png", new PngOptions());
以上便是如何使用通過aspose.psd 在 C# 中將水印添加到 PSD,希望能對您有所幫助,如果您還有其他疑問,歡迎查閱本系列其他教程,或者私信我們獲取幫助~
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn