c#通过httphandler添加水印的代码

发布时间:2019-08-08编辑:脚本学堂
c#使用httphandler添加水印的代码,有需要的朋友可以参考下。

示例代码:
 

复制代码 代码示例:
HttpRequest request = context.Request;
 HttpServerUtility server = context.Server;
 string Water =server.MapPath("~/vener.png");
 HttpResponse response = context.Response;
 string s = request.PhysicalPath;
 response.ContentType = "Images/jpeg";
 if (File.Exists(s) && request.UrlReferrer!=null&& request.UrlReferrer.ToString() == "http://localhost/")
 {
     Image image = Image.FromFile(s);
     Image wimage =Image.FromFile(Water);
     Graphics gs = Graphics.FromImage(image);
     gs.DrawImage(wimage,new Rectangle(image.Width-wimage.Width,image.Height-wimage.Height,wimage.Width,wimage.Height));
     gs.Dispose();
     image.Save(response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
 }
 else
 {
     Image image = Image.FromFile(Water);
     image.Save(response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
     image.Dispose();
 }

webconfig配置文件
 

复制代码 代码示例:

 <system.web>
    <httpHandlers>
      <add path="Images/*.JPG" type="MyHttpHandler" verb="*" />
    </httpHandlers>

  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <add name="Handler" path="Images/*.jpg" verb="*" type="MyHttpHandler" resourceType="Unspecified" requireAccess="None" preCondition="integratedMode" />
    </handlers>
  </system.webServer>