一:上傳圖片加水印dom
1 加文字水印:字體
1)上傳圖片後 , 製做畫布,將選中的圖片流製做成畫布,即:spa
System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.FileContent);code
2) 建立繪製對象,並放在畫布上,即:orm
Graphics g = Graphics.FromImage(image);對象
3)在畫布上畫的東西,須要4個參數,分別是:blog
1)畫的什麼東西,例:畫字體即圖片
string s = "www.fuchunju.com";
2)用什麼字體,多大字號,即:
Font f = new Font("微軟雅黑", 50);
3)用什麼顏色,什麼樣畫筆例如實體,紅色畫筆
Brush b = new SolidBrush(Color.Red);
4)在畫布的那個位置,第一個表明x軸,第二個參數表明y軸;例x軸10,y軸20即
Point p = new Point(10, 20);
5) 將畫的東西參數放在畫布上
g.DrawString(s, f, b, p);
6)上傳到文件夾,即:
image.Save(Server.MapPath(path));
順便經過image控件瀏覽一下,即將路徑賦給控件image1
Image1.ImageUrl=path;ip
總代碼以下:字符串
private void Button1_Click(object sender, EventArgs e) { string[] ns=FileUpload1.FileName.Split('.'); //將文件名以'.'拆分 if (ns[ns.Length - 1] == "aspx" || ns[ns.Length - 1] == "asp" || ns[ns.Length - 1] == "avi") //判斷上傳文件的類型 { Response.Write("<script> alert('不被容許上傳的文件類型') ;</script>"); return ; //阻止,不讓繼續執行下邊代碼 } string path = "Upload/"+DateTime.Now.ToString("yyyMMddHHmmssms")+FileUpload1.FileName;//1 製做畫布,將選中的圖片流製做成畫布,即: System.Drawing.Image image = System.Drawing.Image.FromStream(FileUpload1.FileContent); // 2 引用命名空間,建立繪製對象,並放在畫布上
using System.Drawing; Graphics g = Graphics.FromImage(image); //在畫布上畫的東西,須要4個參數,分別是: //1)畫的什麼東西,例:畫字體即 string s = "www.fuchunju.com"; //2)用什麼字體,多大字號 Font f = new Font("微軟雅黑", 50); //3)用什麼顏色,什麼樣畫筆例如實體,紅色畫筆 Brush b = new SolidBrush(Color.Red); //4)在畫布的那個位置,第一個表明x軸,第二個參數表明y軸;例x軸10,y軸20即 Point p = new Point(10, 20); //將畫的東西參數放在畫布上 g.DrawString(s, f, b, p); //上傳到文件夾,即: image.Save(Server.MapPath(path)); //順便經過image控件瀏覽一下,即將路徑賦給控件image1 Image1.ImageUrl=path; }
效果圖以下:
2) logo水印:
首先將logo圖片放在文件夾,仍舊用畫布因此仍需引用空間即:
using.system.drawing
而後寫以下代碼:
private void Button1_Click(object sender, EventArgs e) { string[] ns = FileUpload1.FileName.Split('.'); //將文件名以'.'拆分 if (ns[ns.Length - 1] == "aspx" || ns[ns.Length - 1] == "asp" || ns[ns.Length - 1] == "avi") //判斷上傳文件的類型 { Response.Write("<script> alert('不被容許上傳的文件類型') ;</script>"); return; //阻止,不讓繼續執行下邊代碼 } string path = "Upload/" + DateTime.Now.ToString("yyyMMddHHmmssms") + FileUpload1.FileName; // FileUpload1.SaveAs(end); //執行上傳命令 //1 製做畫布,將選中的圖片流製做成畫布,即: System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent); // 2 建立繪製對象,並放在畫布上 Graphics g = Graphics.FromImage(img); //畫布參數的位置參數; PointF p = new PointF(10, 20); //將文件夾裏的logo圖片取出來即: System.Drawing.Image logo = System.Drawing.Image.FromFile(Server.MapPath("Upload/dota2logo.png")); //將圖片放在畫布上的圖片上 g.DrawImage(logo,p); //上傳保存 img.Save(Server.MapPath(path)); //瀏覽 Image1.ImageUrl = path;
}
效果圖以下:
3 圖片驗證碼:
1)仍舊引用命名空間:
using. system.drawing;
2) 而後建立一個default窗體並寫如下代碼:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Drawing; public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //1準備寬100高50的圖片畫布即: Bitmap bm = new Bitmap(100, 50); Graphics g = Graphics.FromImage(bm); //在這個畫布上畫東西 List<Color> clist = new List<Color>(); //顏色集合 clist.Add(Color.Red); //往顏色集合裏添加顏色 clist.Add(Color.Orange); clist.Add(Color.Green); clist.Add(Color.Blue); clist.Add(Color.LightGray); clist.Add(Color.Yellow); clist.Add(Color.DarkGreen); string ss = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; //字符串ss的內容 string s = ""; //驗證碼 Random r = new Random(); //隨機數 Color bkcolor = clist[r.Next(0, clist.Count)]; //從顏色集合裏取出一個顏色做爲集合 clist.Remove(bkcolor); // 從顏色集合裏移除這個顏色,而後剩下的供字體用 g.FillRectangle(new SolidBrush(bkcolor), 0, 0, 100,50); //畫一個矩形做爲背景 for (int a = 0; a< 4; a++) { s += ss[r.Next(0, ss.Length)]; //從字符串ss的內容裏隨機取幾個四個數做爲驗證碼 } Session["yzm"] = s; Brush b =new SolidBrush(clist[r.Next(0,clist.Count)]); // 畫刷用實體,顏色從顏色集合裏隨機取 Font f = new Font("微軟雅黑", 20); //字體顏色,字號 Pen p = new Pen(new SolidBrush(clist[r.Next(0, clist.Count)]),r.Next(1,3)); //劃線所用的筆的顏色,粗細 for (var i = 0; i < 5; i++) { //5條幹擾線 Point p1 = new Point(r.Next(0, 100), r.Next(0, 50)); // 干擾線開始位置 Point p2 = new Point(r.Next(0, 100), r.Next(0, 50)); //干擾線結束位置 g.DrawLine(p, p1, p2); //畫干擾線 } g.DrawString(s,f,b,5,5); //畫驗證碼 bm.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);//用jpeg的格式保存到流裏 } }
再在另外一個default窗體裏展現: