using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Drawing; using System.Drawing.Imaging; using System.Drawing.Drawing2D; /// <summary> /// Helper 的摘要说明 /// </summary> public class Helper {public class UpLoadHelper{#region 添加文字水印/// <summary> /// 添加文字水印 /// </summary> /// <param name="text">水印文字</param> /// <param name="file">图片文件</param> public static void AttachText(string text, string file){if (string.IsNullOrEmpty(text)){return;}if (!File.Exists(file)){return;}FileInfo oFile = new FileInfo(file);string strTempFile = Path.Combine(oFile.DirectoryName, Guid.NewGuid().ToString() + oFile.Extension);oFile.CopyTo(strTempFile);Image img = Image.FromFile(strTempFile);ImageFormat thisFormat = img.RawFormat;int nHeight = img.Height;int nWidth = img.Width;Bitmap outBmp = new Bitmap(nWidth, nHeight);Graphics g = Graphics.FromImage(outBmp);g.Clear(Color.White);// 设置画布的描绘质量 g.CompositingQuality = CompositingQuality.HighQuality;g.SmoothingMode = SmoothingMode.HighQuality;g.InterpolationMode = InterpolationMode.HighQualityBicubic;g.DrawImage(img, new Rectangle(0, 0, nWidth, nHeight), 0, 0, nWidth, nHeight, GraphicsUnit.Pixel);int[] sizes = new int[] { 16, 14, 12, 10, 8, 6, 4 };Font crFont = null;SizeF crSize = new SizeF();//通过循环这个数组,来选用不同的字体大小 //如果它的大小小于图像的宽度,就选用这个大小的字体 for (int i = 0; i < 7; i++){//设置字体,这里是用arial,黑体 crFont = new Font("arial", sizes[i], FontStyle.Bold);//Measure the Copyright string in this Font crSize = g.MeasureString(text, crFont);if ((ushort)crSize.Width < (ushort)nWidth){ break; }}//因为图片的高度可能不尽相同, 所以定义了 //从图片底部算起预留了5%的空间 int yPixlesFromBottom = (int)(nHeight * .08);//现在使用版权信息字符串的高度来确定要绘制的图像的字符串的y坐标 float yPosFromBottom = ((nHeight - yPixlesFromBottom) - (crSize.Height / 2));//计算x坐标 float xCenterOfImg = (nWidth / 2);//把文本布局设置为居中 StringFormat StrFormat = new StringFormat();StrFormat.Alignment = StringAlignment.Center;//通过Brush来设置黑色半透明 SolidBrush semiTransBrush2 = new SolidBrush(Color.FromArgb(153, 0, 0, 0));//绘制版权字符串 g.DrawString(text, //版权字符串文本 crFont, //字体 semiTransBrush2, //Brush new PointF(xCenterOfImg + 1, yPosFromBottom + 1), //位置 StrFormat); //设置成白色半透明 SolidBrush semiTransBrush = new SolidBrush(Color.FromArgb(153, 255, 255, 255));//第二次绘制版权字符串来创建阴影效果 //记住移动文本的位置1像素 g.DrawString(text, //版权文本 crFont, //字体 semiTransBrush, //Brush new PointF(xCenterOfImg, yPosFromBottom), //位置 StrFormat);g.Dispose(); // 以下代码为保存图片时,设置压缩质量 EncoderParameters encoderParams = new EncoderParameters();long[] quality = new long[1];quality[0] = 100;EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality);encoderParams.Param[0] = encoderParam; //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();ImageCodecInfo jpegICI = null;for (int x = 0; x < arrayICI.Length; x++){if (arrayICI[x].FormatDescription.Equals("JPEG")){jpegICI = arrayICI[x];//设置JPEG编码 break;}} if (jpegICI != null){ outBmp.Save(file, jpegICI, encoderParams); }else { outBmp.Save(file, thisFormat); }img.Dispose();outBmp.Dispose();File.Delete(strTempFile);}#endregion} }

页面调用:Helper.UpLoadHelper.AttachText("水印文字", FileUpload1.PostedFile.FileName);

2,

