File Upload & Draw watermark

Source: Internet
Author: User

File Upload:

Turns the relative path into an absolute path.
String absolutepath = Server.MapPath (RelativePath);

FileUpload CONTROLS:
Property:
FileName: File name
Hasfile:bool whether the file is selected
Filebytes: Binary data to upload a file
Method:
SaveAs (String absolute path): Upload, Save As.

One, upload to the hard disk folder
(i) Leaflets documents
The first step: Prepare the file and path:
Remove the file name from the client before
string fileName = Fileupload1.filename;

Prevent file names
filename = DateTime.Now.ToString ("yyyymmddhhmmsss") + filename;

Convert relative path to absolute path
String path = Server.MapPath ("uploads/" + fileName);

Step two: Perform the upload:
Upload
Fileupload1.saveas (path); parameter must root path

Attention:
1. How do I prevent file names?
2. How to prevent different users from the same point of time traditional one file name?

(ii) Transfer of multiple documents:
Idea: Traverse all the FileUpload controls in the form and upload if you select a file

int index = 0;
foreach (Control ctrl in Form1. Controls)
{
If (Ctrl is FileUpload)
{
index++;
Get each upload control
FileUpload upload = Ctrl as FileUpload;
The upload control has the file selected.
if (upload. HasFile)
{
Make file path out
String path = Server.MapPath ("uploads/" + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + index. ToString ("xx") + Upload. FileName);

Upload
Upload. SaveAs (path);
}
}
}

Second, upload to the database image field:
(i) Upload to the database
1. Do the operation code of the database. DA Data
The image field corresponds to the byte[] type in the program.

2. Do the code on the interface.
A. Remove the value of the interface
Fileupload1.filebytes-binary data for uploading files.
B. Send to the database

(b) Find it out from the database and show it
Law one: Garbage files are generated
Generate a JPG on the server and assign the path of this JPG to the image control

Law II: Make a separate page to display the binary data of the picture. Assign this page to the image control.


Uploading images and watermarks:
First, from the upload data, converted into a picture object.
Stream s = fileupload1.filecontent;
System.Drawing.Image img = System.Drawing.Image.FromStream (s);

Second, the picture object to draw the watermark
1. Making Pens
SolidBrush brush = new SolidBrush (color.yellow);
2. Making fonts
Font font = new Font ("Comic Sans MS", 18);
3. Locate the image drawing area
Graphics g = Graphics.fromimage (IMG);
g.DrawString ("http://www.itNBA.com", font, brush, 0, 0);

Third, the picture object to save to the hard drive up
string fileName = Fileupload1.filename;
String path = Server.MapPath ("uploads/" + fileName);
Img. Save (path);

File Upload & Draw watermark

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.