Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法
兩者都需要引入命名空間:using System.Drawing;
一、圖片水印
前臺(tái)Photoshuiyin.aspx代碼:
<div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上傳" /><br /> <asp:Image ID="Image1" runat="server" /> </div>
后臺(tái)Photoshuiyin.aspx.cs代碼:
protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
//1、制作畫布
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
Graphics g = Graphics.FromImage(img);
//水印樣式:畫什么東西
string a = "http://www.itnba.com";
//字體、大小
Font f = new Font("黑體", 30);
//顏色
Brush b = new SolidBrush(Color.Red);
//0,0——開始畫水印的位置
g.DrawString(a, f, b, 0, 0);
//保存路徑
string path = "images/" + FileUpload1.FileName;
img.Save(Server.MapPath(path));
//在image控件中展示
Image1.ImageUrl = path;
}
效果展示:

二、圖片驗(yàn)證碼
前臺(tái)Photoyanzhengma.aspx代碼:
<form id="form1" runat="server">
<div>
用戶名:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
密碼:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
驗(yàn)證碼:<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
<asp:Image ID="Image1" runat="server" ImageUrl="YZM.aspx" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
</div>
</form>
</body>
</html>
<script type="text/javascript">
var aaa = 1;
document.getElementById("Image1").onclick = function () {
this.setAttribute("src", "YZM.aspx?id=" + aaa);
aaa++;
};
</script>
鏈接頁(yè)面“YZM.aspx”——無(wú)需前臺(tái)代碼,后臺(tái)代碼是:
protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
string aaa = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
//生成畫布
Bitmap img = new Bitmap(80, 30);
//畫布背景色泛性組合
List<Color> Clist = new List<Color>();
Clist.Add(Color.Yellow);
Clist.Add(Color.Green);
Clist.Add(Color.Blue);
Clist.Add(Color.Aqua);
Clist.Add(Color.Orange);
Clist.Add(Color.Pink);
Graphics g = Graphics.FromImage(img);
g.FillRectangle(new SolidBrush(Clist[r.Next(0, Clist.Count)]), 0, 0, 80, 30);
//隨機(jī)生成顯示的驗(yàn)證碼組合
string str = "";
for (int i = 0; i < 4; i++)
{
str += aaa.Substring(r.Next(0, aaa.Length), 1);
}
Session["YZM"] = str;
Font f = new Font("黑體", 20);
Brush b = new SolidBrush(Color.Red);
//生成
g.DrawString(str, f, b, 10, 0);
//添加干擾線
for (int i = 0; i < r.Next(6, 20); i++)
{
Brush bb = new SolidBrush(Clist[r.Next(0, Clist.Count)]);
Pen p = new Pen(bb, 1);
g.DrawLine(p, r.Next(0, 80), r.Next(0, 30), r.Next(0, 80), r.Next(0, 30));
}
//保存完成
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}
效果展示:

以上所述是小編給大家介紹的Asp.net開發(fā)之webform圖片水印和圖片驗(yàn)證碼的實(shí)現(xiàn)方法,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Asp.net Web Api實(shí)現(xiàn)圖片點(diǎn)擊式圖片驗(yàn)證碼功能
- asp.net點(diǎn)選驗(yàn)證碼實(shí)現(xiàn)思路分享 (附demo)
- ASP.NET中畫圖形驗(yàn)證碼的實(shí)現(xiàn)代碼
- asp.net登錄驗(yàn)證碼實(shí)現(xiàn)方法
- asp.net之生成驗(yàn)證碼的方法集錦(一)
- 如何使用ASP.NET制作簡(jiǎn)單的驗(yàn)證碼
- asp.net驗(yàn)證碼圖片生成示例
- ASP.NET 實(shí)現(xiàn)驗(yàn)證碼以及刷新驗(yàn)證碼的小例子
- Asp.net實(shí)現(xiàn)手寫驗(yàn)證碼的操作代碼
相關(guān)文章
在ASP.net中保存/取出圖片入/從SQL數(shù)據(jù)庫(kù)
在ASP.net中保存/取出圖片入/從SQL數(shù)據(jù)庫(kù)...2006-09-09
asp.net mvc signalr簡(jiǎn)單聊天室制作過(guò)程分析
這篇文章主要為大家分析了asp.net mvc signalr簡(jiǎn)單聊天室制作過(guò)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-09-09
Asp.net中使用Sqlite數(shù)據(jù)庫(kù)的方法
Sqlite是最近比較流行的數(shù)據(jù)庫(kù)了,擁有比Access高效快速,易操作易實(shí)施。完全不需要在客戶端進(jìn)行任何的配置,只需要在站點(diǎn)中引用入DLL文件即可使用了。2009-11-11
在ASP.Net?Core應(yīng)用程序中使用Bootstrap4
這篇文章介紹了在ASP.Net?Core應(yīng)用程序中使用Bootstrap4的方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-01-01
ASP.NET Core 3.x 并發(fā)限制的實(shí)現(xiàn)代碼
這篇文章主要介紹了ASP.NET Core 3.x 并發(fā)限制的實(shí)現(xiàn)代碼,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
ASP.NET AJAX時(shí)用alert彈出對(duì)話框
ASP.NET AJAX alert彈出對(duì)話框 解決 asp.net onClientClick 與 驗(yàn)證控件沖突問(wèn)題2008-12-12
ASP.NET 獲取存儲(chǔ)過(guò)程返回值的實(shí)現(xiàn)代碼
ASP.NET 獲取存儲(chǔ)過(guò)程返回值的實(shí)現(xiàn)代碼,需要的朋友可以參考下。2011-12-12
asp.net動(dòng)態(tài)加載自定義控件的方法
這篇文章主要介紹了asp.net動(dòng)態(tài)加載自定義控件的方法,涉及asp.net動(dòng)態(tài)加載控件的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04

