如何生成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 ...
随机推荐
- 【LeetCode练习题】Partition List
Given a linked list and a value x, partition it such that all nodes less than x come before nodes gr ...
- x/nfu-用gdb查看内存
用gdb查看内存 2007-12-08 12:43 用gdb查看内存 格式: x /nfu <addr> 说明x 是 examine 的缩写 n表示要显示的内存单元的个数 f表示显示方式, ...
- 虚拟机使用PuTTY、SSH Secure Shell Client前的配置
1 仅主机模式 2 检查子网IP 为192.168.154.0 3 进入虚拟机,检查IP地址 ip addr show 添加IP地址 ip addr add dev eno16777736 192.1 ...
- 第一次用shell脚本来自动运行带参程序
将目录下的part-开头的文件带入程序处理 #!bin/sh FILES=/data5/follow_relation_list_part_00000/part-* file="" ...
- 使用ObjectAnimator开发打开、关闭书本动画
动画效果 动画效果-分享链接 (想做成gif图的,尝试各种工具无果) ObjectAnimator简单介绍及实现思路 ObjectAnimator是从api level 11 (Android3.0x ...
- Android----获取activity上所有的控件
/** * @note 获取该activity所有view * @author liuh * */ public List< ...
- SqLiter
1.去重 select * from daydata where wtid||rectime in (select wtid||rectime from daydata group by wtid| ...
- 仅当使用了列的列表并且 IDENTITY_INSERT 为 ON 时,才能为表'SpeType'中的标识列指定显式值
尊重原著作:本文转载自http://blog.163.com/lao12qi12345%40126/blog/static/1179155120101122113316187/ 情况描述 在表Tab ...
- Oracle事物基础
事务 1 事务定义 数据库事务是SQL语句的组合作为一个"工作单元".要么全部完成,要么全部不做. 每个事务都有一个开始和一个结束. 2 事务开始 1. 你连接到数据库并执行DML ...
- 实现一个对象验证库系列 -- 3) Fluent以及扩展方法实现 (请大神批评)
前情回顾: 上一篇 2) 验证器实现 简单描述了下验证器的简单实现 本文将说说Fluent方式的实现,欢迎大神们指点指点 3) Fluent以及扩展方法实现 我们按照之前 Fluent 的设想以及我们 ...