C#实现图片添加水印

有时我们需要在图像上添加水印。例如,在图像上添加版权或名称。我们可能还需要在文档中创建水印。接下来就实现 C# 如何在图像上添加水印。要添加水印的图片如下:

 1、VS2019添加一个控制台程序,当然别的类型程序也可,代码是一样的

 

 

 把图片复制到项目的指定目录下

 2、项目引用System.Drawing.dll

 3、在项目的bin目录下创建目录images,将图片6p.jpg复制到该目录下,当然这个目录你放在别的位置也行。

 4、编写代码:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleAppPicture
{
    class Program
    {
        static void Main(string[] args)
        {
            //获取当前应用程序执行路径
            string apppath = System.Environment.CurrentDirectory+ "\\images\\";
            //设置目标图片路径
            string src_path = apppath + "6p.jpg";
            //设置保存位置
            string dst_path = apppath + "6p-2.jpg";
            //读取目标图片
            System.Drawing.Image src_img = (System.Drawing.Image)Bitmap.FromFile(src_path);
            //设置水印字体、字号
            Font font = new Font("Arial", 115, FontStyle.Italic, GraphicsUnit.Pixel);
            //设置水印颜色
            Color color = Color.FromArgb(255, 255, 0, 0);
            //运算水印位置
            Point atpoint = new Point(src_img.Width / 2, src_img.Height / 3);
            //初始化画刷
            SolidBrush brush = new SolidBrush(color);
            //初始化gdi绘图
            using (Graphics graphics = Graphics.FromImage(src_img))
            {
                StringFormat sf = new StringFormat();
                sf.Alignment = StringAlignment.Center;
                sf.LineAlignment = StringAlignment.Center;
                graphics.DrawString("www.ywjwest.com", font, brush, atpoint, sf); 
                using (MemoryStream m = new MemoryStream())
                {
                    //以jpg格式写入到内存流,完成绘制
                    src_img.Save(m, System.Drawing.Imaging.ImageFormat.Jpeg);
                    //保存到磁盘
                    src_img.Save(dst_path);
                }
            }
        }
    }
}

5、运行效果、

 

 效果相当的帅,要加鸡腿了。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
引用[1]: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; using System.IO; 问题: C#批量添加图片水印 回答: 在C#中,可以使用System.Drawing命名空间中的类来实现批量添加图片水印的功能。首先,你需要引用System.Drawing命名空间,并使用System.IO命名空间来处理文件操作。然后,你可以使用Bitmap类来图片,使用Graphics类来绘制水印,并使用Image类来保存修改后的图片。以下是一个简单的示例代码: ```csharp using System; using System.Drawing; using System.IO; namespace WatermarkExample { class Program { static void Main(string[] args) { string watermarkText = "Watermark"; // 水印文本 string imagePath = "image.jpg"; // 图片路径 string outputDirectory = "output"; // 输出目录 // 创建输出目录 Directory.CreateDirectory(outputDirectory); // 图片 using (Bitmap image = new Bitmap(imagePath)) { // 创建Graphics对象 using (Graphics graphics = Graphics.FromImage(image)) { // 设置水印字体和颜色 Font font = new Font("Arial", 12); Brush brush = new SolidBrush(Color.Red); // 计算水印位置 SizeF textSize = graphics.MeasureString(watermarkText, font); PointF position = new PointF(image.Width - textSize.Width - 10, image.Height - textSize.Height - 10); // 绘制水印 graphics.DrawString(watermarkText, font, brush, position); } // 保存修改后的图片 string outputImagePath = Path.Combine(outputDirectory, "watermarked_image.jpg"); image.Save(outputImagePath); } Console.WriteLine("水印添加完成!"); } } } ``` 在上述示例代码中,你可以修改`watermarkText`变量来设置水印文本,`imagePath`变量来设置要添加水印图片路径,`outputDirectory`变量来设置输出目录。运行代码后,修改后的图片将保存在输出目录中。请确保你已经正确引用了System.Drawing和System.IO命名空间,并且已经将图片文件放置在正确的路径下。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hqwest

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值