CopyCode The Code is 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 the constructor logic here
//
}
/// <Summary>
/// Enumeration of File Upload types
/// </Summary>
Public Enum filetype
{
JPG, GIF, BMP, PNG
}
# Region obtain the file suffix
/// <Summary>
/// Obtain the 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
# Check whether the region file is valid
///
// check whether the uploaded file is valid
///
// file suffix
//
Public static bool checkfileextends (string fileextends)
{< br> bool status = false;
fileextends = fileextends. tolower ();
string [] Fe = enum. getnames (typeof (filetype);
for (INT I = 0; I {< br> If (Fe [I]. tolower () = fileextends)
{< br> Status = true;
break;
}< BR >}< br> return status;
}< BR ># endregion
# Region save the file
/// <Summary>
/// Save the 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! = "")
{
// Obtain the file suffix
Fileextends = EC. uploadobj. getfileextends (filename );
If (! EC. uploadobj. checkfileextends (fileextends ))
{
EC. messageobject. showpre ("the uploaded file type is invalid ");
}
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
# Add a text watermark to Region
/// <Summary>
/// Add a 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 ("the file does not exist ");
}
If (text = string. Empty)
{
Return;
}
// Determine whether the file type is image
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; // the length of the text.
// The following defines a rectangle area, and then draws white and black characters in the rectangle.
Float rectx = 0;
Float recty = 0;
Float rectwidth = text. length * (fontsize + 8 );
Float rectheight = fontsize + 8;
// Declare the rectangular Field
Rectanglef textarea = new rectanglef (rectx, recty, rectwidth, rectheight );
Font font = new font ("", fontsize); // define the font
Brush whitebrush = new solidbrush (color. White); // white brush, used to draw text
Brush blackbrush = new solidbrush (color. Black); // black brush for background painting
G. fillrectangle (blackbrush, rectx, recty, rectwidth, rectheight );
G. drawstring (text, Font, whitebrush, textarea );
Memorystream MS = new memorystream ();
Bitmap. Save (MS, imageformat. JPEG );
// Output the processed image. For demonstration convenience, I will display the image on the page
// Response. Clear ();
// Response. contenttype = "image/JPEG ";
// Response. binarywrite (Ms. toarray ());
G. Dispose ();
Bitmap. Dispose ();
Image. Dispose ();
}
# Endregion
}
}