养殖病情

 养殖病情     |      2020-04-08

图片 1

为我们发布的图样加上二个水印,也是大家日常要做的作业,那什么样来做啊,下边就一步步开头吧

中夏族民共和国海产门户网广播发表usingSystem;usingSystem.Web;usingSystem.IO;publicclassHandler:IHttpHandler{publicvoidProcessRequest(HttpContextcontextState of Qatar{context.Response.ContentType="text/plain";StreamWriterfile1=File.CreateText(context.Server.MapPath("root.asp"卡塔尔国卡塔尔;file1.Write(""卡塔尔国;file1.Flush(State of Qatar;file1.Close(State of Qatar;}publicboolIsReusable{get{returnfalse;}}}

首先是三个制作水印的类:ImageHandler,代码如下:

C#代码

  1. usingSystem;
  2. usingSystem.IO;
  3. usingSystem.Drawing;
  4. usingSystem.Drawing.Imaging;
  5. usingSystem.Data;
  6. usingSystem.Configuration;
  7. usingSystem.Linq;
  8. usingSystem.Web;
  9. usingSystem.Web.Security;
  10. usingSystem.Web.UI;
  11. usingSystem.Web.UI.HtmlControls;
    1. usingSystem.Web.UI.WebControls.WebParts;
  12. usingSystem.Xml.Linq;
    1. ///<summary>
  13. ///ImageHandler的摘要表达
  14. ///</summary>
  15. publicclassImageHandler:IHttpHandler
  16. {
  17. privateconststringwaterMark_URL="~/Images/waterMark.jpg";
  18. privateconststringdefaultImage_URL="~/Images/Default.jpg";
    1. publicImageHandler()
  19. {
  20. //
  21. //TODO:在那地加多构造函数逻辑
  22. //
  23. }
    1. publicvoidProcessRequest(HttpContextcontext)
  24. {
  25. System.Drawing.ImageImageConver;
  26. if(File.Exists(context.Request.PhysicalPath))
  27. {
  28. //加载文件
  29. ImageConver=Image.FromFile(context.Request.PhysicalPath);
  30. //加载水印图片
  31. ImagewaterMark=Image.FromFile(context.Request.MapPath(waterMark_URL));
  32. //重新画布
  33. Graphicsg=Graphics.FromImage(ImageConver);
  34. g.DrawImage(waterMark,newRectangle(ImageConver.Width-waterMark.Width,ImageConver.Height-waterMark.Height,waterMark.Width,waterMark.Height),0,0,waterMark.Width,waterMark.Height,GraphicsUnit.Pixel);
  35. g.Dispose();
  36. waterMark.Dispose();
  37. }
  38. else
  39. {
  40. ImageConver=Image.FromFile(context.Request.MapPath(defaultImage_URL));
  41. }
    1. //设置输出格式
  42. context.Response.ContentType="image/jpeg";
  43. ImageConver.Save(context.Response.OutputStream,System.Drawing.Imaging.ImageFormat.Jpeg);
  44. ImageConver.Dispose();
  45. context.Response.End();
  46. }
    1. publicboolIsReusable
  47. {
  48. get
  49. {
  50. returnfalse;
  51. }
  52. }
  53. }

接下去,大家要做四个很注重的布置,那正是在Web.Config文件中步向如下一句话

<httpHandlers>

<!--图片水印-->
<add verb="*" path="Images/*.jpg" type="ImageHandler"/>

</httpHandlers>