English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Implementation Method of Webform Image Watermark and Image Verification Code in Asp.net Development

Both need to import the namespace: using System.Drawing;

I. Image Watermark

Front-end PhotoWatermark.aspx Code:

<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="Upload" /><br />
<asp:Image ID="Image1" runat="server" />
</div>

Back-end PhotoWatermark.aspx.cs Code:

protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += Button1_Click;
}
void Button1_Click(object sender, EventArgs e)
{
//1、Create Canvas
System.Drawing.Image img = System.Drawing.Image.FromStream(FileUpload1.FileContent);
Graphics g = Graphics.FromImage(img);
//Watermark Style: Draw what
string a = "http://www.itnba.com";
//Font, Size
Font f = new Font("黑体", 30);
//Color
Brush b = new SolidBrush(Color.Red);
//0,0——Start position of watermark drawing
g.DrawString(a, f, b, 0, 0);
//Save Path
string path = "images"/" + FileUpload1.FileName;
img.Save(Server.MapPath(path));
//Display in the image control
Image1.ImageUrl = path;
}

Effect Display:

II. Image Verification Code

Front-end Photo Verification Code Code:

<form id="form1" runat="server">
<div>
Username:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
Password:<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<br />
Captcha:<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>

Link page "YZM.aspx"——No front-end code, the back-end code is:

protected void Page_Load(object sender, EventArgs e)
{
Random r = new Random();
string aaa = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
//Generate canvas
Bitmap img = new Bitmap(80, 30);
//Canvas background color mixture
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);
//Randomly generate the display code combination
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++)
{

Pen p = new Pen(bb, 1
 80), r.Next(0, 30), r.Next(0, 80), r.Next(0, 30));
}
//Save completed
img.Save(Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
Response.End();
}

Effect Display:

The above-mentioned methods of implementing image watermark and image verification code in Asp.net development webform introduced by the editor are for your reference. I hope it will be helpful to you. If you have any questions, please leave a message, and the editor will reply to you in time. At the same time, I would also like to express my heartfelt thanks to everyone for their support of the Yell Tutorial website!

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.)

You May Also Like