C# 给图片添加文字水印

简介: C# 给图片添加文字水印

应用场景

在某些应用项目(如电子档案信息管理)中,查看电子图片信息是经常使用到的功能,此时我们就需要给显示在浏览器中的图片添加文字水印版权或提示信息。增加水印主要起到如下作用:

1、防止盗图:图片加水印可以有效防止盗图,将文字水印嵌入到图片中作为特殊标记,可以在不影响图片质量的情况下保护版权,即使别人下载了图片,也可以通过水印追踪到图片的来源。

2、增加宣传效果:可以通过添加URL或其它宣传性文字,增加宣传效果。

开发运行环境

操作系统: Windows Server 2019 DataCenter

.net版本: .netFramework4.0 或以上

开发工具:VS2019  C#

方法说明

AddWaterText 方法无返回值,具体参数说明请参照下表:

序号 参数名 类型 说明
1 oldpath string 原图片文件路径
2 text string 要添加的水印文字
3 newpath string 新输出图片文件路径
4 point object 设置文字起始位置坐标
5 font System.Drawing.Font 设置文字的字体
6 color System.Drawing.Color

设置文字的颜色

可使用 System.Drawing.Color.FromArgb(alpha, r, g, b)方法添加滤镜效果

7 rotate float 旋转角度值,默认值为 0.0f
8 textWidth int 文本预估宽度,默认值为1
9 textHeight int 文本预估高度,默认值为1
10 repeatD int 多水印文本间距值,默认值为0

方法代码

