如何在C中实现添加文字水印的功能?
- 行业动态
- 2025-01-19
- 2200
csharp,using System;,using System.Drawing;,using System.Drawing.Imaging;,,public class Watermark,{, public static void AddTextWatermark(string inputImagePath, string outputImagePath, string watermarkText), {, using (var image = Image.FromFile(inputImagePath)), {, using (var graphics = Graphics.FromImage(image)), {, var font = new Font("Arial", 20, FontStyle.Bold);, var textSize = graphics.MeasureString(watermarkText, font);, var x = (image.Width (int)textSize.Width) / 2;, var y = (image.Height (int)textSize.Height) / 2;, using (var brush = new SolidBrush(Color.FromArgb(128, Color.White))), {, graphics.DrawString(watermarkText, font, brush, x, y);, }, }, image.Save(outputImagePath, ImageFormat.Jpeg);, }, },},
`
,,这个类包含一个静态方法
AddTextWatermark`,它接受输入图像路径、输出图像路径和水印文本作为参数,并在图像上添加
文字水印。
在C#中添加文字水印到图像上,可以使用System.Drawing命名空间中的Graphics类,以下是一个完整的示例代码,展示了如何将文字水印添加到图像上:
using System; using System.Drawing; using System.Drawing.Imaging; public class Watermark { public static void AddTextWatermark(string inputImagePath, string outputImagePath, string watermarkText) { using (Image image = Image.FromFile(inputImagePath)) { using (Graphics graphics = Graphics.FromImage(image)) { // 设置字体样式、大小和颜色 Font font = new Font("Arial", 20, FontStyle.Bold); Color color = Color.FromArgb(128, 255, 255, 255); // 半透明白色 // 获取图像的宽度和高度 int width = image.Width; int height = image.Height; // 创建一个新的Bitmap对象,用于绘制水印 using (Bitmap watermarkImage = new Bitmap(width, height)) { using (Graphics watermarkGraphics = Graphics.FromImage(watermarkImage)) { // 设置水印文本的位置 PointF point = new PointF((width watermarkText.Length * 10) / 2, height / 2); // 绘制水印文本 watermarkGraphics.DrawString(watermarkText, font, new SolidBrush(color), point); } // 将水印图像与原始图像合并 graphics.DrawImage(watermarkImage, new Point(0, 0)); } } // 保存带有水印的图像 image.Save(outputImagePath, ImageFormat.Jpeg); } } } class Program { static void Main() { string inputImagePath = "path_to_your_input_image.jpg"; string outputImagePath = "path_to_your_output_image.jpg"; string watermarkText = "Your Watermark Text"; Watermark.AddTextWatermark(inputImagePath, outputImagePath, watermarkText); Console.WriteLine("Watermark added successfully!"); } }
代码解释:
1、导入必要的命名空间:
System.Drawing
: 提供基本的图形功能。
System.Drawing.Imaging
: 提供图像格式支持。
2、定义Watermark类:
AddTextWatermark
方法:接受输入图像路径、输出图像路径和水印文本作为参数。
3、加载图像并创建Graphics对象:
使用Image.FromFile
方法加载输入图像。
使用Graphics.FromImage
方法创建一个Graphics对象,用于绘制图像。
4、设置字体样式、大小和颜色:
使用Font
类定义水印文本的字体、大小和样式。
使用Color.FromArgb
方法定义水印文本的颜色(半透明白色)。
5、获取图像的宽度和高度:
使用image.Width
和image.Height
属性获取图像的尺寸。
6、创建新的Bitmap对象用于绘制水印:
使用new Bitmap(width, height)
创建一个新的Bitmap对象。
使用Graphics.FromImage
方法创建一个新的Graphics对象,用于绘制水印。
7、设置水印文本的位置并绘制文本:
计算水印文本的位置,使其居中显示。
使用DrawString
方法绘制水印文本。
8、将水印图像与原始图像合并:
使用graphics.DrawImage
方法将水印图像绘制到原始图像上。
9、保存带有水印的图像:
使用image.Save
方法保存带有水印的图像到指定路径。
FAQs:
Q1: 如何在图像的不同位置添加多个水印?
A1: 可以在AddTextWatermark
方法中多次调用DrawString
方法,每次调用时设置不同的坐标位置。
PointF point1 = new PointF(10, 10); // 左上角 PointF point2 = new PointF(width watermarkText.Length * 10 10, height 20); // 右下角 graphics.DrawString(watermarkText, font, new SolidBrush(color), point1); graphics.DrawString(watermarkText, font, new SolidBrush(color), point2);
Q2: 如何调整水印的透明度?
A2: 可以通过修改Color.FromArgb
方法的alpha值来调整水印的透明度,将alpha值设置为128(范围为0到255),表示50%的透明度:
Color color = Color.FromArgb(128, 255, 255, 255); // 半透明白色
小编有话说:
在实际应用中,添加水印是一个常见的需求,尤其是在处理图片版权保护时,通过上述代码,你可以轻松地在图像上添加自定义的文字水印,如果需要更复杂的水印效果,比如旋转文字或添加阴影,可以进一步扩展此代码,希望这个示例对你有所帮助!