最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

ASP.NET實(shí)現(xiàn)圖片自動添加水印

 更新時間:2020年05月27日 16:25:50   作者:南 墻  
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)圖片自動添加水印,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了ASP.NET實(shí)現(xiàn)圖片自動添加水印的具體代碼,供大家參考,具體內(nèi)容如下

先建一個類,感覺注釋已經(jīng)很詳細(xì)了,有不懂的歡迎評論

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Web;

namespace shuiyin
{
 public class Water : IHttpHandler
 {
  /*
   這個IsReusable的true是可以提高效率但是,會線程不安全
   IHttpHandler實(shí)例可以再次使用

   false,會安全一些,效率會低一些
   IHttpHandler的實(shí)例就不能使用 
    */
  public bool IsReusable => true;
  //水印
  private const string Water_Url = "~/Images/watermark.png";
  //沒有圖片的時候使用
  private const string None_Picture = "~/Error/default.jpg";

  public void ProcessRequest(HttpContext context)
  {
   //獲取圖片的物理路徑
   string path = context.Request.PhysicalPath;
   Image image;
   //如果我當(dāng)前項(xiàng)目中有這個圖片,就可以進(jìn)行加水印操作
   if (File.Exists(path))
   {
    //獲取指定的圖片(要添加水印的圖片)
    image = Image.FromFile(path);
    //再找到,要添加的水印
    Image image_Water = Image.FromFile(context.Server.MapPath(Water_Url));
    //使用畫圖的類,獲取圖片
    Graphics graphics = Graphics.FromImage(image);
    //畫圖方法,第一個參數(shù)就是要添加的水印
    graphics.DrawImage(image_Water,
     //第二個參數(shù)是一個坐標(biāo)的問題,從x1,y1坐標(biāo)開始,繪制的水印的長度和寬度,
     //一共四個參數(shù),x1,y1,水印的長度,寬度
     new Rectangle(image.Width - image_Water.Width, image.Height - image_Water.Height, image_Water.Width, image_Water.Height),
     //從上一個參數(shù)獲取的位置開始作為新的區(qū)域
     //新區(qū)域的0,0開始,也是寬度和長度,
     //最后一個參數(shù)就是,像素的問題,多少像素
     0, 0, image_Water.Width, image_Water.Height,GraphicsUnit.Pixel);
    //使用完了,把兩個圖片的資源都釋放掉
    graphics.Dispose();
    image_Water.Dispose();
   }
   else
   {
    //這里是如果沒有指定的圖片的話,就用一個找不到的圖片去代替
    image = Image.FromFile(context.Server.MapPath(None_Picture));
   }
   //新圖片的類型
   context.Response.ContentType = "Image/Jpeg";
   //把新圖片進(jìn)行保存,輸出流和格式
   image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
   //使用完保存,釋放掉圖片的資源,結(jié)束
   image.Dispose();
   context.Response.End();


  }
 }
}

修改配置文件

<system.webServer>
 <handlers>
  <add verb="*" name="image_Water" path="Images/*.jpg" type="shuiyin.Water"/>
 </handlers>
</system.webServer>

path是加水印圖片的地址,type是那個類的路徑:
也就是命名空間 .(點(diǎn))類名

一個簡單的web窗體

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="ThreePicture_Water.aspx.cs" Inherits="shuiyin.ThreePicture_Water" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
 <title></title>
</head>
<body>
 <form id="form1" runat="server">
  <div>
   <img src="Images/adv1.jpg" />
   <img src="Images/adv2.jpg" />
   <img src="Images/adv3.jpg" />
  </div>
 </form>
</body>
</html>

效果圖

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論

淅川县| 紫云| 文水县| 扎鲁特旗| 大庆市| 武汉市| 榕江县| 漳浦县| 永顺县| 建瓯市| 济南市| 印江| 西乌| 武强县| 安阳县| 邳州市| 六枝特区| 荣成市| 隆安县| 泰兴市| 巍山| 中阳县| 邳州市| 宜都市| 卓尼县| 双峰县| 莆田市| 新宁县| 龙胜| 渝北区| 江永县| 莒南县| 宿州市| 舞阳县| 金山区| 武功县| 英德市| 永泰县| 海门市| 峨山| 万全县|