When we are doing web programs, we often encounter some problems with the image processing, today we have to sum up the places that need picture processing. It is also a summary of their learning process, hope can also give you some inspiration.
One, authentication code.
When we register or log in to some Web sites, we may encounter the place where we need to fill in the verification code, and we don't understand what the picture is like, in fact it's a randomly generated picture, You need to create a new ASPX page in the background or ashx a general process page to specifically generate such a picture. Here is an example to show you such a process!
First we create a new login page to simulate user login. The code is as follows: &NBSP
<div>
<table>
<tr><td> account:</td><td>
<asp:textbox id= "TextBox1 "runat=" server ></asp:TextBox></td></tr>
<tr><td> password:</td><td>
<asp:textbox id= "TextBox2" runat= "Server" textmode= "Password" ></asp:textbox></td></tr >
<tr><td> Verification Code:</td><td>
<asp:image id= "Image1" runat= "Server" Imageurl= " Validateno.aspx "/></td></tr>
<tr><td> Please enter:</td><td>
<asp: TextBox id= "Txtyzm" runat= "Server" ></asp:TextBox></td></tr>
<tr><td>
<asp:button id= "Btndl" runat= "server" text= "login" onclick= "Btndl_click"/></td><td> <asp
: Button id= "Button2" runat= "Server" text= "Cancel" Causesvalidation=false/></td></tr>
</table>
</div>
Layout as shown:
Then create a new validateno.aspx page that will be used to generate the captcha picture, and in this ASPX page just write the following code in the constructor:
protected void Page_Load (object sender, EventArgs e) {Random r = new Random (); int i = R.next (1000, 9999);//Generate a four-bit random number Bitmap bit = new Bitmap (100, 40);//Generate a bitmap of 100,40 size Graphics g = Graphics. FromImage (bit);//Create a drawing instance and use the bitmap created above as the artboard, and of course here, select an existing picture as an artboard.
You only need to swap the parameters in the FromImage () to the existing Image object G.drawline (new Pen (Brushes.blue), new Point (0, ten), New Point (100, 10); G.drawline (New Pen (Brushes.greenyellow), new Point (0,), New Point (100, 25))//Draw two straight lines, play the effect of a certain fuzzy validation code g.drawstring (I.T Ostring (), New Font ("Arial,", Brushes.green, New PointF (0, 0));//The generated four-digit verification code is drawn to the bit on the artboard. Save (Response.outputstream, imageformat.jpeg);//Saves the bitmap in the format of Jpeg session["Validateno"] = i.ToString (); The Seession value holds the value of the generated verification code to compare the value of the validation code entered by the user at logon Response.ContentType = "Image/jpeg";//change the input type to "Image/jpeg" Response.End (
); }
We have just said in the code to save the generated random captcha in seesion, then we can compare the value in the session with the value of the user input to determine if the user input verification code is correct, So we do this on the Aspx.cs page of the login page: (This is just a simulation test, in the actual development, if the verification code, password, account number are correct, will be directed to the new page)
protected void Btndl_click (object sender, EventArgs e)
{
if (session["Validateno"]!= null)
{
string s = session["Validateno"]. ToString ();
if (Txtyzm. Text!= s)
{
Response.Write ("<script>alert (' +s+ ') </script>");
else
{
Response.Write (' <script>alert (' OK ') </script> ');
}
else
{
Response.Write (' <script>alert (' Authentication code does not exist! ') </script> ");
}
Second, add text to the picture
Sometimes we will see some personal space or the homepage of the pictures are added to the corresponding text, like Tencent Weibo kind of send a picture will show Tencent Weibo words. Below I will show you how to add text to the picture.
First we create a new ASPX page with the following page layout:
<table style= "Background:lightblue" ><tr><td> Select Upload file: </td><td><asp:fileupload id= "FileUpload1" runat= "Server"/></td><td> <asp:button id= "btnupload" runat= "server" text= "Upload picture" on click= "Btnupload_click"/></td></tr> <tr><td colspan= "3" > <asp:image ID= "TouXiang" Run at= "Server"/></tr> </table> then we do the following in the handler function of the Click event of the Btnuplod button: protected void Btnupload_click ( Object sender, EventArgs e) {if (FileUpload1.FileName.Trim ()!= "") {string extension = Path.getextension (Fi Leupload1.filename)//First get the suffix of the file string FileName = DateTime.Now.Year.ToString () + DateTime.Now.Month.ToString () + Dateti Me. Now.Minute.ToString ()//To rename a string path = Server.MapPath (".") for the uploaded picture in the format of the current date.
+ "\\images\\" +filename + extension; FileUpload1.PostedFile.SaveAs (path);//Save the picture under the specified path Bitmap img = new Bitmap (path);//Create a new Bitmap object and use this as an artboard Graphics g =
Graphics.fromimage (IMG); G.draWstring ("Hello olive!", New Font ("XXFarEastFont-Arial"), Brushes.greenyellow, New PointF (20,20));//write "Hello Olive" in the Picture (20,20) G.
Dispose ();
NewPath = Server.MapPath (".") + "\\images\\" + fileName + "_new" + extension; Img. Save (NewPath)//Add text to the picture and save it, and delete the original image img.
Dispose ();
if (file.exists (path)) {file.delete (path);
Touxiangpath= "~/images/" + fileName + "_new" + extension; else {Response.Write <script>alert (' Please select the file you want to upload first!
') </script> "); }
}
Effect as shown:
third, generate a thumbnail image
now many blogs, personal homepage, space and so on have to edit personal information settings, in the editing of personal information may need to upload avatar, below we talk about how to generate a little bit like.
Page Layout We still refer to the top layout:
But add a line to the <table></table> to display the resulting thumbnail.
First we need to create a new Cutimage class CutImage.cs, which is designed to scale the picture as follows:
///<summary> (This picture scales the code reference from Blog Park Bo main king-two color days)
///capture picture
///</summary>
///<param name= "OPath" > Original image path </param>
///<param name= "Npaht" > New picture Path </ Param>
///<param name= "W" > Thumbnail width </param>
///<param name= "H" > the height of the thumbnail </param>
///<param name= "mode" > Interception mode </param>
Code:
public static void Cutimg (String oPath, String npaht, int w, int h,string mode) {Image oimg = Image.FromFile (OPath)
;
int ntowidth = W;
int ntoheight = h;
int x = 0;
int y = 0; int owidth = oimg.
Width; int oheight = oimg.
Height;
Switch (mode) {case "HW"://Is scaled according to the specified width and height, which may deform the break;
Case "W"://specified width, high proportional scaling ntoheight = owidth * oheight/ntowidth;
Break
Case "H"://specified height, wide proportional scaling ntowidth=owidth*oheight/ntoheight;
Break Case ' cut '://In accordance with the specified width, high scale if (oimg. Width/oimg. Height) > (ntowidth/ntoheight)) {oheight = oimg.
Height; Owidth = oimg.
Height * ntowidth/ntoheight;
y = 0; x = (oimg.
Width-owidth)/2; else {owidth = oimg.
Width; Oheight = oimg.
Width * NTOHEIGHT/NTOWIDTH;
x = 0; y = (oimg.
Height-oheight)/2;
} break;
Default:break;
//new BMP picture Image bitmap = new Bitmap (ntowidth, ntoheight); Create a new artboard Graphics GP = Graphics.fromimage (bitmap); Gp.
Interpolationmode = Interpolationmode.high; Gp.
SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; Empty the canvas and fill the GP with a transparent color with the background color.
Clear (color.transparent); Gp. DrawImage (oimg, New Rectangle (0, 0, Ntowidth, ntoheight), New Rectangle (x, Y, Owidth, oheight), GraphicsUnit.Pixel); The new thumbnail try {bitmap.
Save (Npaht, System.Drawing.Imaging.ImageFormat.Jpeg);
catch (Exception e) {throw e; finally {oimg.
Dispose (); Bitmap.
Dispose ();
}
}
Then our ASPX page's Btnupload button's handler code for the Click event is as follows:
protected void Btnupload_click (object sender, EventArgs e)
{
if (FileUpload1.FileName.Trim ()!= "")
{
//.......
//....... The code is omitted to be the same
if (file.exists (path))
{
file.delete (path);
}
String p = Server.MapPath (".") + "\\images\\";
Touxiangpath= "~/images/" + fileName + "_new" + extension;
Touxiang.imageurl = Touxiangpath;
Cutimage.cutimg (NewPath, p+ "Olive.jpg", M, "cut");//Call the Cutimage function of the class cutimg that scaled the picture, which is saved directly here for "Olive.jpg" is to facilitate the display of the following references, or to save them in other names and formats.
Luesuotu. IMAGEURL = "~/images/olive.jpg";
}
else
{
Response.Write <script>alert (' Please select the file you want to upload first! ') </script> ");
}
Build effect as shown in figure:
In order to facilitate the use of the picture I have been the zoom function encapsulated into a class, there are some other scaling functions, has exported class templates, interested friends can click to download Imagecut.zip, I hope to give you some help.