加水印

来源:互联网 发布:医院网络部提成方案 编辑:程序博客网 时间:2024/04/27 15:16

 /// <summary>
    /// _02UpLoad 的摘要说明
    /// </summary>
    public class _02UpLoad : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/html";
          //HttpPostedFile file=context.Request.Files[0];
          //if (file.ContentLength > 0)
          //{
          //    string strName = System.IO.Path.GetFileName(file.FileName);
          //    file.SaveAs(context.Server.MapPath("/UpLoad/"+strName));
             

          //}

           
            //======================================加文字水印=====================================================

            StringBuilder sbMsg = new StringBuilder();

            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                HttpPostedFile file = context.Request.Files[i];
                if (file.ContentType.Contains("image/"))
                {
                    if (file.ContentLength>0)
                    {
                    using (Image img=Image.FromStream(file.InputStream))
                    {
                        using ( Graphics g=Graphics.FromImage(img))
                        {
                            g.DrawString("传奇",new Font("微软雅黑",16),Brushes.Red,0,0);
                        }
                        string strName = System.IO.Path.GetFileName(file.FileName);
                        string PhyPath = context.Server.MapPath("/UpLoad/" + strName);
                        img.Save(PhyPath);
                        sbMsg.AppendLine(strName+"<br/>");
                    }
                    }
                }
            }
            context.Response.Write("上传成功!<br/>" + sbMsg.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }

============================================加图片水印======================================================

StringBuilder sbMsg = new StringBuilder();

            for (int i = 0; i < context.Request.Files.Count; i++)
            {
                HttpPostedFile file = context.Request.Files[i];
                if (file.ContentType.Contains("image/"))
                {
                    if (file.ContentLength>0)
                    {
                    using (Image img=Image.FromStream(file.InputStream))
                    {
                        using (Image imgWater = Image.FromFile(context.Server.MapPath("/UpLoad/adidas.jpg")))
                        {
                            using (Graphics g = Graphics.FromImage(img))
                            {
                                                            g.DrawImage(imgWater,0,0);
                            }
                        }
                      
                        string strName = System.IO.Path.GetFileName(file.FileName);
                        string PhyPath = context.Server.MapPath("/UpLoad/" + strName);
                        img.Save(PhyPath);
                        sbMsg.AppendLine(strName+"<br/>");
                    }
                    }
                }
            }
            context.Response.Write("上传成功!<br/>" + sbMsg.ToString());
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
 

0 0
原创粉丝点击