本文实例讲述了asp.net继承IHttpHandler接口实现给网站图片添加水印功能。分享给大家供大家参考,具体如下:
先展示图片效果:
1. 在App_Code下添加类文件,命名为ImageSY 文件内容如下
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
public class ImageSY : IHttpHandler
{
public ImageSY()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
#region IHttpHandler 成员
public bool IsReusable
{
get { return true ; }
}
public void ProcessRequest(HttpContext context)
{
//获得请求的物理图片路径
string imagePath = context.Request.PhysicalPath;
System.Drawing.Image image = null ;
if (File.Exists(imagePath))
{
//定义水印文字
string text = "本图片来至我的网站" ;
//定义水印文字字体大小
int fontSize = 22;
//水印文字字体
Font font = new Font( "宋体" , fontSize);
//根据图片物理地址加载图片
image = System.Drawing.Image.FromFile(imagePath);
Graphics g = Graphics.FromImage(image);
//获取要绘制水印文字所需要的显示区域大小
SizeF size = g.MeasureString(text, font);
if (size.Width > image.Width || size.Height > image.Height)
{
}
else
{
Brush brush = Brushes.Red;
g.DrawString(text, font, brush, image.Width - size.Width, image.Height - size.Height);
g.Dispose();
}
}
else
{
}
image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
#endregion
}
|
2. 配置WebConfig,添加Location新节点
?
1
2
3
4
5
6
7
8
9
10
|
< location path = "images" >
< system.web >
< httpHandlers >
<!---对jpg文件添加水印-->
< add verb = "*" type = "ImageSY" path = "*.jpg" />
< add verb = "*" type = "ImageSY" path = "*.gif" />
< add verb = "*" type = "ImageSY" path = "*.bmp" />
</ httpHandlers >
</ system.web >
</ location >
|
希望本文所述对大家asp.net程序设计有所帮助。
相关文章
猜你喜欢
- 64M VPS建站:怎样选择合适的域名和SSL证书? 2025-06-10
- 64M VPS建站:怎样优化以提高网站加载速度? 2025-06-10
- 64M VPS建站:是否适合初学者操作和管理? 2025-06-10
- ASP.NET自助建站系统中的用户注册和登录功能定制方法 2025-06-10
- ASP.NET自助建站系统的域名绑定与解析教程 2025-06-10
TA的动态
- 2025-07-10 怎样使用阿里云的安全工具进行服务器漏洞扫描和修复?
- 2025-07-10 怎样使用命令行工具优化Linux云服务器的Ping性能?
- 2025-07-10 怎样使用Xshell连接华为云服务器,实现高效远程管理?
- 2025-07-10 怎样利用云服务器D盘搭建稳定、高效的网站托管环境?
- 2025-07-10 怎样使用阿里云的安全组功能来增强服务器防火墙的安全性?
快网idc优惠网
QQ交流群
您的支持,是我们最大的动力!
热门文章
-
laravel通过创建自定义artisan make命令来新建类文件详解
2025-05-27 101 -
2025-05-25 88
-
Win11系统安装不了SolidWorks怎么解决?Win11不能安装SolidWorks解决办法
2025-05-27 53 -
2025-05-29 48
-
2025-05-27 88
热门评论