因本网站上传图片的需要,参考很多成熟的经验,在ASP.net平台上使用C#语言,做了这一自动批量上传图片的.ASPX文件,并经调试成功,在本网站上使用,现发出来供大家参考,也希望高手多加指点。
本程序主要功能有:
(1)可以根据自己的需要更改上传到服务器上的目录,上传的源图、缩略图、文字水印图和图片水印图分别存入所定目录下的不同目录;
(2)自动检查目录,如无所选择的目录,则自动创建它们;
(3)自行设定生成缩略图的大小;
(4)可以选择是否需要生成文字水印、图片水印,默认为不生成水印图;
(5)可以添加、删除所需上传的图片。
在本程序中均加了相关注释,所以直接发代码,不再多作解释。
后台程序:
- 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;
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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();");
- 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)
- {
-
- treePath = Server.MapPath("/") + TextBox3.Text.Trim() + "/";
- imageW = Convert.ToInt32(TextBox4.Text.ToString());
- imageH = Convert.ToInt32(TextBox5.Text.ToString());
- if (!File.Exists(treePath + "images"))
- {
- System.IO.Directory.CreateDirectory(treePath + "images");
- }
- if (!File.Exists(treePath + "thumbnails"))
- {
- System.IO.Directory.CreateDirectory(treePath + "thumbnails");
- }
- if (!File.Exists(treePath + "textImages"))
- {
- System.IO.Directory.CreateDirectory(treePath + "textImages");
- }
- if (!File.Exists(treePath + "waterImages"))
- {
- System.IO.Directory.CreateDirectory(treePath + "waterImages");
- }
-
- if (FileUpload1.HasFile)
- {
- string fileContentType = FileUpload1.PostedFile.ContentType;
- if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
- {
- string name = FileUpload1.PostedFile.FileName;
-
- FileInfo file = new FileInfo(name);
- string fileName = file.Name;
- string fileName_s = "x_" + file.Name;
- string fileName_sy = "text_" + file.Name;
- string fileName_syp = "water_" + file.Name;
-
- string webFilePath = treePath + "images/" + fileName;
- string webFilePath_s = treePath + "thumbnails/" + fileName_s;
- string webFilePath_sy = treePath + "textImages/" + fileName_sy;
- string webFilePath_syp = treePath + "waterImages/" + fileName_syp;
- string webFilePath_sypf = Server.MapPath("../images/tzwhx.png");
-
- if (!File.Exists(webFilePath))
- {
- try
- {
- FileUpload1.SaveAs(webFilePath);
- if (CheckBox1.Checked)
- {
- AddWater(webFilePath, webFilePath_sy);
- }
- if (CheckBox2.Checked)
- {
- AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);
- }
- MakeThumbnail(webFilePath, webFilePath_s, imageW, imageH, "Cut");
- Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + 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 = "提示:文件上传失败,失败原因:" + ex.Message;
- }
- }
- else
- {
- Label1.Text = "提示:文件已经存在,请重命名后上传";
- }
- }
- else
- {
- Label1.Text = "提示:文件类型不符";
- }
- }
- }
-
-
-
-
-
-
-
-
-
- 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":
- break;
- case "W":
- toheight = originalImage.Height * width / originalImage.Width;
- break;
- case "H":
- towidth = originalImage.Width * height / originalImage.Height;
- break;
- case "Cut":
- 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;
- }
-
-
- System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
-
-
- System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
-
-
- g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
-
-
- g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
-
-
- g.Clear(System.Drawing.Color.Transparent);
-
-
- g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
- new System.Drawing.Rectangle(x, y, ow, oh),
- System.Drawing.GraphicsUnit.Pixel);
-
- try
- {
-
- bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
- }
- catch (System.Exception e)
- {
- throw e;
- }
- finally
- {
- originalImage.Dispose();
- bitmap.Dispose();
- g.Dispose();
- }
- }
-
-
-
-
-
-
-
- protected void AddWater(string Path, string Path_sy)
- {
- string addText = "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);
- System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);
-
- g.DrawString(addText, f, b, 14, 14);
- g.Dispose();
-
- image.Save(Path_sy);
- image.Dispose();
- }
-
-
-
-
-
-
-
-
- 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)
- {
-
- WebClient client = new WebClient();
-
- 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;
-
-
-
- Regex regName = new Regex(@"\w+.(?:jpg|gif|bmp|png)", RegexOptions.IgnoreCase);
-
- string strNewImgName = DateTime.Now.ToShortDateString().Replace("-", "") + regName.Match(imgUrl).ToString();
-
- try
- {
-
-
-
- }
- catch
- {
- }
- finally
- {
- }
- client.Dispose();
- }
- Response.Write("<mce:script type="text/javascript"><!--
- alert('远程图片保存成功,保存路径为ImageUpload/auto')
-
- }
- }
前台代码:
- <%@ 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>自动上传图片并加文字水印</title>
- </head>
- <body>
- <form id="form1" runat="server" method="post" enctype="multipart/form-data">
- <label for="pagebody1" style="display: none" mce_style="display: none">
- </label>
- <fieldset id="container">
- <legend>上传图片并加文字水印</legend>
- <div class="window" style="list-style: none;" mce_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" Heig, ht="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)图片将保存在你网站根目录<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>中,你可以修改它,但建议你使用默认目录。<br />
- (2)上传图片保存在所建目录的子目录 /images下;缩略图在 /thumbnails下;文字水印图在 /textImages下;图形水印图在 /waterImages目录下。<br />
- (3)生成的缩略图的默认宽度和高度均为100px,你也可以修改它们,宽为: <asp:TextBox ID="TextBox4" runat="server" Width="54px"></asp:TextBox>高为:<asp:TextBox
- ID="TextBox5" runat="server" Width="56px"></asp:TextBox><br />
- <asp:CheckBox ID="CheckBox1" runat="server" Text="文字水印" />
- <asp:CheckBox ID="CheckBox2" runat="server" Text="图形水印" />
- 你可以选择是否生成水印图,默认不生成。 </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="上 传" Style="height: 26px" mce_Style="height: 26px" OnClick="Upload_Click" />
- <asp:Button ID="AddFile" runat="server" Text="添 加" OnClick="AddFile_Click1" />
- <asp:Button ID="AddAllFile" runat="server" Text="全部添加" OnClick="AddAllFile_Click"
- Enabled="False" />
- <asp:Button ID="DelFile" runat="server" Text="删 除" OnClick="DelFile_Click1" />
- <asp:Button ID="btnExit" runat="server" Text="完 成" />
- </div>
- <div>
-
- </div>
- </fieldset>
- </form>
- </body>
- </html>
另外,为解决大文件上传的限制,你必须在Web.config中加入以下代码。
- <system.web>
- <httpRuntime executionTimeout="90" maxRequestLength="20000" useFullyQualifiedRedirectUrl="false" requestLengthDiskThreshold="8192"/>
- </system.web>
文章出处:http://www.diybl.com/course/4_webprogram/asp.net/asp_netxl/2008106/148796_3.html