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

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. 创建FBI树

    需求:数串由2^n个'0' '1'数串组成,对于一个数串,有01混合出现,则视为F,全0数串为B,全1数串为I. 将给定数串进行切割,如10010011可以用二叉树表示为 F(10010011) / ...

  2. hdu 1757 A Simple Math Problem_矩阵快速幂

    题意:略 简单的矩阵快速幂就行了 #include <iostream> #include <cstdio> #include <cstring> using na ...

  3. IE 下a标签在 position:absolute 后无法点击的问题

    IE 下 a 标签在 position:absolute 后无法点击的问题 条件 : DOCTYPE 为 XHTML. IE 浏览器 现象 : 将 a 的 position 指定为 absolute, ...

  4. 安装android studio时,解决unable to access android sdk add-on list

    1.打开时提示如下错误. 2.在安装路径下找到idea.properties文件,用记事本打开,添加disable.android.first.run=true,保存即可. 3.再次打开Android ...

  5. 类加载器与methodinterceptor接口

    类加载器: JVM将类加载过程分为三个步骤: 装载(Load):加载二进制文件 链接(Link)进行了验证:验证文件准确性 准备:将静态变量进行分配内存,初始化其默认值. 解析:符号引用转换为直接引用 ...

  6. OC基础1:一些基本概念

    "OC基础"这个分类的文章是我在自学Stephen G.Kochan的<Objective-C程序设计第6版>过程中的笔记. 1.关于类方法和实例方法: (1).类方法 ...

  7. Javascript 中的false,零值,null,undefined和空字符串对象

    在Javascript中,我们经常会接触到题目中提到的这5个比较特别的对象--false.0.空字符串.null和undefined.这几个对象很容易用错,因此在使用时必须得小心. 类型检测 我们下来 ...

  8. taobao面试要点

    第一: 其中有几个点必问,JVMGC深层机制.类加载,包括Tomcat和Jboss的.线程相关的如离线锁,互斥同步,java主线程和工作线程机制,concurrent包下的锁和sync关键字一些区别, ...

  9. Chrome浏览器切换到之前打开的标签页会重新加载

    这是谷歌的一种策略.当系统内存不足时,系统会自动从内存中舍弃标签页 在地址栏输入chrome://flags/#automatic-tab-discarding,设置为停用即可.

  10. 《think in python》学习-9

    think in python think in python -9 案例分析:文字游戏 从文本文件中读取文字 作者提供了一个文本文件words.txt 地址 本章后面案例也会用带该文件中的词组 fi ...