Image upload, image watermark, Verification code production

Source: Internet
Author: User

File Upload:

  Controls used: FileUpload

When using the idea:

1, determine whether the user selected files

Fileupload.filename Gets the selected file name, determines the length, if the length is greater than 0 means that the file has been selected

JS End: Gets the control by ID and then the value of the control gets the selected file name

2. Save the file to the server

Fileupload.saveas ("absolute path");

3. Get absolute path

Write a relative path first: "Uploads/abc.txt", for example

Then map the path to absolute path: Server.MapPath ("Uploads/abc.txt");

4, keep the original file type and file name

The relative path is changed to "uploads/" + fileupload1.filename;

5, prevent the file name overlay problem

"uploads/" + request.cookies["user"]. Value + DateTime.Now.ToString ("yyyymmddhhmmssms") + fileupload1.filename;

That is, in front of the file name stitching on the login user name and operation time, accurate to milliseconds

6. Restrict user-selectable file types

Add Property accept= ". Jpg,.png,.jpeg,.txt" to the control

However, in the Open dialog box, you can still select all the files, only through the JS side to limit:

document.getElementById ("Button1"). onclick = function () {        var fileName = document.getElementById ("FileUpload1"). value;                var name = Filename.substr (filename.length-4, 4);        var name1 = Filename.substr (filename.length-5, 5);        if (name! = ". jpg" && name! = ". png" && name! = ". txt" && name1! = ". jpeg")        {            alert ("Please select the correct The file! You're looking for something, huh? ");            return false;        }     };

Image plus watermark:

<%@ Page language="C #"autoeventwireup="true"codefile="Shuiyin.aspx.cs"inherits="Shuiyin"%><! 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="Button"/> <asp:image id="Image1"runat="Server"/> </div> </form></body>
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingsystem.web;usingSystem.Web.UI;usingSystem.Web.UI.WebControls;//Be sure to add this reference!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!usingSystem.Drawing; Public Partial classshuiyin:system.web.ui.page{protected voidPage_Load (Objectsender, EventArgs e) {Button1.Click+=button1_click; }    voidButton1_Click (Objectsender, EventArgs e) {        //get the picture you want to uploadSystem.Drawing.Image img =System.Drawing.Image.FromStream (fileupload1.filecontent); //Add Watermark//Turn The get picture into a GDI drawing surface and start drawingGraphics g =graphics.fromimage (IMG); //corresponding (watermark content, watermark font, watermark color, watermark location)g.DrawString ("ha ha haha",NewFont ("Microsoft Ya-Black", -),NewSolidBrush (color.red),NewPointF (0,0)); //Save Picture//img refers to the folder name, followed by renaming the file to be watermarked        stringUL ="img/"+ DateTime.Now.ToString ("yyyymmddhhmmssms") +Fileupload1.filename; //save and generate an absolute pathimg.        Save (Server.MapPath (UL)); //show the effect and look at it.Image1.imageurl =ul; }}

Effect:

Image upload, image watermark, Verification code production

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.