加水印

本文介绍了一个 ASP.NET 应用程序中如何处理文件上传并为上传的图片添加文字和图片水印的方法。具体包括使用 HttpPostedFile 对象获取文件、检查文件类型、读取文件流以及利用 Graphics 对象进行水印添加等关键步骤。

 /// <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;
            }
        }
 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值