First, picture and watermark
Front Code:
<%@ Page language="C #"autoeventwireup="true"codefile="Default1.aspx.cs"inherits="DEFAULT1"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div> <asp:fileupload id="FileUpload1"runat="Server"/><asp:button id="Button1"runat="Server"text="Upload"/><br/> <asp:image id="Image1"runat="Server"/> </div> </form></body>Background code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Drawing; Public Partial classdefault1:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; } voidButton1_Click (Objectsender, EventArgs e) { //1. Get the pictures first//we're going to paint the canvas.System.Drawing.Image img =System.Drawing.Image.FromStream (fileupload1.filecontent); //2. Draw watermark on topGraphics g = Graphics.fromimage (IMG);//This is the drawing method, the right side of the equal sign is drawn on that picture. strings ="http://www.itnba.com";//the drawn stringFont f =NewFont ("Microsoft Ya-Black", -);//drawing the font usedSolidBrush B =NewSolidBrush (color.red);//Paint the brush used, set the colorg.DrawString (S, F, B,0,0);//Finish Drawing//3. Save to server specified path, and display in image control stringPath = Server.MapPath ("uploads/"+fileupload1.filename); Img. Save (path);//Save the finished picture, note: Never Save the selected pictureImage1.imageurl="uploads/"+Fileupload1.filename; }}
Second, the Image verification code
1. First reference namespace: System. Drawing;
2. Use an ASPX page as a picture path, draw in the Load event, and save in the memory stream
Front Code:
<%@ Page language="C #"autoeventwireup="true"codefile="Default2.aspx.cs"inherits="DEFAULT2"%><! DOCTYPE html>"http://www.w3.org/1999/xhtml">"Server"><meta http-equiv="Content-type"Content="text/html; Charset=utf-8"/> <title></title>"Form1"runat="Server"> <div>Please enter the verification code:<asp:textbox id="TextBox1"runat="Server"></asp:textbox><asp:image id="Image1"runat="Server"Imageurl="~/yzm.aspx"/><br/> <asp:button id="Button1"runat="Server"text="Validation"/><asp:label id="Label1"runat="Server"text="Label"></asp:Label> </div> </form></body>"Text/javascript">varAAA =0; document.getElementById ('Image1'). onclick =function () { This. SetAttribute ("src","yzm.aspx?id="+AAA); AAA++; };</script>
Background code:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls; Public Partial classdefault2:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; } voidButton1_Click (Objectsender, EventArgs e) { if(TextBox1.Text = = session["YZM"]. ToString ()) {Label1.Text="Right! "; } Else{Label1.Text="Error! "; } }}
Generate background code for the picture captcha:
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;usingSystem.Drawing; Public Partial classyzm:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) { //1. Preparing the canvasBitmap img =NewBitmap ( -, -); //2. Draw the verification code on the canvas stringall ="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"; stringAAA =""; Random R=NewRandom (); for(inti =0; I <4; i++) {AAA+ = all. Substring (R.next (All). Length),1); } session["YZM"] =AAA; Graphics g=graphics.fromimage (IMG); Font F=NewFont ("Microsoft Ya-Black", -); SolidBrush b=NewSolidBrush (Color.green); g.DrawString (AAA, F, B,0,0); Img. Save (Response.outputstream, System.Drawing.Imaging.ImageFormat.Png); }}
Upload a watermark image, image verification code