asp.net file upload class (get file suffix name, save file, add text watermark) _ Practical skills

Source: Internet
Author: User
Tags file upload save file

Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Drawing;
Using System.IO;
Using System.Drawing.Imaging;

Namespace EC
{
<summary>
Upload class
</summary>
public class Uploadobj
{

Public Uploadobj ()
{
//
TODO: Add constructor logic here
//
}
<summary>
Enumeration of types that allow file uploads
</summary>
public enum FileType
{
Jpg,gif,bmp,png
}

#region Get file suffix
<summary>
Get file suffix
</summary>
<param name= "filename" > file name </param>
<returns></returns>
public static string Getfileextends (string filename)
{
string ext = null;
if (filename. IndexOf ('. ') > 0)
{
string[] fs = filename. Split ('. ');
ext = Fs[fs. LENGTH-1];
}
return ext;
}
#endregion

#region Detection file is legal
<summary>
Detecting whether the uploaded file is legitimate
</summary>
<param name= "fileextends" > File suffix name </param>
<returns></returns>
public static bool Checkfileextends (string fileextends)
{
BOOL status = FALSE;
Fileextends = Fileextends.tolower ();
string[] Fe = Enum.getnames (typeof (FileType));
for (int i = 0; i < Fe. Length; i++)
{
if (fe[i). ToLower () = = Fileextends)
{
Status = TRUE;
Break
}
}
return status;
}
#endregion

#region Save a file
<summary>
Save File
</summary>
<param name= "Fpath" > Full path, Server.MapPath () </param>
<param name= "Myfileupload" > Upload control </param>
<returns></returns>
public static string Photosave (String Fpath,fileupload myfileupload)
{
string s = "";
String fileextends = "";
string fileName = Myfileupload.filename;
if (FileName!= "")
{
Get file suffix
Fileextends = EC. Uploadobj.getfileextends (FileName);
if (! EC. Uploadobj.checkfileextends (Fileextends))
{
EC. Messageobject.showpre ("Upload file type is illegal");
}
Random rd = new Random ();
s = EC. Randomobject.daterndname (RD) + "." + fileextends;
string file = Fpath + "\" + S;
Try
{
Myfileupload.saveas (file);
}
catch (Exception ee)
{
throw new Exception (EE. ToString ());
}
}
return s;
}

#endregion

#region Add text watermark

<summary>
Add text watermark
</summary>
<param name= "FileName" > File name Path (full path) </param>
<param name= "text" > File </param>
public void Addtexttoimg (string fileName, string text)
{
if (! File.exists (FileName))
{
throw new FileNotFoundException ("file does not exist");
}
if (Text = = String. Empty)
{
Return
}
To determine whether a file type is an image type

System.Drawing.Image Image = System.Drawing.Image.FromFile (fileName);
Bitmap Bitmap = new Bitmap (image, image. Width, image. Height);
Graphics g = graphics.fromimage (bitmap);
float fontsize = 12.0f;//font Size
float textWidth = text. Length * fontsize;//of text
The following defines a rectangular area, which is then painted with a white background and a black word in the rectangle.
float rectx = 0;
float recty = 0;
float rectwidth = text. Length * (fontsize + 8);
Float Rectheight = fontsize + 8;
Declaring a rectangular field
RectangleF TextArea = new RectangleF (Rectx, Recty, Rectwidth, rectheight);
Font font = new Font ("XXFarEastFont-Arial", fontsize);//define Fonts
Brush Whitebrush = new SolidBrush (color.white)//White brush, drawing text
Brush Blackbrush = new SolidBrush (color.black)//Black brush, drawing background
G.fillrectangle (Blackbrush, Rectx, Recty, Rectwidth, rectheight);
g.DrawString (text, font, Whitebrush, TextArea);
MemoryStream ms = new MemoryStream ();
Bitmap. Save (MS, Imageformat.jpeg);
Output processing image, here for demonstration convenience, I will display the picture in the page
Response.Clear ();
Response.ContentType = "Image/jpeg";
Response.BinaryWrite (Ms. ToArray ());
G.dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}
#endregion
}
}

Related Article

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.