For the need to upload images on this website, I have made this automatic batch Upload by using the C # language on the ASP.net Platform Based on a lot of mature experience. the ASPX file has been successfully debugged and used on this website. It is now available for your reference.
The main functions of this program include:
(1) You can change the directories uploaded to the server based on your needs. The uploaded source images, thumbnails, text watermarks, and image watermarks are saved to different directories under the selected directories;
(2) Automatically Check directories. If no directories are selected, they are automatically created;
(3) set the size of the generated thumbnail;
(4) You can choose whether to generate text and image watermarks. By default, no watermark image is generated;
(5) You can add or delete images to be uploaded.
Related comments are added in this program, so the code is directly sent and no longer explained.
Background Program:
Using System;
Using System. Collections;
Using System. Configuration;
Using System. Data;
Using System. Linq;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. HtmlControls;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Xml. Linq;
Using System. IO;
Using System. Net;
Using System. Text. RegularExpressions;
/// <Summary>
/// FileUpload1.HasFile if it is true, it indicates that the control has a file to upload.
/// FileUpload1.FileName
/// FileUpload1.FileContent returns a stream object pointing to the uploaded file
/// FileUpload1.PostedFile
/// FileUpload1.PostedFile. ContentLength returns the size of the uploaded file in bytes.
/// FileUpload1.PostedFile. ContentType: the MIME content type of the uploaded file, that is, the file type. For example, "image/jpg" is returned"
/// FileUpload1.PostedFile. FileName)
/// FileUpload1.PostedFile. InputStream returns a stream object pointing to the uploaded file.
/// The FileInfo object indicates the files on the disk or network. Provide the file path to create a FileInfo object:
/// </Summary>
Public partial class BackManagement_ImagesUpload: System. Web. UI. Page
{
Public string treePath = "";
Public int imageW = 100;
Public int imageH = 100;
Protected void Page_Load (object sender, EventArgs e)
{
This. Button5.Attributes. Add ("Onclick", "window. close ();"); // when you close the current page locally without sending it to the server to close the current page
If (! Page. IsPostBack)
{
Label2.Text = Server. MapPath ("/");
TextBox3.Text = "ImageUpload ";
TreePath = Server. MapPath ("/") + TextBox3.Text. Trim () + "/";
TextBox4.Text = imageW. ToString ();
TextBox5.Text = imageH. ToString ();
}
}
Protected void btnload_Click (object sender, EventArgs e)
{
// If the directory for saving the image does not exist, create it
TreePath = Server. MapPath ("/") + TextBox3.Text. Trim () + "/";
ImageW = Convert. ToInt32 (TextBox4.Text. ToString ());
ImageH = Convert. ToInt32 (TextBox5.Text. ToString ());
If (! File. Exists (treePath + "images") // If/ImageUpload/images does not exist, create/ImageUpload/images to store the source Image
{
System. IO. Directory. CreateDirectory (treePath + "images ");
}
If (! File. Exists (treePath + "thumbnails") // If/ImageUpload/thumbnails does not exist, create/ImageUpload/thumbnails to store thumbnail slices
{
System. IO. Directory. CreateDirectory (treePath + "thumbnails ");
}
If (! File. Exists (treePath + "textImages") // If/ImageUpload/textImages does not exist, create/ImageUpload/textImages for saving text watermark Images
{
System. IO. Directory. CreateDirectory (treePath + "textImages ");
}
If (! File. Exists (treePath + "waterImages") // If/ImageUpload/waterImages does not exist, create/ImageUpload/waterImages to store image watermarks.
{
System. IO. Directory. CreateDirectory (treePath + "waterImages ");
}
If (FileUpload1.HasFile) // if it is true, it indicates that the control has files to be uploaded.
{
String fileContentType = FileUpload1.PostedFile. ContentType;
If (fileContentType = "image/bmp" | fileContentType = "image/gif" | fileContentType = "image/pjpeg ")
{
String name = FileUpload1.PostedFile. FileName; // return the full path of the file on the client (including the full name of the file name)
FileInfo file = new FileInfo (name); // The FileInfo object indicates the file on the disk or network. Provide the file path to create a FileInfo object:
String fileName = file. Name; // file Name
String fileName_s = "x _" + file. Name; // The Name Of The thumbnail file.
String fileName_sy = "text _" + file. Name; // watermark image file Name (text)
String fileName_syp = "water _" + file. Name; // watermark image file Name (image)
String webFilePath = treePath + "images/" + fileName; // server-side file path
String webFilePath_s = treePath + "thumbnails/" + fileName_s; // server-side thumbnail path
String webFilePath_sy = treePath + "textImages/" + fileName_sy; // watermark image path (text) on the server side)
String webFilePath_syp = treePath + "waterImages/" + fileName_syp; // watermark path (image) on the server side)
String webFilePath_sypf = Server. MapPath ("../images/tzwhx.png"); // Server-side watermark image path (image)
If (! File. Exists (webFilePath ))
{
Try
{
FileUpload1.SaveAs (webFilePath); // use the SaveAs method to save the file
If (CheckBox1.Checked) // whether to generate a text watermark image
{
AddWater (webFilePath, webFilePath_sy );
}
If (CheckBox2.Checked) // whether to generate a graphic watermark
{
AddWaterPic (webFilePath, webFilePath_syp, webFilePath_sypf );
}
MakeThumbnail (webFilePath, webFilePath_s, imageW, imageH, "Cut"); // method for generating thumbnails
Label1.Text = "prompt: the file" "+ fileName +" "is uploaded successfully, and a" + fileName_s + "thumbnail is generated. The file type is" + FileUpload1.PostedFile. contentType + ", file size:" + FileUpload1.PostedFile. contentLength + "B ";
Image1.ImageUrl = "/" + TextBox3.Text. ToString () + "/images/" + fileName;
TextBox1.Text = webFilePath;
TextBox2.Text = "/" + TextBox3.Text. ToString () + "/images/" + fileName;
}
Catch (Exception ex)
{
Label1.Text = "prompt: File Upload Failed. cause of failure:" + ex. Message;
}
}
Else
{
Label1.Text = "prompt: The file already exists. Please rename it and upload it ";
}
}
Else
{
Label1.Text = "prompt: the file type does not match ";
}
}
}
/**/
/// <Summary>
/// Generate a thumbnail
/// </Summary>
/// <Param name = "originalImagePath"> source image path (physical path) </param>
/// <Param name = "thumbnailPath"> thumbnail path (physical path) </param>
/// <Param name = "width"> thumbnail width </param>
/// <Param name = "height"> thumbnail height </param>
/// <Param name = "mode"> how to generate a thumbnail </param>
Public static void MakeThumbnail (string originalImagePath, string thumbnailPath, int width, int height, string mode)
{
System. Drawing. Image originalImage = System. Drawing. Image. FromFile (originalImagePath );
Int towidth = width;
Int toheight = height;
Int x = 0;
Int y = 0;
Int ow = originalImage. Width;
Int oh = originalImage. Height;
Switch (mode)
{
Case "HW": // specify high-width Scaling (possibly deformed)
Break;
Case "W": // specify the width, and the height is proportional.
Toheight = originalImage. Height * width/originalImage. Width;
Break;
Case "H": // specify the height. The width is proportional.
Towidth = originalImage. Width * height/originalImage. Height;
Break;
Case "Cut": // specify the height and width (not deformed)
If (double) originalImage. Width/(double) originalImage. Height> (double) towidth/(double) toheight)
{
Oh = originalImage. Height;
Ow = originalImage. Height * towidth/toheight;
Y = 0;
X = (originalImage. Width-ow)/2;
}
Else
{
Ow = originalImage. Width;
Oh = originalImage. Width * height/towidth;
X = 0;
Y = (originalImage. Height-oh)/2;
}
Break;
Default:
Break;
}
// Create a bmp Image
System. Drawing. Image bitmap = new System. Drawing. Bitmap (towidth, toheight );
// Create a canvas
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (bitmap );
// Set a high quality Interpolation Method
G. InterpolationMode = System. Drawing. Drawing2D. InterpolationMode. High;
// Set high quality and smooth Low Speed
G. SmoothingMode = System. Drawing. Drawing2D. SmoothingMode. HighQuality;
// Clear the canvas and fill it with a transparent background color
G. Clear (System. Drawing. Color. Transparent );
// Draw the specified part of the original image at the specified position and in the specified size
G. DrawImage (originalImage, new System. Drawing. Rectangle (0, 0, towidth, toheight ),
New System. Drawing. Rectangle (x, y, ow, oh ),
System. Drawing. GraphicsUnit. Pixel );
Try
{
// Save the thumbnail in jpg format
Bitmap. Save (thumbnailPath, System. Drawing. Imaging. ImageFormat. Jpeg );
}
Catch (System. Exception e)
{
Throw e;
}
Finally
{
OriginalImage. Dispose ();
Bitmap. Dispose ();
G. Dispose ();
}
}
/**/
/// <Summary>
/// Add a text watermark to the image
/// </Summary>
/// <Param name = "Path"> original server image Path </param>
/// <Param name = "Path_sy"> path of the image with a text watermark </param>
Protected void AddWater (string Path, string Path_sy)
{
String addText = "http://www.tzwhx.com /";
System. Drawing. Image image = System. Drawing. Image. FromFile (Path );
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (image );
G. DrawImage (image, 0, 0, image. Width, image. Height );
System. Drawing. Font f = new System. Drawing. Font ("Verdana", 10); // The Font position is left blank and 10
System. Drawing. Brush B = new System. Drawing. SolidBrush (System. Drawing. Color. Green );
G. DrawString (addText, f, B, 14, 14); // The font size is 14X14.
G. Dispose ();
Image. Save (Path_sy );
Image. Dispose ();
}
/**/
/// <Summary>
/// Generate an image watermark on the Image
/// </Summary>
/// <Param name = "Path"> original server image Path </param>
/// <Param name = "Path_syp"> generated image path with image watermark </param>
/// <Param name = "Path_sypf"> watermark image path </param>
Protected void AddWaterPic (string Path, string Path_syp, string Path_sypf)
{
System. Drawing. Image image = System. Drawing. Image. FromFile (Path );
System. Drawing. Image copyImage = System. Drawing. Image. FromFile (Path_sypf );
System. Drawing. Graphics g = System. Drawing. Graphics. FromImage (image );
G. drawImage (copyImage, new System. drawing. rectangle (image. width-copyImage. width, image. height-copyImage. height, copyImage. width, copyImage. height), 0, 0, copyImage. width, copyImage. height, System. drawing. graphicsUnit. pixel );
G. Dispose ();
Image. Save (Path_syp );
Image. Dispose ();
}
Protected void Button2_Click (object sender, EventArgs e)
{
// Automatically save the Remote Image
WebClient client = new WebClient ();
// Standby Reg:
Regex reg = new Regex ("IMG [^>] *? Src \ s * = \ s *(? :\"(? <1> [^ \ "] *) \" | '(? <1> [^ \ '] *)') ", RegexOptions. IgnoreCase );
MatchCollection m = reg. Matches (TextBox1.Text );
Foreach (Match math in m)
{
String imgUrl = math. Groups [1]. Value;
// Add YYMMDD to the original image name and upload it
Regex regName = new Regex (@ "\ w + .(? : Jpg | gif | bmp | png) ", RegexOptions. IgnoreCase );
String strNewImgName = DateTime. Now. tow.datestring (). Replace ("-", "") + regName. Match (imgUrl). ToString ();
Try
{
// Save the image
// Client. DownloadFile (imgUrl, Server. MapPath ("../ImageUpload/Auto/" + strNewImgName ));
}
Catch
{
}
Finally
{
}
Client. Dispose ();
}
Response. Write ("<script> alert ('remote image saved successfully, saved to ImageUpload/auto') </script> ");
}
}
Front-end code:
<% @ Page Language = "C #" AutoEventWireup = "true" CodeFile = "ImagesAutoUpload. aspx. cs" Inherits = "BackManagement_ImagesAutoUpload" %>
<〈! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head id = "Head1" runat = "server">
<Title> automatically upload an image and add a text watermark </title>
</Head>
<Body>
<Form id = "form1" runat = "server" method = "post" enctype = "multipart/form-data">
<Label for = "pagebody1" style = "display: none">
</Label>
<Fieldset id = "container">
<Legend> upload an image and add a text watermark </legend>
<Div class = "window" style = "list-style: none;">
<Div style = "padding: 0px; width: 808px; height: 200px; float: left; margin-right: 0px;">
<Ul>
<Li style = "width: 150px; margin: 0px; padding: 0px; float: left;">
<Asp: Image ID = "Image1" runat = "server" Height = "200px" BorderWidth = "2px" Width = "150px"
ImageUrl = "~ /Images/Jpg/135X67/0 _11_16.gif "/>
</Li>
<Li style = "width: 250px; margin: 0px;">
<Asp: ListBox ID = "FileList" runat = "server" Width = "250px" Height = "200px"> </asp: ListBox>
</Li>
<Li style = "width: 400px; margin: 0px; float: right;">
(1) The image will be saved in the root directory of your website <asp: TextBox ID = "TextBox3" runat = "server"> </asp: TextBox>. You can modify it, however, we recommend that you use the default directory. <Br/>
(2) upload an image to the subdirectory/images of the Created directory. The thumbnail is under/thumbnails, the text watermark is under/textImages, and the image watermark is under/waterImages. <Br/>
(3) The default width and height of the generated thumbnails are PX. You can modify them as follows: <asp: textBox ID = "TextBox4" runat = "server" Width = "54px"> </asp: TextBox> the height is: <asp: TextBox
ID = "TextBox5" runat = "server" Width = "56px"> </asp: TextBox> <br/>
<Asp: CheckBox ID = "CheckBox1" runat = "server" Text = "Text watermark"/>
<Asp: CheckBox ID = "CheckBox2" runat = "server" Text = "graphic watermark"/>
You can choose whether to generate a watermark image, which is not generated by default. </Li>
</Ul>
</Div>
</Div>
<Div>
<Asp: FileUpload ID = "FindFile" runat = "server" Width = "529px"/>
</Div>
<Div>
<Asp: TextBox ID = "TipInfo" runat = "server" Width = "400px"> </asp: TextBox>
<Asp: TextBox ID = "TextBox1" runat = "server" Width = "400px"> </asp: TextBox>
</Div>
<Div>
<Asp: Button ID = "Upload" runat = "server" Text = "Upload" Style = "height: 26px" OnClick = "Upload_Click"/>
<Asp: Button ID = "AddFile" runat = "server" Text = "" OnClick = "AddFile_Click1"/>
<Asp: Button ID = "AddAllFile" runat = "server" Text = "add all" OnClick = "AddAllFile_Click"
Enabled = "False"/>
<Asp: Button ID = "DelFile" runat = "server" Text = "delete" OnClick = "DelFile_Click1"/>
<Asp: Button ID = "btnExit" runat = "server" Text = ""/>
</Div>
<Div>
</Div>
</Fieldset>
</Form>
</Body>
</Html>
In addition, to solve the restrictions on uploading large files, you must add the following code in Web. config.
<System. web>
<HttpRuntime executionTimeout = "90" maxRequestLength = "20000" useFullyQualifiedRedirectUrl = "false" requestLengthDiskThreshold = "8192"/>
</System. web>