本文实例为大家分享了ASP.NET实现图片自动添加水印的具体代码,供大家参考,具体内容如下
先建一个类,感觉注释已经很详细了,有不懂的欢迎评论
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实例可以再次使用 false,会安全一些,效率会低一些 IHttpHandler的实例就不能使用 */ 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; //如果我当前项目中有这个图片,就可以进行加水印操作 if (File.Exists(path)) { //获取指定的图片(要添加水印的图片) image = Image.FromFile(path); //再找到,要添加的水印 Image image_Water = Image.FromFile(context.Server.MapPath(Water_Url)); //使用画图的类,获取图片 Graphics graphics = Graphics.FromImage(image); //画图方法,第一个参数就是要添加的水印 graphics.DrawImage(image_Water, //第二个参数是一个坐标的问题,从x1,y1坐标开始,绘制的水印的长度和宽度, //一共四个参数,x1,y1,水印的长度,宽度 new Rectangle(image.Width - image_Water.Width, image.Height - image_Water.Height, image_Water.Width, image_Water.Height), //从上一个参数获取的位置开始作为新的区域 //新区域的0,0开始,也是宽度和长度, //最后一个参数就是,像素的问题,多少像素 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"; //把新图片进行保存,输出流和格式 image.Save(context.Response.OutputStream, ImageFormat.Jpeg); //使用完保存,释放掉图片的资源,结束 image.Dispose(); context.Response.End(); } } }
修改配置文件
<system.webServer> <handlers> <add verb="*" name="image_Water" path="Images/*.jpg" type="shuiyin.Water"/> </handlers> </system.webServer>
path是加水印图片的地址,type是那个类的路径:
也就是命名空间 .(点)类名
一个简单的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="/UploadFiles/2021-04-02/adv1.jpg">效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
华山资源网 Design By www.eoogi.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
免责声明:本站资源来自互联网收集,仅供用于学习和交流,请遵循相关法律法规,本站一切资源不代表本站立场,如有侵权、后门、不妥请联系本站删除!
华山资源网 Design By www.eoogi.com
暂无评论...
稳了!魔兽国服回归的3条重磅消息!官宣时间再确认!
昨天有一位朋友在大神群里分享,自己亚服账号被封号之后居然弹出了国服的封号信息对话框。
这里面让他访问的是一个国服的战网网址,com.cn和后面的zh都非常明白地表明这就是国服战网。
而他在复制这个网址并且进行登录之后,确实是网易的网址,也就是我们熟悉的停服之后国服发布的暴雪游戏产品运营到期开放退款的说明。这是一件比较奇怪的事情,因为以前都没有出现这样的情况,现在突然提示跳转到国服战网的网址,是不是说明了简体中文客户端已经开始进行更新了呢?
更新日志
2024年10月01日
2024年10月01日
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】
- 李恕权.2003-回·20年全精选2CD【SONY】【WAV+CUE】