net为图片制作水印

在自己的网站上传的图片加水印好像成了一种风尚,成为防止图片盗用图片和做宣传网站的一种方式。
现在就教你如何制作这些水印的、

        private void Button1_Click(object sender, System.EventArgs e)
        {
            string filename=this.file_up.PostedFile.FileName;
            string path;   

           //本水印制作不改变上传图片的文件名
            //代码编写:阿光

           //取得文件的名称
            filename=filename.Substring(filename.LastIndexOf("//")+1);

            //对jpg图片加水印
            if(getExtName(filename)=="jpg")
            {
                if(this.rblTypes.SelectedValue=="1")
                    addText(filename);
                else
                    addImage(filename);

            }
            else
            {
                //直接上传图片
                path=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename;
                //保存
                this.file_up.PostedFile.SaveAs(path);
            }

            this.Image1.ImageUrl=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename;

        }
两个方法:


        //取得文件名(不包括扩展名)
        private string getFileName(string filename)
        {
            return filename.Remove(filename.LastIndexOf("."),4);
        }

        //取得文件的扩展名
        private string getExtName(string filename)
        {
            return filename.Substring(filename.LastIndexOf(".")+1);
        }

加文字水印:

        private void addText(string filename)
        {
            string str;
            string path;
            string filename1,extname;

            //取得文件名(不包括扩展名)
            filename1=getFileName(filename);
            //取得文件的扩展名
            extname=getExtName(filename);

            //取得上传后的文件路径
            path=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename1+"_temp."+extname;
            //上传图片
            this.file_up.PostedFile.SaveAs(path);

            System.Drawing.Image image=System.Drawing.Image.FromFile(path);

            System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(image);

            //将图片绘制到graphics中
            g.DrawImage(image,0,0,image.Width,image.Height);
            //设置文字的属性
            System.Drawing.Font f=new Font("Verdana",10);
            //判断图片的大小,如果图片过小,不写文字
            if(image.Width>=250)
                //在此设定在图片上所加的文字
                str="jzg7366";
            else
                str="";
            int x,y;
            //写的文字的起始位置,x,y坐标
            x=image.Width-(int)(str.Length*15);
            y=image.Height-20;
            //设置字体的颜色
            System.Drawing.Brush b=new SolidBrush(Color.White);
            //写字
            g.DrawString(str,f,b,x,y);
            //释放graphics
            g.Dispose();

            //确定新图片的文件路径
            string newpath=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename;
            //保存写上字的图片
            image.Save(newpath);
            //释放image
            image.Dispose();
            //删除没加水印的图片,记得一定要放在image释放之后,否则无法删除
            System.IO.File.Delete(path);
        }加图片水印:
        private void addImage(string filename)
        {
            string path;
            string logo_path;
            string filename1,extname;

            //取得文件名(不包括扩展名)
            filename1=getFileName(filename);
            //取得文件的扩展名
            extname=getExtName(filename);

            //取得上传后的文件路径
            path=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename1+"_temp."+extname;
            //上传图片
            this.file_up.PostedFile.SaveAs(path);

            //上传文件的临时位置
            path=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename1+"_temp."+extname;
            //图标文件的位置
            logo_path=HttpContext.Current.Request.PhysicalApplicationPath+"images//logo.gif";
           
            System.Drawing.Image image=System.Drawing.Image.FromFile(path);
           
            System.Drawing.Image copyImage=System.Drawing.Image.FromFile(logo_path);
           
            System.Drawing.Graphics g=System.Drawing.Graphics.FromImage(image);
           
            //将水印打印到上传图片上去
            g.DrawImage(copyImage,new Rectangle(image.Width-copyImage.Width-5,image.Height-copyImage.Height-5,copyImage.Width,copyImage.Height),0,0,copyImage.Width,copyImage.Height,System.Drawing.GraphicsUnit.Pixel);
           
            g.Dispose();

            //确定新图片的文件路径
            string newpath=HttpContext.Current.Request.PhysicalApplicationPath+"images//"+filename;
            //保存写上字的图片
            image.Save(newpath);
            //释放image
            image.Dispose();
            //删除没加水印的图片,记得一定要放在image释放之后,否则无法删除
            System.IO.File.Delete(path);
        }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值