日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 综合教程 >内容正文

综合教程

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

發(fā)布時間:2023/12/19 综合教程 26 生活家
生活随笔 收集整理的這篇文章主要介紹了 教你用.net为图片制作水印 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

為在自己的網站上傳的圖片加水印好像成了一種風尚,成為防止圖片盜用圖片和做宣傳網站的一種方式。
現在就教你如何制作這些水印的、
這里有兩種為加水印的形式,一種是加文字水印,一種是加圖片水印。
文字水印如這個圖(右下角的那行字):

圖片水印樣式(右下角的那個圖表)

privatevoidButton1_Click(objectsender,System.EventArgse)
{
stringfilename=this.file_up.PostedFile.FileName;
stringpath;


//本水印制作不改變上傳圖片的文件名
//代碼編寫:阿風(小妞表跑)


//取得文件的名稱
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;

}

兩個方法:

//取得文件名(不包括擴展名)
privatestringgetFileName(stringfilename)
{
returnfilename.Remove(filename.LastIndexOf("."),4);
}

//取得文件的擴展名
privatestringgetExtName(stringfilename)
{
returnfilename.Substring(filename.LastIndexOf(".")+1);
}

加文字水印:

privatevoidaddText(stringfilename)
{
stringstr;
stringpath;
stringfilename1,extname;

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

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

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

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

//將圖片繪制到graphics中
g.DrawImage(image,0,0,image.Width,image.Height);
//設置文字的屬性
System.Drawing.Fontf=newFont("Verdana",10);
//判斷圖片的大小,如果圖片過小,不寫文字
if(image.Width>=250)
//在此設定在圖片上所加的文字
str="小妞表跑制作";
else
str="";
intx,y;
//寫的文字的起始位置,x,y坐標
x=image.Width-(int)(str.Length*15);
y=image.Height-20;
//設置字體的顏色
System.Drawing.Brushb=newSolidBrush(Color.White);
//寫字
g.DrawString(str,f,b,x,y);
//釋放graphics
g.Dispose();

//確定新圖片的文件路徑
stringnewpath=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename;
//保存寫上字的圖片
image.Save(newpath);
//釋放image
image.Dispose();
//刪除沒加水印的圖片,記得一定要放在image釋放之后,否則無法刪除
System.IO.File.Delete(path);
}加圖片水印:

privatevoidaddImage(stringfilename)
{
stringpath;
stringlogo_path;
stringfilename1,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.Imageimage=System.Drawing.Image.FromFile(path);

System.Drawing.ImagecopyImage=System.Drawing.Image.FromFile(logo_path);

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

//將水印打印到上傳圖片上去
g.DrawImage(copyImage,newRectangle(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();

//確定新圖片的文件路徑
stringnewpath=HttpContext.Current.Request.PhysicalApplicationPath+"images\\"+filename;
//保存寫上字的圖片
image.Save(newpath);
//釋放image
image.Dispose();
//刪除沒加水印的圖片,記得一定要放在image釋放之后,否則無法刪除
System.IO.File.Delete(path);
}

總結

以上是生活随笔為你收集整理的教你用.net为图片制作水印的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。