首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

施用HttpModule实现全部图片加水印

2013-03-17 
使用HttpModule实现全部图片加水印情况是这样,一个web程序,有asp和asp.net的程序,能否用HttpModule实现:只

使用HttpModule实现全部图片加水印
情况是这样,一个web程序,有asp和asp.net的程序,能否用HttpModule实现:只要是asp、asp.net程序上传的图片都会加上水印?
我目前仅能实现在asp.net页面的编辑器里面批量上传的图片批量加水印,不知道这种混合程序的该怎么实现? HttpModule 批量水印
[解决办法]
1.开发两套上传
2.实施监测吧.写个service 。 后台运行。监测到有图片加上水印。或者在asp ,asp.net上传时通知这个service
[解决办法]
/res.axd?icon=BuildingKey


所有图片路径都以这样的形式访问
[解决办法]
以上是FineUi的方法
 Web.config  
  <httpHandlers>
      <add verb="GET" path="res.axd" type="FineUI.ResourceHandler, FineUI" validate="false"/>
    </httpHandlers>


using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Reflection;
using System.IO;
using System.Drawing.Imaging;

namespace FineUI
{
    /// <summary>
    /// 资源处理程序
    /// </summary>
    public class ResourceHandler : IHttpHandler
    {
        /// <summary>
        /// 处理资源的请求
        /// </summary>
        /// <param name="context">Http请求上下文</param>
        public void ProcessRequest(HttpContext context)
        {
            string type = String.Empty, 
                typeValue = String.Empty,
                resName = "FineUI.";
                

            if (!String.IsNullOrEmpty(typeValue = context.Request.QueryString["icon"]))
            {
                type = "icon";
            }
            else if (!String.IsNullOrEmpty(typeValue = context.Request.QueryString["js"]))
            {
                type = "js";
                resName += "js." + typeValue;
            }
            else if (!String.IsNullOrEmpty(typeValue = context.Request.QueryString["lang"]))
            {
                type = "lang";


                resName += "js.lang." + typeValue;
            }
            else if (!String.IsNullOrEmpty(typeValue = context.Request.QueryString["theme"]))
            {
                type = "theme";
                resName += "res.theme." + typeValue;
            }
            else if (!String.IsNullOrEmpty(typeValue = context.Request.QueryString["css"]))
            {
                type = "css";
                resName += "res.css." + typeValue;
            }
            else if (!String.IsNullOrEmpty(typeValue = context.Request.QueryString["img"]))
            {
                type = "img";
                resName += "res.img." + typeValue;
            }

            byte[] binary;
            switch (type)
            {
                case "icon":
                    if (!typeValue.EndsWith(".png") && !typeValue.EndsWith(".gif"))
                    {
                        typeValue = IconHelper.GetName((Icon)Enum.Parse(typeof(Icon), typeValue));
                    }
                    //resName += "res.icon." + typeValue;
                    string serverPath = String.Format("{0}/{1}", GlobalConfig.GetIconBasePath(), typeValue);
                    context.Response.WriteFile(context.Server.MapPath(serverPath));

                    context.Response.ContentType = "image/" + GetImageFormat(typeValue);
                    break;


                case "js":
                case "lang":
                    context.Response.Write(ResourceHelper.GetResourceContent(resName));
                    context.Response.ContentType = "text/javascript";
                    break;
                case "css":
                    context.Response.Write(ResourceHelper.GetResourceContent(resName));
                    context.Response.ContentType = "text/css";
                    break;
                case "theme":
                case "img":
                    binary = ResourceHelper.GetResourceContentAsBinary(resName);
                    context.Response.OutputStream.Write(binary, 0, binary.Length);
                    //context.Response.Write(ResourceHelper.GetResourceContent(resName));
                    context.Response.ContentType = "image/" + GetImageFormat(resName);
                    break;
            }

            
            // 缓存一年,只能通过改变 URL 来强制更新缓存
            context.Response.Cache.SetExpires(DateTime.Now.AddYears(1));
            context.Response.Cache.SetCacheability(HttpCacheability.Public);
        }

        //private void RenderImage(HttpContext context, string resName)
        //{
        //    Assembly assembly = Assembly.GetExecutingAssembly();
        //    using (Stream stream = assembly.GetManifestResourceStream(resName))
        //    {
        //        using (System.Drawing.Image image = System.Drawing.Image.FromStream(stream))
        //        {
        //            // PNG输出时出现“GDI+ 中发生一般性错误”


        //            using (MemoryStream ms = new MemoryStream())
        //            {
        //                image.Save(ms, image.RawFormat);
        //                ms.WriteTo(context.Response.OutputStream);
        //                context.Response.ContentType = "image/" + GetImageFormat(image.RawFormat);
        //            }
        //        }
        //    }
        //}

        private string GetImageFormat(string imageName)
        {
            int lastDotIndex = imageName.LastIndexOf(".");
            if (lastDotIndex >= 0)
            {
                return imageName.Substring(lastDotIndex + 1);
            }
            return "png";
        }

        private string GetImageFormat(ImageFormat format)
        {
            if (format == ImageFormat.Bmp)
            {
                return "bmp";
            }
            else if (format == ImageFormat.Gif)
            {
                return "gif";
            }
            else if (format == ImageFormat.Jpeg)
            {
                return "jpeg";
            }
            else if (format == ImageFormat.Png)
            {
                return "png";
            }
            else if (format == ImageFormat.Tiff)


            {
                return "tiff";
            }
            else if (format == ImageFormat.Icon)
            {
                return "icon";
            }
            return "gif";
        }


        /// <summary>
        /// 只要请求的 URL 相同,则请求可以重用
        /// </summary>
        public bool IsReusable
        {
            get
            {
                return true;
            }
        }
    }
}


[解决办法]
用handler吧

热点排行