一、水印
1.畫布
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
2.創建繪制對象,告訴它往哪張圖片上繪制
Graphics g = Graphics.FromImage(img);
3.繪制的內容
string s = www.itnba.com;
4.繪制的字體
Font f = new Font("黑體",130);
5.繪制的畫刷
Brush b = new SolidBrush(Color.Red);
6.開始繪制
g.DrawString(s, f, b, 5, 5);
二、圖片驗證碼
Bitmap bit = new Bitmap(90, 40);//畫布大小 Graphics g = Graphics.FromImage(bit);//創建繪制對象,告訴它往哪張圖片上繪制 Random r = new Random(); string s = ""; Color color1 = Color.FromArgb(r.Next(155, 255), r.Next(155, 255), r.Next(155, 255)); g.FillRectangle(new SolidBrush(color1), 0, 0, 90, 40);//畫布顏色隨機 for (int i = 0; i < 10; i++)//隨機畫干擾線 { Color color3 = Color.FromArgb(r.Next(170, 255), r.Next(190, 255), r.Next(170, 255)); Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5)); g.DrawLine(pp, r.Next(0, 90), r.Next(0, 40), r.Next(0, 90), r.Next(0, 40)); } string yan = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";//設置隨機的驗證碼,從裡面抽取 int wei = 0; for (int i = 0; i < 4; i++)//取四位驗證碼,顏色隨機 { string m = yan.Substring(r.Next(0, yan.Length), 1); s += m; Color color2 = Color.FromArgb(r.Next(0, 155), r.Next(0, 155), r.Next(0, 155)); int w = r.Next(20, 25); Font f = new Font("黑體", w); SolidBrush b = new SolidBrush(color2); g.DrawString(m, f, b, wei, r.Next(0, 10)); wei += w; } Session["YZM"] = s;//把驗證碼保存到session中 for (int i = 0; i < 251; i++) { Color color3 = Color.FromArgb(r.Next(0, 255), r.Next(0, 255), r.Next(0, 255)); Pen pp = new Pen(new SolidBrush(color3), r.Next(0, 5)); int a = r.Next(0, 90); int b = r.Next(0, 40); g.DrawLine(pp, a, b, (a + 1), (b + 1)); } bit.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg); Response.End();
驗證碼:
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> <asp:Image ID="Image1" runat="server" ImageUrl="Default2.aspx" />//這裡圖片路徑不是一張圖片的話會默認打開
以上所述是小編給大家分享的Web前端開發之水印、圖片驗證碼,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的,在此也非常感謝大家對網站的支持!