发布于 2016-06-10 06:45:00 | 111 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


这篇文章介绍了asp.net中水印的具体实现代码,有需要的朋友可以参考一下

水印是为了防止别盗用我们的图片.

两种方式实现水印效果

1)可以在用户上传时添加水印.

a)   好处:与2种方法相比,用户每次读取此图片时,服务器直接发送给客户就行了.

b)   缺点:破坏了原始图片.

2)通过全局的一般处理程序,当用户请求这张图片时,加水印.

a)   好处:原始图片没有被破坏

b)   缺点:用户每次请求时都需要对请求的图片进行加水印处理,浪费的服务器的资源.

代码实现第二种方式:


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

namespace BookShop.Web   
{   
    public class WaterMark : IHttpHandler   
    {   

        private const string WATERMARK_URL = "~/Images/watermark.jpg";        //水印图片   
        private const string DEFAULTIMAGE_URL = "~/Images/default.jpg";<span style="white-space:pre">   </span>      //默认图片   
        #region IHttpHandler 成员   

        public bool IsReusable   
        {   
            get { return false; }   
        }   

        public void ProcessRequest(HttpContext context)   
        {   

            //context.Request.PhysicalPath  //获得用户请求的文件物理路径   

            System.Drawing.Image Cover;   
            //判断请求的物理路径中,是否存在文件   
            if (File.Exists(context.Request.PhysicalPath))   
            {   
                //加载文件   
                Cover = Image.FromFile(context.Request.PhysicalPath);   
                //加载水印图片   
                Image watermark = Image.FromFile(context.Request.MapPath(WATERMARK_URL));   
            //通过书的封面得到绘图对像   
                Graphics g = Graphics.FromImage(Cover);   
                //在image上绘制水印   
                g.DrawImage(watermark, new Rectangle(Cover.Width - watermark.Width, Cover.Height - watermark.Height,    
[csharp] view plaincopy 
watermark.Width, watermark.Height), 0, 0, watermark.Width, watermark.Height, GraphicsUnit.Pixel);   
                //释放画布   
                g.Dispose();   
                //释放水印图片   
                watermark.Dispose();   
            }   
            else  
            {   
                //加载默认图片   
                Cover = Image.FromFile(context.Request.MapPath(DEFAULTIMAGE_URL));   
            }   
            //设置输出格式   
            context.Response.ContentType = "image/jpeg";   
            //将图片存入输出流   
            Cover.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);   
            Cover.Dispose();   
            context.Response.End();   
        }   

        #endregion   
    }   
}



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务