如何生成log新信息背景图片和在图片上添加水印
在图片上添加文字水印,其实就是要用到两个类,
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新信息背景图片和在图片上添加水印的更多相关文章
- iOS开发——多线程篇——快速生成沙盒目录的路径,多图片下载的原理、SDWebImage框架的简单介绍
一.快速生成沙盒目录的路径 沙盒目录的各个文件夹功能 - Documents - 需要保存由"应用程序本身"产生的文件或者数据,例如:游戏进度.涂鸦软件的绘图 - 目录中的文件会被 ...
- .NET 采用 SkiaSharp 生成二维码和图形验证码及图片进行指定区域截取方法实现
在最新版的 .NET 平台中,微软在逐步放弃 System.Drawing.Imaging ,给出的理由如下: System.Drawing命名空间对某些操作系统和应用程序类型有一些限制. 在Wind ...
- PHPThumb处理图片,生成缩略图,图片尺寸调整,图片截取,图片加水印,图片旋转
[强烈推荐]下载地址(github.com/masterexploder/PHPThumb). 注意这个类库有一个重名的叫phpThumb,只是大小写的差别,所以查找文档的时候千万注意. 在网站建设过 ...
- 接收新信息,在会话中看不到(thread表数据插入/更新失败)
分析原因:收到短信,sms表插入信息,触发器会自动更新thread表,更新失败导致一直有一条未读信息数量显示,但在会话列表中却看不到. (偶现,低概率. 解决方法:接收新信息插入后,立即查询threa ...
- 关于点击ztree的节点将页面生成到easyui的新增选项卡(easyui-tabs)时,总是在浏览器中生成一个新的页面的问题
最近的项目中用到了easyui,还有ztree菜单.在这里将我遇到的一些问题写出来算是做个笔记吧. 这是我头一次在博客园里分享代码,我的处女作,写的不好的地方还望各位见谅! 由于很久没有写过前台的东西 ...
- C#图片切割、图片压缩、缩略图生成
C#图片切割.图片压缩.缩略图生成的实现代码 /// 图片切割函数 /// </summary> /// <param name="sourceFile"&g ...
- powershell 生成随机用户信息
#生成随机用户信息,包括姓名.QQ.邮箱,手机号 $nameArr=@('一','丁','三','专','世','业','丝','中','丰','临','丹','丽','举','乃','义', '乐' ...
- sublime text3快速生成html头部信息(转)
sublime text3快速生成html头部信息 https://blog.csdn.net/sunshinegirl_7/article/details/49802579 经常见别人创建新的ht ...
- RF根据单个/多个output文件重新生成log和report文件
场景1:根据单个output文件重新生成log和report文件命令: rebot -d 日志和报告文件保存路径 output.xml文件 场景2:合并两个不同路径下的output文件并生成新的lo ...
随机推荐
- C++中基类的析构函数为什么要用virtual虚析构函数
知识背景 要弄明白这个问题,首先要了解下C++中的动态绑定. 关于动态绑定的讲解,请参阅: C++中的动态类型与动态绑定.虚函数.多态实现 正题 直接的讲,C++中基类采用virtual虚析构函数是 ...
- jQuery中设置form表单中action值的方法
jQuery中设置form表单中action值的方法 (2011-03-17 10:18:19) 转载▼ 标签: 杂谈 html代码: <form id="myFormId&quo ...
- Win8开发疑问与解答
疑问:怎样获取开发者许可证 打开VS2012时,怎么在没有取得开发者许可证之前,屏蔽/跳过弹出的窗体“获取Windows8开发者许可证 你需要具有开发者许可证才能开发适用于......” 打开Blen ...
- hdu4135Co-prime 容斥原理水题
//问一个区间[a,b]与n互素的数的个数 //利用容斥原理可知 //在[a,b] 区间内对n的素数因子 //ans = 被一个数整除的数的个数 - 被两个数的最小公倍数整除的数的个数 + 被三个数的 ...
- RMAN备份之非归档模式下的备份
Backing Up a Database in NOARCHIVELOG Mode:1.Log into RMAN2.Shutdown immediate from RMAN3.Startup mo ...
- ORACLE 热备begin backup / end backup
执行begin backup之后,oracle会把将要备份的数据文件都标记为hot-backup-in-progress,锁定所要备份的datafile header的scn,例如此时scn=100, ...
- css系列教程--css文件的创建
css文件的创建:1.外部样式表:<link rel="stylesheet" type="text/css" href="mystyle.cs ...
- node.js url模块
URL url.parse(urlStr[, parseQueryString][, slashesDenoteHost]) url.format(urlObj) url.resolve(from, ...
- VMware linux 增加根目录空间 (使用图形分区工具gparted LiveCd)
写这篇文章的原因: 最近要给服务器Centos上的ruby版本升级,由于是第一次升级,不敢直接在服务器上操作. 所以在我的winxp上装了Vmware ,又在Vmware中装了Centos5.2. 用 ...
- 08JS高级 ——“继承”
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...