C#添加文字水印

 1 <head>
 2     <title>图片添加水印</title>
 3     <script src="../Scripts/jquery-1.7.1.js"></script>
 4     <script type="text/javascript">
 5         $(function(){
 6             $(":file").change(function () {
 7 
 8             //    var fileName = $(this).val();
 9             //    var ext = fileName.substr(fileName.lastIndexOf("."));
10 
11             //    if (ext==".jpg" || ext ==".jpeg" || ext ==".png" || ext==".gif") {
12             //        return true;
13             //    } else {
14             //        $(this).val("");
15             //        alert("请上传正确格式的文件");
16             //    } 
17             });
18 
19             $("#submit").click(function () {
20 
21                 var fileName = $(":file").val();
22 
23                 if (fileName.length>0) {
24                     var ext = fileName.substr(fileName.lastIndexOf(".")); 
25                     if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif") {
26                         return true;
27                     } else {
28                         $(":file").val("");
29                         alert("请上传正确格式的文件");
30                         return false;
31                     }
32                 } else {
33                     alert("请选择需要上传的文件!");
34                     return false;
35                 }
36               
37                  
38             });
39 
40 
41         })
42 
43 
44     </script>
45 
46 
47 
48 </head>
49 <body>
50     <form action="ImageProcess.ashx" method="post" enctype="multipart/form-data">
51         <input type="file" name="imgFile"  />
52         <input type="submit" id="submit" value=" 上传" />
53 
54     </form>
55 </body>
 1    context.Response.ContentType = "text/html";
 2 
 3             HttpPostedFile file = context.Request.Files["imgFile"];
 4 
 5             if (file!=null)
 6             {
 7 
 8                 //把上传的文件做成一个image对象 
 9                 Image img = Image.FromStream(file.InputStream);
10           //在图片上创建画布
11                 Graphics graphics = Graphics.FromImage(img);
12 
13                 //水印文字
14                 string str = "天天看天下";
15                 graphics.DrawString(str, new Font("黑体", 24), new SolidBrush(Color.Red), new PointF(img.Width - (24 * (str.Length+1)), img.Height - 24));
16 
17                 string strPath = "/Upload" + Guid.NewGuid().ToString() + file.FileName;
18 
19                 //保存图片
20                 img.Save(context.Request.MapPath(strPath),ImageFormat.Jpeg);
21                 string strHtml = string.Format( "<html><head></head><body><img  src='{0}'/></body></html>",strPath);
22 
23                 //显示图片
24 
25                 context.Response.Write(strHtml);
26             }
27             else
28             {
29                 context.Response.End();
30             }
原文地址:https://www.cnblogs.com/allenzhang/p/6733476.html