/// <summary>/// 增加图片文字水印/// </summary>/// <param name="filename">文件名</param>/// <param name="watermarkText">水印文字</param>/// <param name="watermarkStatus">图片水印位置</param>/// <param name="quality">附加图片质量,1是 0不是</param> public static void AddImageSignText(Image img, string filename, string watermarkText, int watermarkStatus, int quality, string fontname, int fontsize){//System.Drawing.Bitmap bmp = new System.Drawing.Bitmap(img);// .FromFile(filename); Graphics g = Graphics.FromImage(img);Font drawFont = new Font(fontname, fontsize, FontStyle.Regular, GraphicsUnit.Pixel);SizeF crSize;crSize = g.MeasureString(watermarkText, drawFont);float xpos = 0;float ypos = 0;switch (watermarkStatus){case 1:xpos = (float)img.Width * (float).01;ypos = (float)img.Height * (float).01;break;case 2:xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);ypos = (float)img.Height * (float).01;break;case 3:xpos = ((float)img.Width * (float).99) - crSize.Width;ypos = (float)img.Height * (float).01;break;case 4:xpos = (float)img.Width * (float).01;ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);break;case 5:xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);break;case 6:xpos = ((float)img.Width * (float).99) - crSize.Width;ypos = ((float)img.Height * (float).50) - (crSize.Height / 2);break;case 7:xpos = (float)img.Width * (float).01;ypos = ((float)img.Height * (float).99) - crSize.Height;break;case 8:xpos = ((float)img.Width * (float).50) - (crSize.Width / 2);ypos = ((float)img.Height * (float).99) - crSize.Height;break;case 9:xpos = ((float)img.Width * (float).99) - crSize.Width;ypos = ((float)img.Height * (float).99) - crSize.Height;break;}// System.Drawing.StringFormat StrFormat = new System.Drawing.StringFormat();// StrFormat.Alignment = System.Drawing.StringAlignment.Center;//// g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.White), xpos + 1, ypos + 1, StrFormat);// g.DrawString(watermarkText, drawFont, new System.Drawing.SolidBrush(System.Drawing.Color.Black), xpos, ypos, StrFormat); g.DrawString(watermarkText, drawFont, new SolidBrush(Color.White), xpos + 1, ypos + 1);g.DrawString(watermarkText, drawFont, new SolidBrush(Color.Black), xpos, ypos);ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();ImageCodecInfo ici = null;foreach (ImageCodecInfo codec in codecs){if (codec.MimeType.IndexOf("jpeg") > -1){ici = codec;}}EncoderParameters encoderParams = new EncoderParameters();long[] qualityParam = new long[1];if (quality < 0 || quality > 100){quality = 80;}qualityParam[0] = quality;EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qualityParam);encoderParams.Param[0] = encoderParam;if (ici != null){img.Save(filename, ici, encoderParams);}else{img.Save(filename);}g.Dispose();//bmp.Dispose(); img.Dispose();}

页面调用:、
Image img = Image.FromStream(this.FileUpload1.PostedFile.InputStream);
Helper.UpLoadHelper.AddImageSignText(img, FileUpload1.PostedFile.FileName, "水印文字", 1, 100, "宋体", 20);

3,加上LOGO图片水印

/// <summary>/// 加上logo图片水印/// </summary>/// <param name="FilePath">源图片地址</param>/// <param name="SavePath">图片保存路径</param> public static void AddWaterMark(string FilePath, string SavePath,string logoPath){Bitmap bitmap = new Bitmap(FilePath);Graphics g = Graphics.FromImage(bitmap);System.Drawing.Image logo = System.Drawing.Image.FromFile(logoPath);//加载logo图片 if (bitmap.Width < logo.Width || bitmap.Height <= logo.Height)return;//下面定义一个矩形区域,以后在这个矩形里画上透明背景和白色字体 float rectWidth = (float)logo.Width;float rectHeight = (float)logo.Height;float rectX = bitmap.Width - rectWidth;float rectY = bitmap.Height - rectHeight;//声明矩形域 RectangleF textArea = new RectangleF(rectX, rectY, rectWidth, rectHeight);g.DrawImage(logo, textArea);MemoryStream ms = new MemoryStream();//保存为Jpg类型 bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);g.Dispose();bitmap.Dispose();logo.Dispose();FileStream fs = new FileStream(SavePath, FileMode.OpenOrCreate);fs.Write(ms.ToArray(), 0, ms.ToArray().Length);fs.Close();}

页面调用:
Helper.UpLoadHelper.AddWaterMark(FileUpload1.PostedFile.FileName, Server.MapPath("~/images/aa.jpg"), FileUpload2.PostedFile.FileName);