public void AddWaterText(string oldpath, string text, string newpath, object point, System.Drawing.Font font, System.Drawing.Color color, float rotate = 0.0f, int textWidth = 1,int textHeight=1, int repeatD=0)
    {
      
      try
      {
        FileStream fs = new FileStream(oldpath, FileMode.Open);
        BinaryReader br = new BinaryReader(fs);
        byte[] bytes = br.ReadBytes((int)fs.Length);
        br.Close();
        fs.Close();
        MemoryStream ms = new MemoryStream(bytes);
 
        System.Drawing.Image imgPhoto = System.Drawing.Image.FromStream(ms);
        int imgPhotoWidth = imgPhoto.Width;
        int imgPhotoHeight = imgPhoto.Height;
 
        Bitmap bmPhoto = new Bitmap(imgPhotoWidth, imgPhotoHeight, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
 
        bmPhoto.SetResolution(72, 72);
        Graphics gbmPhoto = Graphics.FromImage(bmPhoto);
        gbmPhoto.Clear(Color.FromName("white"));
        gbmPhoto.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
        gbmPhoto.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
        gbmPhoto.DrawImage(imgPhoto, new Rectangle(0, 0, imgPhotoWidth, imgPhotoHeight), 0, 0, imgPhotoWidth, imgPhotoHeight, GraphicsUnit.Pixel);
        System.Drawing.SizeF crSize = new SizeF();
        crSize = gbmPhoto.MeasureString(text, font);
        float y = imgPhotoHeight - crSize.Height;
        float x = imgPhotoWidth - crSize.Width;
 
        System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();
        StrFormat.Alignment = System.Drawing.StringAlignment.Center;
 
        if(point!=null)
        {
          System.Drawing.Point newpos=((System.Drawing.Point)point);
          x=newpos.X;
          y=newpos.Y;
        }
 
 
        System.Drawing.SolidBrush semiTransBrush = new System.Drawing.SolidBrush(color);
        System.Drawing.Color.FromArgb(1,1,1,1);
                gbmPhoto.RotateTransform(rotate);
                if (repeatD == 0)
                {
                    gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
                }
                else
                {
                    int xcount = imgPhotoWidth/textWidth+3;
                    int ycount = imgPhotoHeight/textHeight+3;
                    float ox = x;
                    for (int k = 0; k < ycount; k++)
                    {
                        for (int i = 0; i < xcount; i++)
                        {
                            for (int j = 0; j < xcount; j++)
                            {
 
                                gbmPhoto.DrawString(text, font, semiTransBrush, x, y);
                            }
                            x += textWidth+repeatD;
                        }
                        x = ox;
                        y += textHeight+repeatD;
                    }
                }
        bmPhoto.Save(newpath, System.Drawing.Imaging.ImageFormat.Jpeg);
        gbmPhoto.Dispose();
        imgPhoto.Dispose();
        bmPhoto.Dispose();
      }
      catch
      {               
        ;               
      }
    }

调用示例

//获取源图片文件路径
string tempfile=Request.PhysicalApplicationPath+"\\app_data\\test.jpg";
//设置文字位置
System.Drawing.Point point = new System.Drawing.Point();
point.X = -10;
point.Y = -100;
//设置字体类
System.Drawing.Font font = new System.Drawing.Font("微软雅黑", 19, System.Drawing.FontStyle.Bold);
//设置字体滤镜值 ,和RGB分量颜色
int alpha = 25; int r = 255; int g = 0; int b = 255;
System.Drawing.Color color = System.Drawing.Color.FromArgb(alpha, r, g, b);
 
 
float rotate=30.0f; // 旋转角度
int textWidth = 100; //文本预估宽度
int textHeight=30; //文本预估高度
int repeatD=100; // 多水印文本间距,则表示多水印输出
 
//添加水印文字
string text="版权所有";
AddWaterText(tempfile,text,tempfile, point, font, color,rotate,textWidth,textHeight,repeatD);
 
File.Delete(tempfile);  //删除释放文件,在些之前可执行显示操作,如获取base64编码

显示效果如下图:

小结

AddWaterText 方法需要根据您实际应用中的图片大小动态调整参数,以达到满意的显示效果,如果文字起始位置,字体大小,水印间距等。您也可以改造本方法或应用,自动适应调整参数值。

调用示例中新旧图片文件输出为同一文件,然后删除释放文件所占用磁盘的空间,因此我们想要正确显示图片在浏览器的话,需要在删除文件前获取图片的Base64编码即可,如何获取base64数据的方法请参照我的文章:《C# 自动填充文字内容到指定图片》

感谢您的阅读,希望本文能够对您有所帮助。

相关文章
|
4天前
|
存储 算法 C#
C# 生成指定图片的缩略图
C# 生成指定图片的缩略图
|
4天前
|
开发框架 .NET C#
C# 自动填充文字内容到指定图片
C# 自动填充文字内容到指定图片
|
4天前
|
API C# 数据安全/隐私保护
C# 实现网页内容保存为图片并生成压缩包
C# 实现网页内容保存为图片并生成压缩包
|
7月前
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
选中项目,点击右上角的显示全部文件按钮,会将默认隐藏的文件显示出来,选中所需图片,右键,添加到项目,然后选择图片查看属性,生成操作选择resource。完毕。本人目前的解决方案。
267 41
C#WPF 图片在显示时没有问题,但在运行时图片显示不出来的解决
|
5月前
|
API C#
C# 调用系统“API“设置图片为“桌面壁纸“
C# 调用系统“API“设置图片为“桌面壁纸“
|
7月前
|
C#
C# 图片RGB处理判断
C# 图片RGB处理判断 需要:根据一张原始图的RGB平均值和新的图片的RGB平均值的差距,来判断图中是否出现除原图中物体外的其他物体 前提:.Net framework 4.8 及以上 示例代码: 程序集:using System;using System.Drawing;using System.Drawing.Drawing2D;using System.Drawing.Imagin...
25 0
|
10月前
|
人工智能 文字识别 API
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
C# 10分钟完成百度图片提取文字(文字识别)——入门篇
|
算法 定位技术 C#
C#开发:不规则裁切图片
C#开发:不规则裁切图片
122 0
|
区块链 C#
C#实现把图片转换为ico格式
C#实现把图片转换为ico格式
613 0