C#--web Watermark +++++ Verification Code

Source: Internet
Author: User

Watermark: A FileUpload a button

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Drawing;
Using System.IO;
Public partial class watermark: System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{

}
protected void Shangchuan_click (object sender, EventArgs e)
{
First, from the upload data, converted into a picture object.
Stream s = fileupload1.filecontent;
System.Drawing.Image img = System.Drawing.Image.FromStream (s);//Focus
Second, the picture object to draw the watermark
1. Making Pens
SolidBrush brush=new SolidBrush (Color.yellow);
2. Making fonts
Font f = new Font ("Buxton Sketch", 12);

3. Locate the image drawing area
Graphics GS = Graphics.fromimage (IMG);

4. Determine where to begin drawing
float x = 0, y = 0;

SizeF size = Gs. MeasureString ("Happy new year!", f);//determines the size of the written watermark (that is, the word to be written)
x = img. Width-size.width;
y = img. Height-size.height;
5. Start drawing
Gs. DrawString ("Happy new year!", F, brush, x, y);
Third, the picture object to save to the hard drive up
String app = Fileupload1.filename;
String path = Server.MapPath ("/upload/") +app;
Img. Save (path);


}
}

Verification Code:

Display page (Verification code, verification code in CS code):

ASPX page (the original random number is changed after clicking the verification code in the script):

<%@ page language= "C #" autoeventwireup= "true" codefile= "Denglu.aspx.cs" inherits= "Denglu"%>

<! DOCTYPE html>

<meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>
<title></title>
<script language= "JavaScript" >
function Bianhua ()
{
var img = document.getElementById ("Image1");
Img.setattribute ("src", "captcha. aspx?id=" + math.random ());
}
</script>
<body>
<form id= "Form1" runat= "Server" >
<div>

<asp:textbox id= "TextBox1" runat= "Server" ></asp:TextBox>
<asp:image id= "Image1" runat= "Server" imageurl= "verification code. aspx" onclick= "Bianhua ()"/>
<br/>
<asp:button id= "Deng" runat= "server" text= "login"/>

</div>
</form>
</body>

Generate picture page for Captcha:

Aspx.cs

Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Drawing;
Using System.IO;
Public partial class verification code: System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Bitmap img = new Bitmap (70, 30);//Create a new bitmap
String Suijishu = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890";
A collection string of all randomly generated characters
Random ran = new random ();
String yzm = "";
for (int i = 0; i < 4; i++)
{
int a = ran. Next (Suijishu. Length);
Yzm + = Suijishu. Substring (a,1);
}

The above is to produce 4 random characters consisting of a 4 character string

session["Code" = Yzm;
A randomly generated string is recorded for verification when the user fills out

、、、 the following is the resulting string watermark onto the resulting bitmap (since the bitmap default background color is black)
SolidBrush brush = new SolidBrush (color.green);//Outline Brush color
Font fo = new Font ("SketchFlow Print", 18);//description Fonts and sizes

Graphics GS = Graphics.fromimage (IMG);//Describe the image to be watermarked, that is, the newly generated bitmap

Gs. DrawString (Yzm, FO, brush, 0, 0);//Sketch Watermark

Save the picture to the output stream
Img. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End ();
}
}

Or the background image of the verification code is white:

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 YZM:System.Web.UI.Page
{
protected void Page_Load (object sender, EventArgs e)
{
Create a picture
Bitmap img = new Bitmap (60, 30);
Generate a random number
String str = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
Random rand = new Random ();
String code = "";
for (int i = 0; i < 4; i++)
{
int start = rand. Next (str. Length);
Code + = str. Substring (start, 1);
}
session["Yzm"] = code;
Draw a random number to the picture
SolidBrush brush = new SolidBrush (color.white);
Font font = new Font ("Lucida Sans Unicode", 14);
Graphics g = Graphics.fromimage (IMG);

A. Paint the background of the picture white
G.fillrectangle (brush, 0, 0, 60, 30);
B. Change the color of the brush
Brush. Color = color.red;


g.DrawString (code, font, brush, 0, 0);

Save the picture to the output stream
Img. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Jpeg);

Response.End ();
}
}

C#--web Watermark +++++ Verification Code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.