对网页全部的图片加水印的方式有 2 种:javascript
Html 页面:html
<body>
<!-- 图片 src 属性请求了一个服务器端的通常处理程序 -->
<img src="AddWaterMarkDynamic.ashx?name=sky11.jpg" alt="" />
</body>
通常处理程序:java
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
string msg = "Hello World!";
string imgName = context.Request.QueryString["name"];
if (!string.IsNullOrEmpty(imgName))
{
string imgPath = context.Server.MapPath("upload/image/" + imgName);
using (Image img = Bitmap.FromFile(imgPath))
{
// 建立一个绘图者,并指定绘制图像
using (Graphics g = Graphics.FromImage(img))
{
g.DrawString(msg, new Font("Verdana", 12, FontStyle.Bold), Brushes.Black, new PointF(5, 8));
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
效果:浏览器
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
string msg = "Hello World!";
string imgName = context.Request.QueryString["name"];
if (!string.IsNullOrEmpty(imgName))
{
string imgPath = context.Server.MapPath("upload/image/" + imgName);
using (Image img = Bitmap.FromFile(imgPath))
{
string water = context.Server.MapPath("upload/image/water.jpg");
using (Image waterImg = Image.FromFile(water))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawImage(waterImg, (img.Width - waterImg.Width) / 2, (img.Height - waterImg.Height) / 2);
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
}
}
此示例很简单,可优化的部分有不少,好比重构成一个水印工厂类,来作到精确控制水印内容、位置、文字水印仍是图片水印、透明度等等。缓存
在接收到用户上传后的文件,若是文件类型是图片类型,你在保存原图的同时可能还须要保存一张缩略图。服务器
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "text/html";
HttpPostedFile file = context.Request.Files[0];
if (file.ContentLength > 0)
{
string extName = Path.GetExtension(file.FileName);
string fileName = Guid.NewGuid().ToString();
string fullName = fileName + extName;
string phyFilePath = context.Server.MapPath("~/Upload/Image/") + fullName;
file.SaveAs(phyFilePath);
context.Response.Write("上传成功!文件名:" + fullName + "<br />");
if (file.ContentType.Contains("image"))
{
context.Response.Write(string.Format("<img src='Upload/Image/{0}'/><br />", fullName));
using (Image img = Bitmap.FromStream(file.InputStream))
{
using (Bitmap smallImg = new Bitmap(120, 40))
{
using (Graphics g = Graphics.FromImage(smallImg))
{
g.DrawImage(img, new Rectangle(0, 0, smallImg.Width, smallImg.Height),
new Rectangle(0, 0, img.Width, img.Height), GraphicsUnit.Pixel);
smallImg.Save(context.Server.MapPath("~/Upload/Image/") + fileName + "_small" + extName);
context.Response.Write(string.Format("缩略图保存成功!<br />"));
context.Response.Write(string.Format("<img src='Upload/Image/{0}'/><br />", (fileName + "_small" + extName)));
}
}
}
}
}
}
通常处理程序中要使用 Session 对象,必须实现一个标记接口:IRequiresSessionState 。微软在后期的开发,对于标记都采用了 Attribute 属性,但在开发 Session 时,仍是启用了空的标记接口的作法。app
public class ValidateCode : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
context.Response.ContentType = "image/jpeg";
string code = CreateRandomNumbers();
context.Session["code"] = code;
using (Bitmap img = new Bitmap(100, 40))
{
using (Graphics g = Graphics.FromImage(img))
{
g.DrawString(code, new Font("微软雅黑", 16), Brushes.White, 15, 7);
img.Save(context.Response.OutputStream, ImageFormat.Jpeg);
}
}
}
public string CreateRandomNumbers()
{
Random r = new Random();
string str = string.Empty;
for (int i = 0; i < 5; i++)
{
str += r.Next(10).ToString();
}
return str;
}
public bool IsReusable
{
get
{
return false;
}
}
}
在前台页面实现点击图片生成新的验证码效果,只需加上一小段 js 代码,用来操纵图片的 src 属性便可(随机数使浏览器的缓存机制失效):dom
<script type="text/javascript">
window.onload = function () {
document.getElementById("vCodeImg").onclick = function () {
this.src = "ValidateCode.ashx?a=" + new Date();
};
}
</script>