如何生成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 ...
随机推荐
- Centos下编译Linux内核
Linux内核编译是一件简单却费事的事.但是独立的编译linux内核会帮助你很好的理解Linux内核的工作机理. 首先编译linux内核我们需要在当前linux操作系统下安装gcc编译器,因为我是Ce ...
- 电子科大POJ "a^b"
a ^ b Time Limit: 3000/1000MS (Java/Others) Memory Limit: 65535/65535KB (Java/Others) C-sources: ...
- Redis 实践笔记
本文来自:http://www.cnblogs.com/me-sa/archive/2012/03/13/redis-in-action.html 最近在项目中实践了一下Redis,过程中遇到并解决了 ...
- 深入浅出CChart 每日一课——第十六课 实习之旅,百年老店之新锐WTL
上节课笨笨给大家介绍了CChart在微软MFC框架下的应用,本节课的内容仍然和百年老店微软相关,只不过主角换成WTL了. 不了解WTL的同学可以先找度娘温习一下.度娘在怀,今生何求.郎君啊,你是不是闷 ...
- 获取oracle sql语句中绑定变量值的方法
在诊断 sql的性能问题时,我们有时候须要获取其绑定变量的实际值,然后将此实际值带入到sql语句其中,用原来的sql构成select语句(带where条件),实际的运行一下,看一下选择性怎样. 本文就 ...
- LeetCode Day1
Palindrome Linked List /** * LeetCode: Palindrome Linked List * Given a singly linked list, determin ...
- DevExpress GridView使用技巧之列标题点击事件
在这里使用GridView的MouseDown事件.这里同样使用的是GridHitInfo来获取点击位置的信息,来判断是否在列标题上.GridHitInfo根据鼠标点击的x.y坐标获取该点的相关信息, ...
- asp.net中的绝对路径和相对路径
一.关于相对路径和绝对路径相对路径转绝对路径一般,我们在ASP.NET网站中往往需要把一个相对路径转化为绝对路径.通常是用Server.MapPath()方法.比如网站根目录下有 个"Upl ...
- javascript 【封装AJAX】
post function createXHR() { if (typeof XMLHttpRequest != 'undefined') { return new XMLHttpRequest(); ...
- Free Sql Server SMSS format Plugin
免费Sql Server 格式化插件 http://www.apexsql.com/sql_tools_refactor.aspx http://architectshack.com/PoorMans ...