图形处理控件Aspose.PSD 入门教程(3):在 C# 中将水印添加到 PSD

在本文中,我们将学习如何在 C# 中为 PSD 添加水印,欢迎查阅!

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 添加水印。

(一)用C# Photoshop API 为 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
(二) 使用 C# 将文本水印添加到 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 PsdImagePsdImage psdImage = (PsdImage)Image.Load(@"C:FilesSimplePSD.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:FilesAddWatermark_output.png", new PngOptions());

使用 C# 将文本水印添加到 PSD

我们还可以使用以下代码示例将输出保存为 PSD 文件:

psdImage.Save(@"C:FilesAddWatermark_output.psd", new PsdOptions());
(三)使用 C# 在 PSD 中创建对角线水印

我们可以按照以下步骤在 PSD 文件中添加对角线文本水印:

  1. 首先,使用Image类将 PSD 文件加载为PsdImage 。
  2. 接下来,创建Graphics类的实例。
  3. 然后,定义一个Font类对象来绘制水印宽度。
  4. 同时,创建一个带有颜色的SolidBrush类的实例。
  5. 然后,指定变换矩阵来旋转水印。
  6. 稍后,指定字符串对齐方式。
  7. 之后,调用DrawString()方法。
  8. 最后,使用Save()方法保存输出文件。

以下代码示例展示了如何在 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 PsdImagePsdImage psdImage = (PsdImage)Image.Load(@"C:FilesSimplePSD.psd");// Create graphics object to perform draw operationsGraphics graphics = new Graphics(psdImage);// Create font to draw watermark withFont font = new Font("Arial", 25.0f);// Create a solid brush with color alpha set near to 0 to use watermarking effectSolidBrush brush = new SolidBrush(Color.FromArgb(80, 128, 128, 128));// Specify transform matrix to rotate watermarkgraphics.Transform = new Matrix();graphics.Transform.RotateAt(45, new PointF(psdImage.Width / 2, psdImage.Height / 2));// Specify string alignment to put watermark at the image centerStringFormat sf = new StringFormat();sf.Alignment = StringAlignment.Center;// Draw watermark using font, partly-transparent brush at the image centergraphics.DrawString("Sample Watermark Text", font, brush, new RectangleF(0, psdImage.Height / 2, psdImage.Width, psdImage.Height / 2), sf);// Export the image into PNG file formatpsdImage.Save(@"C:FilesAddDiagnolWatermark_output.png", new PngOptions());

使用 C# 在 PSD 中创建对角线水印
(四)使用 C# 将图像水印添加到 PSD

我们还可以按照以下步骤将图像作为水印添加到 PSD 文件中:

  1. 首先,使用Image类将 PSD 文件加载为PsdImage 。
  2. 接下来,创建Layer类的实例。
  3. 然后,设置图层的高度宽度不透明度
  4. 接下来,调用AddLayer()方法将图层添加到 PSD。
  5. 之后,将水印图像加载到图层中。
  6. 然后,将图像水印添加到图层。
  7. 之后,调用DrawImage()方法。它将位置和水印图像层作为参数。
  8. 最后,使用Save()方法保存输出文件。

以下代码示例展示了如何在 C# 中将图像水印添加到 PSD 文件中

// This code sample shows how to add an image watermark to a PSD file// Load a PSD file into PsdImage objectPsdImage psdImage = (PsdImage)Image.Load(@"C:FilesSimplePSD.psd");// Add a new watermark layervar baseLayer = new Layer();baseLayer.Top = 200;baseLayer.Bottom = 600;baseLayer.Right = 600;baseLayer.Opacity = 50;// Add layer to PSD filepsdImage.AddLayer(baseLayer);// Load a watermark image into a layerFileStream ImageWatermarkFile = new FileStream(@"C:Filesaspose_logo.png", FileMode.Open);Layer ImageWatermarkLayer = new Layer(ImageWatermarkFile);// Add image watermark to base layerbaseLayer.DrawImage(new Point(0, 200), ImageWatermarkLayer);// Save final watermarked PSD filepsdImage.Save(@"C:FilesImageWatermarkedPSD.png", new PngOptions());

使用 C# 将图像水印添加到 PSD

以上便是如何使用通过aspose.psd 在 C# 中将水印添加到 PSD,希望能对您有所帮助,如果您还有其他疑问,欢迎查阅本系列其他教程,或者私信我们获取帮助~


欢迎下载|体验更多Aspose文档管理产品 
获取更多信息请咨询在线客服 或 加入Aspose技术交流群(

标签:

来源:慧都

声明:本站部分文章及图片转载于互联网,内容版权归原作者所有,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2022年8月26日
下一篇 2022年8月26日

相关推荐

发表回复

登录后才能评论