在图片上添加文字水印,其实就是要用到两个类,

using System.Drawing;
             using System.Drawing.Drawing2D;

废话不多说了,直接上代码;代码很容易看懂;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Drawing;
using System.Drawing.Drawing2D;
namespace WebApplication10
{
public enum Postion
{
LeftTop,
RightTop,
LeftButtom,
RightButtom,
Center
} public class Watermark
{
public void AddTextToIMage(string IMagePath,string TextStr,Postion postion,string familyfont,float fontSize)
{
//设置文字水印的区域
float RectX = 0;
float RectY = 0;
float RectWidth = 0;
float RectHeight = 0; //获取图片
System.Drawing.Image image = System.Drawing.Image.FromFile(IMagePath);
System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(image);
Graphics g = Graphics.FromImage(bitmap); if(fontSize>(float)(image.Height/10.0) || fontSize>(float)(image.Width/10.0))
{
fontSize = (fontSize > (float)(image.Height / 10.0)) == true ? (float)(image.Height / 10.0) : (float)(image.Width / 10.0);
} //设置字体
Font font = new Font(familyfont, fontSize);
RectWidth = (fontSize + 2) * TextStr.Length;
RectHeight = fontSize + 4;
StringFormat strFormat = new StringFormat();
strFormat.Alignment = StringAlignment.Center; //设置画刷
Color color = Color.FromArgb(221, 221, 221);
HatchBrush hbbrush = new HatchBrush(HatchStyle.DiagonalCross, color, color); switch(postion)
{
case Postion.LeftTop:
RectX = 0;
RectY = 0;
break;
case Postion.LeftButtom:
RectX = 0;
RectY = image.Height - RectHeight;
break;
case Postion.RightTop:
RectX = image.Width - RectWidth;
RectY = 0;
break;
case Postion.RightButtom:
RectX = image.Width - RectWidth;
RectY = image.Height - RectHeight;
break;
case Postion.Center:
RectX = (image.Width - RectWidth) / 2;
RectY = (image.Height - RectHeight) / 2;
break; } RectangleF rectf = new RectangleF(RectX, RectY, RectWidth, RectHeight);
g.DrawString(TextStr, font, hbbrush, rectf, strFormat); System.IO.MemoryStream ms = new System.IO.MemoryStream();
bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg); System.Web.HttpContext.Current.Response.Clear();
System.Web.HttpContext.Current.Response.ContentType = "image/jpeg";
System.Web.HttpContext.Current.Response.BinaryWrite(ms.ToArray()); g.Dispose();
bitmap.Dispose();
image.Dispose(); }
}
}

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls; namespace WebApplication10
{
public partial class _default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Watermark watermark = new Watermark();
watermark.AddTextToIMage(Server.MapPath("/Images/sss.JPG"), "你好2016!", Postion.Center, "宋体", 15.0f);
}
}
}

  

 结果演示图:

如何生成log新信息背景图片和在图片上添加水印的更多相关文章

  1. iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍

    一.快速生成沙盒目录的路径 沙盒目录的各个文件夹功能 - Documents - 需要保存由"应用程序本身"产生的文件或者数据,例如:游戏进度.涂鸦软件的绘图 - 目录中的文件会被 ...

  2. .NET 采用 SkiaSharp 生成二维码和图形验证码及图片进行指定区域截取方法实现

    在最新版的 .NET 平台中,微软在逐步放弃 System.Drawing.Imaging ,给出的理由如下: System.Drawing命名空间对某些操作系统和应用程序类型有一些限制. 在Wind ...

  3. PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转

    [强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...

  4. 接收新信息,在会话中看不到(thread表数据插入/更新失败)

    分析原因:收到短信,sms表插入信息,触发器会自动更新thread表,更新失败导致一直有一条未读信息数量显示,但在会话列表中却看不到. (偶现,低概率. 解决方法:接收新信息插入后,立即查询threa ...

  5. 关于点击ztree的节点将页面生成到easyui的新增选项卡(easyui-tabs)时,总是在浏览器中生成一个新的页面的问题

    最近的项目中用到了easyui,还有ztree菜单.在这里将我遇到的一些问题写出来算是做个笔记吧. 这是我头一次在博客园里分享代码,我的处女作,写的不好的地方还望各位见谅! 由于很久没有写过前台的东西 ...

  6. C#图片切割、图片压缩、缩略图生成

    C#图片切割.图片压缩.缩略图生成的实现代码 /// 图片切割函数  /// </summary>  /// <param name="sourceFile"&g ...

  7. powershell 生成随机用户信息

    #生成随机用户信息,包括姓名.QQ.邮箱,手机号 $nameArr=@('一','丁','三','专','世','业','丝','中','丰','临','丹','丽','举','乃','义', '乐' ...

  8.  sublime text3快速生成html头部信息(转)

    sublime text3快速生成html头部信息  https://blog.csdn.net/sunshinegirl_7/article/details/49802579 经常见别人创建新的ht ...

  9. RF根据单个/多个output文件重新生成log和report文件

    场景1:根据单个output文件重新生成log和report文件命令: rebot -d 日志和报告文件保存路径  output.xml文件 场景2:合并两个不同路径下的output文件并生成新的lo ...

随机推荐

  1. Tip.It诞生记

    灵光一闪 之所以想做 Tip.It,完全是受自己初到北京时找房子各种痛苦的启发,当时为了跳过中介租房子,去58同城,豆瓣等各种网站去看房子,比如说下图就是一个典型的58同城的租房页面. 可以看到电话号 ...

  2. 顶尖数据挖掘辅助教学套件(TipDM-T6)产品白皮书

          顶尖数据挖掘辅助教学套件 (TipDM-T6)           产  品  说  明  书 广州泰迪智能科技有限公司 版权所有 地址: 广州市经济技术开发区科学城232号 网址: ht ...

  3. Hadoop源码解析之: HBase Security

    文不打算对这部分代码进行全面的解读,而是先对几个主要类的职能进行概述,然后再罗列一些有价值的重要细节. 第一部分:HBase Security 概述 HBase Security主要是基于User和U ...

  4. -DDEBUG编译标记

    想必大家都有利用输出函数如printf来帮助我们调试程序的经历,这是一种比较原始的程序调试辅助方法,在Linux下也可以为我们所用.不过这种方法有一个明显的缺点,就是在调试完后我们必须注释或删除掉这些 ...

  5. Metropolis Hasting算法

    Metropolis Hasting Algorithm: MH算法也是一种基于模拟的MCMC技术,一个非常重要的应用是从给定的概率分布中抽样.主要原理是构造了一个精妙的Markov链,使得该链的稳态 ...

  6. Position详解---转

    position有四个属性值: relative absolute fixed static 下面分别讲述这四个属性. 1. relative relative属性,相对定位,我们要搞清它是相对哪个对 ...

  7. Fiddler 跟踪 手机页面数据包

    随着 HTML5 的急速增长,现在越来越多的人,开始涉及到移动终端的 Web 开发领域,但手机端始终没有 PC 端这么多的调试工具.即使 PC 端浏览器模拟 user-agent 进行开发,也可能会发 ...

  8. asp.net 跨页面传值常用方法

    常用方法有以下: 1.queryString 2.form-post控件传递 3.cookie 4.application 5.session querystring: http://website. ...

  9. Web--->>>Cookie与Session

    1.cookie 1.cookie是存在客户端(浏览器)的进程内存中和客户端所在的机器硬盘上 2.cookie只能能够存储少量文本,大概4K大小 3.cookie是不能在不同浏览器之间共享 3.创建c ...

  10. 操作Sql数据库帮助类

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...