应用场景
在某些应用项目(如电子档案信息管理)中,查看电子图片信息是经常使用到的功能,此时我们就需要给显示在浏览器中的图片添加文字水印版权或提示信息。增加水印主要起到如下作用:
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# 自动填充文字内容到指定图片》
到此这篇关于c#实现给图片添加文字水印的示例代码的文章就介绍到这了,更多相关c#图片添加文字水印内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论