bandariiswear

幽艳纷相竞 秾纤各自奇 虽然春烂漫 也要笔扶持 清人题画诗

导航

通过WebHandler给图片添加水印

图片生成webhandler水印,主要把水印的信息和位置post到webhandler去处理,返回后再div上显示

前台传递水印的信息和位置:

  /***********提取DIV属性****************/
        var offsetLeft = 0;
        var offsetTop = 0;
        function exc() {
           offsetLeft= document.getElementById("d_panel").offsetLeft;
           offsetTop = document.getElementById("d_panel").offsetTop;
            var trlist = getElementsByName_iefix("div", "ref","dyq");
            var params = "c=1";
            for (var i = 0;  trlist.length > i; i++) {
                params += trlist[i];
            }
            toPost("Create.ashx", params);
        }
        //获取查找DIV对象
        function getElementsByName_iefix(tag, name,val) {
            var elem = document.getElementsByTagName(tag);
            var arr = new Array();
            for (i = 0,iarr = 0; i < elem.length; i++) {
                att = elem[i].getAttribute(name);
                if (att == val) {
                    arr[iarr] = extraction(elem[i], iarr++);
                }
            }
            return arr;
        }
        //获取DIV属性 传入数据对象
        function extraction(obj, i) {
            var strpage = new StringBuilder(); 
            var color=obj.style.color.toString();
            strpage.append("&color_" + i + "=" + color.colorRgb());
            strpage.append("&fontsize_" + i + "=" + obj.style.fontSize);
            strpage.append("&offsetleft_" + i + "=" + (obj.offsetLeft - offsetLeft-3));//坐标误差 -3 可以删除 可能是边框造成
            strpage.append("&offsettop_" + i + "=" + (obj.offsetTop - offsetTop - 1)); //坐标误差-1 可以删除 可能是边框造成
            strpage.append("&txt_" + i + "=" + obj.innerHTML);
            return strpage.tostring()
        }

        /**********POST************/
        function toPost(url, params) {
   
            createXMLHttpRequest();
            xmlHttp.open("POST", url, true);
            xmlHttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
            xmlHttp.onreadystatechange = function () { ShowResult(); };
            xmlHttp.send(params);

        }
        //回调方法
        function ShowResult() {
            if (xmlHttp.readyState == 1 || xmlHttp.readyState == 2 || xmlHttp.readyState == 3) {
            } else if (xmlHttp.readyState == 4) {
                if (xmlHttp.responseText == "f") {
                    alert("图片生成失败");
                } else {
                document.getElementById("d_out").innerHTML = "<img src='" + xmlHttp.responseText + "' />";
            
            }
            }
        }


        var xmlHttp;
        function createXMLHttpRequest() {
            if (window.ActiveXObject) {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            else if (window.XMLHttpRequest) {
                xmlHttp = new XMLHttpRequest();
            }
        }
View Code

后台webhandler的处理:

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        string _msg = "f";
        NameValueCollection nvc = context.Request.Form;
        System.Drawing.Image originalImage = System.Drawing.Image.FromFile(context.Server.MapPath("4b7a8989jw6df6jumd2z8j.jpg"));
        List<M_Div> _mlist = new List<M_Div>();
        for (int i = 0; nvc["color_" + i] != null; i++)
        {
            M_Div _M_Div = new M_Div();
            _M_Div.Txt = nvc["txt_" + i].ToString().Replace("%20", "");
            _M_Div.X = nvc["offsetleft_" + i].ToString().Length != 0 ? Convert.ToInt32(nvc["offsetleft_" + i].ToString()) : 0;
            _M_Div.Y = nvc["offsettop_" + i].ToString().Length != 0 ? Convert.ToInt32(nvc["offsettop_" + i].ToString()) : 0;
            string Fontsize = nvc["fontsize_" + i].ToString().Replace("px", "");
            _M_Div.Fontsize = Fontsize.Length != 0 ? (Convert.ToInt32(Fontsize)) : 5;
            _M_Div.Color = Utility.StringToRgb(nvc["color_" + i].ToString().Replace("%20", ""));
            _mlist.Add(_M_Div);
        }
        if (_mlist.Count != 0)
        {
            new B_Image().operate(originalImage, _mlist);
            _msg = "images/" + DateTime.Now.Ticks + ".jpg";
            Utility.Save(originalImage, context.Server.MapPath(_msg));
        }
        context.Response.Write(_msg);
    }
View Code

源代码在http://download.csdn.net/detail/yekeyishuo/6424051

posted on 2013-10-19 19:22  YEKEYISHUO  阅读(264)  评论(0编辑  收藏  举报