ASP.NET 上传图片添加文字、Logo水印相关推荐

  1. ffmpeg指定位置添加文字图片水印

    FFmpeg命令详解_Android&Java&C-CSDN博客 ​​​​​​利用ffmpeg实现添加图片水印和文字水印,添加多个水印.代码和命令实现及中文水印乱码_浪漫老狼的博客-C ...

  2. ios图片添加文字或者水印

    http://www.tuicool.com/articles/nYNFVj   mark 一般在客户端做图片处理的数量不宜太多,因为受设备性能的限制,如果批量的处理图片,将会带来交互体验性上的一些问 ...

  3. python的pillow给图片加文字_Python-Pillow库给图片添加文字、水印

    apt-get install -y python-PIL 在图片右下角添加文字: Python # -*- coding: utf-8 -*- from PIL import Image, Imag ...

  4. android图片上水印字体颜色,Android给图片添加文字和水印

    话不多说 上图 gif5新文件.gif public class ImageUtil { /** * 设置水印图片在左上角 * * @param context 上下文 * @param src * ...

  5. SpringBoot上传图片+ 添加文字水印

    <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...

  6. 如何用Python给图片添加文字/图片水印的方法,特别简单好用,filestools和Pil模块

    系统环境:Linux Debian 一直想给要发表的图片加上自己的水印,但找了很长时间一直没有合适的方法,最近因疫情关系,所以有机会多花点时间在网上找到各种方案,其中这位网友 "风度78&q ...

  7. PHP添加文字图片水印

    需要注意几点: 1.确保GD库的开启(增加图片水印只确保这一点即可) 2.确保msyh.ttf文件存在并引入 增加文字水印: /*打开图片*///1.配置图片路径 $src = "1.jpg ...

  8. uni-app APP,H5图片上传添加文字水印,图片只显示左上角bug(H5,安卓App可用,其他暂未测试)

    项目场景: 提示:这里简述项目相关背景: uni-app上传图片添加文字水印功能时图片只显示左上角,或者图片重叠 图片不规则一系列bug,特此记录 问题描述 提示:这里描述项目中遇到的问题: 上传图片 ...

  9. 怎么给视频加logo水印?操作简单易上手

    怎么给视频加logo水印?说到给视频添加水印可能会让很多人感到很麻烦,因为在大家的印象中给视频添加水印就是一项非常难的工作,首先需要使用到复杂的视频编辑软件,光学习使用视频编辑软件就需要花上大把的时间 ...

最新文章

  1. 无法使用xcode打出ipa包的解决方法
  2. pandas使用read_csv函数读取文件最后N行数据并保留表头、pandas使用read_csv函数读取网络url链接数据
  3. oracle表空间处理操作
  4. OpenKruise v1.0:云原生应用自动化达到新的高峰
  5. 极光推送JPush的快速集成
  6. java lock 信号_java各种锁(ReentrantLock,Semaphore,CountDownLatch)的实现原理
  7. Chapter7-7_Deep Learning for Coreference Resolution
  8. spring boot 配置动态刷新
  9. 一群阿里人如何用 10 年自研洛神云网络平台?技术架构演进全揭秘!
  10. Android Studio 2.2:新布局、Firebase、OpenJDK以及Java 8
  11. php 去掉无关数据,php 读取 mysql 表中的double数据,去掉多余的0
  12. 【CV学习笔记】ROI与泛洪填充
  13. 工作周记 - 第四周 (2016/06/12 - 2016/06/18) 我没喝多,但是今天话多了 - -
  14. L230 RF可靠性测试-RF指标
  15. php 多城市切换,切换城市
  16. 心电信号质量评估——ecg_qc工具包介绍(二)
  17. 炉石传说一直显示重新连接服务器,炉石传说一直显示战网开启中 炉石传说卡在启动战网解决办法...
  18. MySQL Group Replication的RECOVERING状态深度理解
  19. 借助小程序云开发实现小程序的登陆注册功能
  20. 谁可以参与初创股权分配?

热门文章

  1. chatgpt如何自动生成角色prompt模板
  2. backgroundLinearGradient线性渐变制作折角效果
  3. 前端下载本地excel模板
  4. sendgrid html text,Laravel + SendGrid htmlspecialchars()期望参数1为字符串,给定对象
  5. 复习|typedef什么意思,用法,作用
  6. leetcode 881 救生艇 (python3- 贪心算法)
  7. 英文金曲大赛c语言,英文歌曲_最激情!佐治亚理工开学典礼欢迎辞_沪江英语
  8. C++ if条件语句用法
  9. c++二分法求平方根
  10. 医咖会SPSS免费教程学习笔记—R*C卡方检验