有你在真好 的个人博客
.net为图片制作水印
阅读:2300 添加日期:2021/3/22 16:07:19
"

在自己的网站上传的图片加水印好像成了一种风尚,成为防止图片盗用图片和做宣传网站的一种方式。

现在就教你如何制作这些水印的、

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);

}


"
ICP备案号:苏ICP备14035786号-1 苏公网安备 32050502001014号