教你用.net为图片制作水印

为在自己的网站上传的图片加水印好像成了一种风尚,成为防止图片盗用图片和做宣传网站的一种方式。
现在就教你如何制作这些水印的、
这里有两种为加水印的形式,一种是加文字水印,一种是加图片水印。
文字水印如这个图(右下角的那行字):
200312190482_358078.jpg
图片水印样式(右下角的那个图表)
303574.jpg

None.gif          private   void  Button1_Click( object  sender, System.EventArgs e)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string filename=this.file_up.PostedFile.FileName;
InBlock.gif            
string path;
InBlock.gif            
InBlock.gif
InBlock.gif            
//本水印制作不改变上传图片的文件名
InBlock.gif            
//代码编写:阿风(小妞表跑)
InBlock.gif    
InBlock.gif
InBlock.gif            
//取得文件的名称
InBlock.gif
            filename=filename.Substring(filename.LastIndexOf("\\")+1);
InBlock.gif
InBlock.gif            
//对jpg图片加水印
InBlock.gif
            if(getExtName(filename)=="jpg")
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if(this.rblTypes.SelectedValue=="1")
InBlock.gif                    addText(filename);
InBlock.gif                
else
InBlock.gif                    addImage(filename);
InBlock.gif
ExpandedSubBlockEnd.gif            }

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

InBlock.gif
InBlock.gif            
this.Image1.ImageUrl=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename;
InBlock.gif
ExpandedBlockEnd.gif        }

两个方法:

None.gif          // 取得文件名(不包括扩展名)
None.gif
         private   string  getFileName( string  filename)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
return filename.Remove(filename.LastIndexOf("."),4);
ExpandedBlockEnd.gif        }

None.gif
None.gif        
// 取得文件的扩展名
None.gif
         private   string  getExtName( string  filename)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
return filename.Substring(filename.LastIndexOf(".")+1);
ExpandedBlockEnd.gif        }


加文字水印:

None.gif          private   void  addText( string  filename)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string str;
InBlock.gif            
string path;
InBlock.gif            
string filename1,extname;
InBlock.gif
InBlock.gif            
//取得文件名(不包括扩展名)
InBlock.gif
            filename1=getFileName(filename);
InBlock.gif            
//取得文件的扩展名
InBlock.gif
            extname=getExtName(filename);
InBlock.gif
InBlock.gif            
//取得上传后的文件路径
InBlock.gif
            path=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename1+"_temp."+extname;
InBlock.gif            
//上传图片
InBlock.gif
            this.file_up.PostedFile.SaveAs(path);
InBlock.gif
InBlock.gif            System.Drawing.Image image
=System.Drawing.Image.FromFile(path);
InBlock.gif
InBlock.gif            System.Drawing.Graphics g
=System.Drawing.Graphics.FromImage(image);
InBlock.gif
InBlock.gif            
//将图片绘制到graphics中
InBlock.gif
            g.DrawImage(image,0,0,image.Width,image.Height);
InBlock.gif            
//设置文字的属性
InBlock.gif
            System.Drawing.Font f=new Font("Verdana",10);
InBlock.gif            
//判断图片的大小,如果图片过小,不写文字
InBlock.gif
            if(image.Width>=250)
InBlock.gif                
//在此设定在图片上所加的文字
InBlock.gif
                str="小妞表跑  制作";
InBlock.gif            
else
InBlock.gif                str
="";
InBlock.gif            
int x,y;
InBlock.gif            
//写的文字的起始位置,x,y坐标
InBlock.gif
            x=image.Width-(int)(str.Length*15);
InBlock.gif            y
=image.Height-20;
InBlock.gif            
//设置字体的颜色
InBlock.gif
            System.Drawing.Brush b=new SolidBrush(Color.White);
InBlock.gif            
//写字
InBlock.gif
            g.DrawString(str,f,b,x,y);
InBlock.gif            
//释放graphics
InBlock.gif
            g.Dispose();
InBlock.gif
InBlock.gif            
//确定新图片的文件路径
InBlock.gif
            string newpath=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename;
InBlock.gif            
//保存写上字的图片
InBlock.gif
            image.Save(newpath);
InBlock.gif            
//释放image
InBlock.gif
            image.Dispose();
InBlock.gif            
//删除没加水印的图片,记得一定要放在image释放之后,否则无法删除
InBlock.gif
            System.IO.File.Delete(path);
ExpandedBlockEnd.gif        }
加图片水印:
None.gif          private   void  addImage( string  filename)
ExpandedBlockStart.gifContractedBlock.gif        
dot.gif {
InBlock.gif            
string path;
InBlock.gif            
string logo_path;
InBlock.gif            
string filename1,extname;
InBlock.gif
InBlock.gif            
//取得文件名(不包括扩展名)
InBlock.gif
            filename1=getFileName(filename);
InBlock.gif            
//取得文件的扩展名
InBlock.gif
            extname=getExtName(filename);
InBlock.gif
InBlock.gif            
//取得上传后的文件路径
InBlock.gif
            path=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename1+"_temp."+extname;
InBlock.gif            
//上传图片
InBlock.gif
            this.file_up.PostedFile.SaveAs(path);
InBlock.gif
InBlock.gif            
//上传文件的临时位置
InBlock.gif
            path=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename1+"_temp."+extname;
InBlock.gif            
//图标文件的位置
InBlock.gif
            logo_path=HttpContext.Current.Request.PhysicalApplicationPath+"images\\logo.gif";
InBlock.gif            
InBlock.gif            System.Drawing.Image image
=System.Drawing.Image.FromFile(path);
InBlock.gif            
InBlock.gif            System.Drawing.Image copyImage
=System.Drawing.Image.FromFile(logo_path);
InBlock.gif            
InBlock.gif            System.Drawing.Graphics g
=System.Drawing.Graphics.FromImage(image);
InBlock.gif            
InBlock.gif            
//将水印打印到上传图片上去
InBlock.gif
            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);
InBlock.gif            
InBlock.gif            g.Dispose();
InBlock.gif
InBlock.gif            
//确定新图片的文件路径
InBlock.gif
            string newpath=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename;
InBlock.gif            
//保存写上字的图片
InBlock.gif
            image.Save(newpath);
InBlock.gif            
//释放image
InBlock.gif
            image.Dispose();
InBlock.gif            
//删除没加水印的图片,记得一定要放在image释放之后,否则无法删除
InBlock.gif
            System.IO.File.Delete(path);
ExpandedBlockEnd.gif        }

转载于:https://www.cnblogs.com/songafeng/archive/2005/03/23/124026.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值