第一步,添加一个一般处理程序(Handler),本例是ImageHandler
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mime;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
/// <summary>
/// Summary description for ImageHandler
/// </summary>
public
class
ImageHandler : IHttpHandler
{
public
ImageHandler()
{
}
public
string GetContentType(String path)
{
switch
(Path.GetExtension(path))
{
case
".bmp"
:
return
"Image/bmp"
;
case
".gif"
:
return
"Image/gif"
;
case
".jpg"
:
return
"Image/jpeg"
;
case
".png"
:
return
"Image/png"
;
default
:
break
;
}
return
String.Empty;
}
public
ImageFormat GetImageFormat(String path)
{
switch
(Path.GetExtension(path).ToLower())
{
case
".bmp"
:
return
ImageFormat.Bmp;
case
".gif"
:
return
ImageFormat.Gif;
case
".jpg"
:
return
ImageFormat.Jpeg;
case
".png"
:
return
ImageFormat.Png;
default
:
return
null
;
}
}
protected
byte
[] WatermarkImage(HttpContext context)
{
byte
[] imageBytes =
null
;
if
(File.Exists(context.Request.PhysicalPath))
{
// Normally you'd put this in a config file somewhere.
string watermark =
"世复检测"
;
Image image = Image.FromFile(context.Request.PhysicalPath);
Graphics graphic;
if
(image.PixelFormat != PixelFormat.Indexed && image.PixelFormat != PixelFormat.Format8bppIndexed && image.PixelFormat != PixelFormat.Format4bppIndexed && image.PixelFormat != PixelFormat.Format1bppIndexed)
{
// Graphic is not a Indexed (GIF) image
graphic = Graphics.FromImage(image);
}
else
{
/* Cannot create a graphics object from an indexed (GIF) image.
* So we're going to copy the image into a new bitmap so
* we can work with it. */
Bitmap indexedImage =
new
Bitmap(image);
graphic = Graphics.FromImage(indexedImage);
// Draw the contents of the original bitmap onto the new bitmap.
graphic.DrawImage(image,
0
,
0
, image.Width, image.Height);
image = indexedImage;
}
graphic.SmoothingMode = SmoothingMode.AntiAlias & SmoothingMode.HighQuality;
Font myFont =
new
Font(
"Arial"
,
15
);
SolidBrush brush =
new
SolidBrush(Color.FromArgb(
255
, Color.Red));
/* This gets the size of the graphic so we can determine
* the loop counts and placement of the watermarked text. */
SizeF textSize = graphic.MeasureString(watermark, myFont);
Write the text across the image.
//for (int y = 0; y < image.Height; y++)
//{
// for (int x = 0; x < image.Width; x++)
// {
// PointF pointF = new PointF(x, y);
// graphic.DrawString(watermark, myFont, brush, pointF);
// x += Convert.ToInt32(textSize.Width);
// }
// y += Convert.ToInt32(textSize.Height);
//}
// Write the text at the right bottom of the image.
for
(
int
y = image.Height-
25
; y < image.Height; y++)
{
for
(
int
x = image.Width-
100
; x < image.Width; x++)
{
PointF pointF =
new
PointF(x, y);
graphic.DrawString(watermark, myFont, brush, pointF);
x += Convert.ToInt32(textSize.Width);
}
y += Convert.ToInt32(textSize.Height);
}
using (MemoryStream memoryStream =
new
MemoryStream())
{
image.Save(memoryStream, GetImageFormat(context.Request.PhysicalPath));
imageBytes = memoryStream.ToArray();
}
}
return
imageBytes;
}
#region IHttpHandler Members
public
bool IsReusable
{
get {
return
false
; }
}
public
void
ProcessRequest(HttpContext context)
{
context.Response.Clear();
context.Response.ContentType = GetContentType(context.Request.PhysicalPath);
byte
[] imageBytes = WatermarkImage(context);
if
(imageBytes !=
null
)
{
context.Response.OutputStream.Write(imageBytes,
0
, imageBytes.Length);
}
else
{
// No bytes = no image which equals NO FILE.
// Therefore send a 404 - not found response.
context.Response.StatusCode =
404
;
}
context.Response.End();
}
#endregion
}
|
第二步,在web.config里添加如下代码:
1
2
3
4
|
<httphandlers>
<!---->
</add></httphandlers>
|
第三步,调试在每一个图片的右下角显示红色的“世复检测”四个字,具体显示请查看如下连接:
点击打开链接