当前位置:首页 > 行业动态 > 正文

如何在C中添加文字水印?请提供相关代码示例。

当然,以下是一个使用 C# 添加文字水印的示例代码:,,“ csharp,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)), {, int width = image.Width;, int height = image.Height;,, using (Graphics graphics = Graphics.FromImage(image)), {, Font font = new Font("Arial", 20, FontStyle.Bold);, SolidBrush brush = new SolidBrush(Color.FromArgb(128, Color.White));, SizeF textSize = graphics.MeasureString(watermarkText, font);,, float x = (width textSize.Width) / 2;, float y = (height textSize.Height) / 2;,, 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, Color.White); // 半透明白色
                // 获取图像的宽度和高度
                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.Widthimage.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); // 右下角
watermarkGraphics.DrawString(watermarkText, font, new SolidBrush(color), point1);
watermarkGraphics.DrawString(watermarkText, font, new SolidBrush(color), point2);

Q2: 如何调整水印的透明度?

A2: 可以通过修改Color.FromArgb方法的alpha值来调整透明度,将alpha值从128改为255可以使水印完全不透明:

Color color = Color.FromArgb(255, Color.White); // 不透明白色

小编有话说:

在实际应用中,添加水印是一个常见的需求,尤其是在处理图片版权保护时,通过上述代码,你可以轻松地在图像上添加自定义的文字水印,如果需要更复杂的水印效果,比如旋转文字或添加阴影,可以进一步扩展此代码,希望这个示例对你有所帮助!

0