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

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

        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="小妞表跑  制作";
            
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);
        }
原文地址:https://www.cnblogs.com/songafeng/p/124026.html