ASP.NET给图片加文字水印
类别:编程学习 浏览量:525
时间:2013-9-19 ASP.NET给图片加文字水印
ASP.NET给图片加文字水印ASP.NET给图片加文字水印代码如下:
using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; /// <summary> /// 文字水印处理方法 /// </summary> /// <param name="path">图片路径(绝对路径)</param> /// <param name="size">字体大小</param> /// <param name="letter">水印文字</param> /// <param name="color">颜色</param> /// <param name="location">水印位置</param> public static string LetterWatermark(string path, int size, string letter, Color color, string location) { string kz_name = Path.GetExtension(path); if (kz_name == ".jpg" || kz_name == ".bmp" || kz_name == ".jpeg") { DateTime time = DateTime.Now; string filename = "" + time.Year.ToString() + time.Month.ToString() + time.Day.ToString() + time.Hour.ToString() + time.Minute.ToString() + time.Second.ToString() + time.Millisecond.ToString(); Image img = Bitmap.FromFile(path); Graphics gs = Graphics.FromImage(img); ArrayList loca = GetLocation(location, img, size, letter.Length); Font font = new Font("宋体", size); Brush br = new SolidBrush(color); gs.DrawString(letter, font, br, float.Parse(loca[0].ToString()), float.Parse(loca[1].ToString())); gs.Dispose(); string newpath = Path.GetDirectoryName(path) + filename + kz_name; img.Save(newpath); img.Dispose(); File.Copy(newpath, path, true); if (File.Exists(newpath)) { File.Delete(newpath); } } return path; } /// <summary> /// 文字水印位置的方法 /// </summary> /// <param name="location">位置代码</param> /// <param name="img">图片对象</param> /// <param name="width">宽(当水印类型为文字时,传过来的就是字体的大小)</param> /// <param name="height">高(当水印类型为文字时,传过来的就是字符的长度)</param> private static ArrayList GetLocation(string location, Image img, int width, int height) { ArrayList loca = new ArrayList(); //定义数组存储位置 float x = 10; float y = 10; if (location == "LT") { loca.Add(x); loca.Add(y); } else if (location == "T") { x = img.Width / 2 - (width * height) / 2; loca.Add(x); loca.Add(y); } else if (location == "RT") { x = img.Width - width * height; } else if (location == "LC") { y = img.Height / 2; } else if (location == "C") { x = img.Width / 2 - (width * height) / 2; y = img.Height / 2; } else if (location == "RC") { x = img.Width - height; y = img.Height / 2; } else if (location == "LB") { y = img.Height - width - 5; } else if (location == "B") { x = img.Width / 2 - (width * height) / 2; y = img.Height - width - 5; } else { x = img.Width - width * height; y = img.Height - width - 5; } loca.Add(x); loca.Add(y); return loca; }
您可能感兴趣
- asp.net后台输出js脚本
- ASP.NET的Random随机数
- ASP.NET压力测试
- ASP.NET生成静态页面的方法
- ASP.NET下载远程图片到本地
- ASP.NET常用加密解密方法
- ASP.NET写文件的方法
- Asp.net导出Excel乱码
- asp.net把word文档转换成pdf
- 为什么asp.net网站第一次加载慢
- ASP.NET中XML和JSON互转
- ASP.NET实现FTP上传文件
- ASP.NET 生成条形码
- ASP.NET中Partial Class部分类
- ASP.NET参数化模糊查询
- asp.net使用swfupload上传
- 这竟然是捏出来的 20种橡皮泥玩法让你轻松hold住魔娃(这竟然是捏出来的)
- 自制橡皮泥(自制橡皮泥)
- 还在卖 禁药西布曲明网上论斤卖(还在卖禁药西布曲明网上论斤卖)
- 微商在朋友圈热卖的 DL减肥咖啡 含违禁药物,你还敢买吗(微商在朋友圈热卖的)
- 八一节,说说中国女兵(八一节说说中国女兵)
- 王治郅菜鸟赛季已让八一带入正轨,大郅七大经典语录或是成功秘诀(王治郅菜鸟赛季已让八一带入正轨)
热门推荐
- css设置隐藏左右边框(CSS 实现元素较宽不能被完全展示时将其隐藏的方法)
- python飞机大战游戏背景(python实现飞机大战游戏)
- thinkphp5对接支付宝扫码支付(ThinkPHP框架下整合支付宝支付功能图文教程)
- python的turtle库怎么画同心圆(详解python使用turtle库来画一朵花)
- docker node 分阶段构建(Docker安装、创建镜像、加载并运行NodeJS程序的详细过程)
- 如何认识服务器(关于Nginx、Apache、Tomcat三个WEB服务器的区别和认知)
- python中的迭代器详解(Python通过for循环理解迭代器和生成器实例详解)
- dedecms的简单说明(dedecms中英文网站之中英文搜索结果实现方法)
- javascript如何转换int型(浅谈JavaScript中的parseInt的妙用)
- mybatissql解析(mybatis动态sql常用场景总结)