- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章ASP.NET 图片加水印防盗链实现代码由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
首先建一个类: 。
复制代码代码如下
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Drawing; /// <summary> ///Class1 的摘要说明 /// </summary> public class Class1:IHttpHandler //调用接口 { public Class1() { // //TODO: 在此处添加构造函数逻辑 // } public bool IsReusable { get { return true; } } public void ProcessRequest(HttpContext context) { HttpRequest req = context.Request; if (req.UrlReferrer != null && req.UrlReferrer.Host.Length > 0) //反盗链代码判断 { System.Drawing.Image img = System.Drawing.Image.FromFile(context.Request.PhysicalPath); System.Drawing.Graphics g = Graphics.FromImage(img); g.DrawString("三国演义", new Font("宋体", 20, FontStyle.Bold), Brushes.White, 10, 10); img.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); context.Response.Flush(); context.Response.End(); } else { context.Response.Write("您不能盗链本站图片"); } } } 。
在web.config中注册接口: 。
复制代码代码如下
<httpHandlers> <add verb="*" path="images/*.jpg" type="Class1,App_Code"/> </httpHandlers> 。
url:http://greatverve.cnblogs.com/archive/2011/12/20/asp-net-hotlinking.html 参考: 1.修改web.config 。
复制代码代码如下
<system.web> <httpHandlers> <remove verb="*" path="*.asmx"/> <!--解决图片防盗链问题--> <add verb="*" path="*.jpg" type="MyHttpHandler.Watermark"/> <add verb="*" path="*.gif" type="MyHttpHandler.Watermark"/> <add verb="*" path="*.png" type="MyHttpHandler.Watermark"/> </httpHandlers> </system.web> 。
2.添加一个一般执行文件Watermark.ashx,代码如下: 。
复制代码代码如下
using System; using System.Data; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; using System.Drawing.Imaging; using System.Drawing; using System.IO; namespace MyHttpHandler { /// <summary> /// Summary description for $codebehindclassname$ /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class Watermark : IHttpHandler { public bool IsReusable { get { return false; } } public void ProcessRequest(HttpContext context) { //设置客户端缓冲时间过期时间为0,即立即过期 //context.Response.Expires = 0; //清空服务器端为此会话开启的输出缓存 //context.Response.Clear(); //设置输出文件类型 context.Response.ContentType = "image/jpg"; //将请求文件写入到输出缓存中 #region 获取XML配置信息 DataSet dsConfing = new DataSet(); string watermarkConfigPath = context.Server.MapPath("~/Config/WaterMarkConfig.xml"); if (System.IO.File.Exists(watermarkConfigPath)) dsConfing.ReadXml(watermarkConfigPath); else { //添加默认的水印配置 } DataRow drConfing = dsConfing.Tables[0].Rows[0]; #endregion string currentHost = drConfing["allowhost"].ToString(); //判断是否是本地网站引用图片,如果是则返回正确的图片 if (context.Request.Url.Authority.Equals(currentHost, StringComparison.InvariantCultureIgnoreCase)) { string localPath = context.Request.Url.LocalPath; localPath = localPath.Remove(localPath.LastIndexOf('/')).ToLower();// "/images/userphoto" if (drConfing["isflag"].Equals("true") && drConfing["files"].ToString().ToLower().IndexOf(localPath) > 0) { #region 水印代码 string sImgStartPhysicalPath = context.Request.PhysicalPath; System.Drawing.Image imgStart = System.Drawing.Image.FromFile(sImgStartPhysicalPath); //备份原图片 //int indexOf = sImgStartPhysicalPath.LastIndexOf("."); //string bakPath = sImgStartPhysicalPath.Remove(indexOf) + "_bak" + sImgStartPhysicalPath.Substring(indexOf); //imgStart.Save(bakPath); Graphics gh = System.Drawing.Graphics.FromImage(imgStart); if (drConfing["type"].Equals("img")) { System.Drawing.Image imgWatermark = System.Drawing.Image.FromFile(context.Server.MapPath(drConfing["img-path"].ToString())); Rectangle rg = SetImgPosition(drConfing["position"].ToString(), imgStart.Width, imgStart.Height, imgWatermark.Width, imgWatermark.Height); gh.DrawImage(imgWatermark, rg, 0, 0, imgWatermark.Width, imgWatermark.Height, GraphicsUnit.Pixel); gh.Save(); gh.Dispose(); imgWatermark.Dispose(); } else if (drConfing["type"].Equals("font")) { //文字水印 string content = drConfing["font-content"].ToString(); float Size = (float)Convert.ToDouble(drConfing["font-size"].ToString()); FontStyle fontStyle = (FontStyle)int.Parse(drConfing["font-style"].ToString()); System.Drawing.Font f = new System.Drawing.Font("Arial", Size, fontStyle); Color G_Color = Color.FromName(drConfing["font-color"].ToString()); System.Drawing.Brush b = new System.Drawing.SolidBrush(G_Color); SizeF sizeF = gh.MeasureString(content, f); gh.DrawString(content, f, b, SetFontPosition(drConfing["position"].ToString(), imgStart.Width, imgStart.Height, (int)sizeF.Width, (int)sizeF.Height)); gh.Save(); gh.Dispose(); } //将请求文件写入到输出缓存中 imgStart.Save(context.Response.OutputStream, ImageFormat.Jpeg); imgStart.Dispose(); #endregion } else { #region 输出原图 //将请求文件写入到输出缓存中 context.Response.WriteFile(context.Request.Url.AbsolutePath); #endregion } } //如果不是本地引用,则是盗链本站图片 else { //将请求文件写入到输出缓存中 context.Response.WriteFile(context.Request.PhysicalApplicationPath + drConfing["errimgpath"].ToString()); } //将输出缓存中的信息传送到客户端 context.Response.End(); } /// <summary> /// 图片绘画水印的位置 /// </summary> /// <param name="positionConfig">位置类型</param> /// <param name="width">原图片宽</param> /// <param name="height"></param> /// <param name="watermarkWidth">水印图宽</param> /// <param name="watermarkHeight"></param> /// <returns></returns> private Rectangle SetImgPosition(string positionConfig,int width,int height,int watermarkWidth,int watermarkHeight) { int xpos = 0; int ypos = 0; int margin = 10; int width_margin = width - margin; int height_margin = height - margin; double proportion = 1d;//水印图片缩放比例 //int if ((width_margin > watermarkWidth * proportion) && (height_margin > watermarkHeight * proportion)) { } else if ((width_margin > watermarkWidth * proportion) && (height_margin < watermarkHeight * proportion)) { proportion = Convert.ToDouble( height_margin) / Convert.ToDouble( watermarkHeight); } else if ((width_margin < watermarkWidth * proportion) && (height_margin > watermarkHeight * proportion)) { proportion = Convert.ToDouble(width_margin) / Convert.ToDouble(watermarkWidth); } else { double proportionW = Convert.ToDouble(width_margin) / Convert.ToDouble(watermarkWidth); double proportionH = Convert.ToDouble(height_margin) / Convert.ToDouble(watermarkHeight); proportion = proportionW >= proportionH ? proportionH : proportionW; } watermarkWidth = Convert.ToInt32(watermarkWidth * proportion); watermarkHeight = Convert.ToInt32(watermarkHeight * proportion); switch (positionConfig) { case "top-left": xpos = margin; ypos = margin; break; case "top-right": xpos = width_margin - watermarkWidth; ypos = margin; break; case "bottom-left": xpos = margin; ypos = height_margin - watermarkHeight; break; case "bottom-right": xpos = width_margin - watermarkWidth ; ypos = height_margin - watermarkHeight ; break; default: xpos = width_margin - watermarkWidth ; ypos = height_margin - watermarkHeight; break; } return new Rectangle(xpos,ypos,watermarkWidth,watermarkHeight); } /// <summary> /// 图片绘画文字位置 /// </summary> /// <param name="positionConfig">位置类型</param> /// <param name="width">原图片宽</param> /// <param name="height"></param> /// <param name="fontWidth">文字长度</param> /// <param name="fontHeight"></param> /// <returns></returns> private Point SetFontPosition(string positionConfig, int width, int height, int fontWidth, int fontHeight) { int xpos = 0; int ypos = 0; int margin = 10; int width_margin = width - margin; int height_margin = height - margin; double proportion = 1d;//水印图片缩放比例 //int if ((width_margin > fontWidth * proportion) && (height_margin > fontHeight * proportion)) { } else if ((width_margin > fontWidth * proportion) && (height_margin < fontHeight * proportion)) { proportion = Convert.ToDouble(height_margin) / Convert.ToDouble(fontHeight); } else if ((width_margin < fontWidth * proportion) && (height_margin > fontHeight * proportion)) { proportion = Convert.ToDouble(width_margin) / Convert.ToDouble(fontWidth); } else { double proportionH = Convert.ToDouble(height_margin) / Convert.ToDouble(fontHeight); double proportionW = Convert.ToDouble(width_margin) / Convert.ToDouble(fontWidth); proportion = proportionW >= proportionH ? proportionH : proportionW; } fontWidth = Convert.ToInt32(fontWidth * proportion); fontHeight = Convert.ToInt32(fontHeight * proportion); switch (positionConfig) { case "top-left": xpos = margin; ypos = margin; break; case "top-right": xpos = width_margin - fontWidth; ypos = margin; break; case "bottom-left": xpos = margin; ypos = height_margin - fontHeight; break; case "bottom-right": xpos = width_margin - fontWidth; ypos = height_margin - fontHeight; break; default: xpos = width_margin - fontWidth; ypos = height_margin - fontHeight; break; } return new Point(xpos, ypos); } } } 。
3.配置文件的WaterMarkConfig.xml,内容如下: 。
复制代码代码如下
<?xml version="1.0" encoding="utf-8" ?> <watermark> <allowhost>localhost:6219</allowhost><!--允许访问的域名--> <isflag>true</isflag><!-- true、false--> <type>font</type><!-- img、font--> <files>/config|/upfiles/ab</files><!--需要加水印的文件夹--> <position>bottom-right</position><!-- top-left、top-right、bottom-left、bottom-right--> <img-path>~/UpFiles/Watermark.png</img-path><!-- 水印位置 --> <font-style>1</font-style><!--普通文本 0, 加粗文本 1, 倾斜文本 2, 带下划线的文本 4, 中间有直线通过的文本 8--> <font-size>60</font-size> <font-color>red</font-color> <font-content>¥:8000元</font-content> <errimgpath>images/error.jpg</errimgpath><!-- 盗图片的请求返回的跟目录下的某图片 --> </watermark> 。
最后此篇关于ASP.NET 图片加水印防盗链实现代码的文章就讲到这里了,如果你想了解更多关于ASP.NET 图片加水印防盗链实现代码的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
我正在尝试使用水印并使用复杂过滤器应用 Yadif,但我无法弄清楚如何使用以下语法应用 Yadif ffmpeg -i "source:" -i C:\logo.png -c:v libx264 -p
我正在使用 zubrags PHP 水印脚本(附在下面),它工作得很好,除非我尝试使用 PNG-24 作为我的水印。生成的图像有一个乱码、不透明的水印。我想知道是否有人可以帮助解释我需要在下面的脚本中
基本上,我想拍摄用户从照片库中选择的图像,然后应用水印,即右下角的一个三角形,上面有应用程序名称。我已经在 Photoshop 中使用透明层制作了第二张图像。 我尝试了一个函数,我不记得它的确切名称,
Closed. This question needs to be more focused。它当前不接受答案。 想改善这个问题吗?更新问题,使其仅关注editing this post的一个问题。
这个问题我已经发了很多次了,但还是找不到正确的答案。我的目标是加载 PDF(扫描的调查问卷页),用页码标记每个页面,并将每个页面保存在单独的 JPEG 文件中以供以后使用。除了未绘制 NSString
你好,我的代码有问题 $obj = new stdClass(); $obj->cat_id = !empty($_POST['cat_id']) ? $_POST['cat_id']
SO 上有很多类似的问题/答案,但似乎没有一个能解决我的问题。 我的目标是使用 Paperclip 为图像生成“动态水印”(用户头像覆盖在另一张图像上)。我遇到的问题是我无法获得模型的“user_id
我想在我的图片上添加水印,这是我用来截图的代码。有人可以教我如何在图像中添加水印吗?我想在图片的右上角有一个小 Logo 。 我正在尝试研究是否可以实现我在 Canvas 中保留的内容,以便在截取屏幕
我有以下命令: ffmpeg -ss 00:00:30 -i "$i" -i ../audio.mov -map 0:0 -map 1:0 -to 30 -vf "fade=in:0:24, fade
我正在尝试从一些图片以及现有的 mp3(复制)制作幻灯片。图片尺寸不同,但我希望视频输出为 16:9 纵横比和 3840x2160。我也想要水印。重要的是不要拉伸(stretch)图片。 我试过这个代
我已经可以给任何 PDF 加水印,里面的图像,一切正常,但现在我只需要在打印 PDF 时才显示水印......这可能吗?如何? 我当然需要以编程方式执行此操作。 最佳答案 对于 future 的读者,
有没有办法在整个网页上创建浅色透明水印?一个留在屏幕上,即使它滚动?我的想法是创建一个 .PNG 位图并使用带有样式表的 DIV 标签,该样式表将我的 PNG 设置为背景图像,并设置绝对位置。问题是,
是否可以屏蔽应用程序的屏幕截图(电源 + 菜单按钮)?如果没有,此屏幕截图是否有可能收到水印? 问候,克劳迪奥 最佳答案 创建屏幕截图是一种系统行为,您不能覆盖它。 重复 Notification o
所以我一直在寻找如何为图像添加带有 colorBox 的水印,我在谷歌的第一个结果中找到了一个较旧的 colorBox 组,下一个答案是: Jack Moore 10/3/09 Ok, this sh
我有以下 CSS, #duplicateCopy { -webkit-transform:rotate(-20deg); -moz-transform:rotate(-20deg);
我有一个 pdf在它的背景上有水印。当开始扫描以在背景中突出显示带有水印或注释的任何单词时,它会被选中,因为它首先在触摸区域中找到。 我正在使用 CGPDFScanner 扫描文本。 我的问题是如何检
我正在寻找一种在选定字段上放置水印的方法。 那是行不通的-> [select* c_type class:ic watermark "choose type" "a" "b" "c"] 为了放置验证失
我正在尝试向视频添加各种 Gifs/水印,但我无法让它正常工作。 我们假设视频时长为 60 秒,我正在添加一张 Gif 图片。输出看起来正确,声音打开,gif 动画,视频没有停止。这是代码:
我正在尝试使用 FFmpeg 以编程方式将图像或视频叠加在另一个视频的顶部。似乎 AVFilter 可以做到这一点。 有很多关于如何使用命令行执行此操作或类似操作的示例,但是,除了 doc/examp
我正在尝试实现类似 StackOverflow 的水印功能。 我正在使用 jquery-watermark为了这。我遇到的问题是水印文本随着输入元素获得焦点而消失,这在 SO 中不会发生(我也不希望在
我是一名优秀的程序员,十分优秀!