代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
Asp.Net
】
批量上传图片并自动生成缩略图,文字水印图,图片水印图
作者:
Dezai.CN
/ 发布于
2011/9/13
/
666
本程序主要功能有:(1)可以根据自己的需要更改上传到服务器上的目录,上传的源图、缩略图、文字水印图和图片水印图分别存入所定目录下的不同目录;(2)自动检查目录,如无所选择的目录,则自动创建它们; (3)自行设定生成缩略图的大小;(4)可以选择是否需要生成文字水印、图片水印,默认为不生成水印图; (5)可以添加、删除所需上传的图片。
<div><span style="font-size: 16px">using System;</span> <span style="font-size: 16px">using System.Collections;</span> <span style="font-size: 16px">using System.Configuration;</span> <span style="font-size: 16px">using System.Data;</span> <span style="font-size: 16px">using System.Linq;</span> <span style="font-size: 16px">using System.Web;</span> <span style="font-size: 16px">using System.Web.Security;</span> <span style="font-size: 16px">using System.Web.UI;</span> <span style="font-size: 16px">using System.Web.UI.HtmlControls;</span> <span style="font-size: 16px">using System.Web.UI.WebControls;</span> <span style="font-size: 16px">using System.Web.UI.WebControls.WebParts;</span> <span style="font-size: 16px">using System.Xml.Linq;</span> <span style="font-size: 16px">using System.IO;</span> <span style="font-size: 16px">using System.Net;</span> <span style="font-size: 16px">using System.Text.RegularExpressions;</span> <span style="font-size: 16px">/// 〈summary></span> <span style="font-size: 16px">/// FileUpload1.HasFile 如果是true,则表示该控件有文件要上传</span> <span style="font-size: 16px">/// FileUpload1.FileName 返回要上传文件的名称,不包含路径信息</span> <span style="font-size: 16px">/// FileUpload1.FileContent 返回一个指向上传文件的流对象</span> <span style="font-size: 16px">/// FileUpload1.PostedFile 返回已经上传文件的引用</span> <span style="font-size: 16px">/// FileUpload1.PostedFile.ContentLength 返回上传文件的按字节表示的文件大小</span> <span style="font-size: 16px">/// FileUpload1.PostedFile.ContentType 返回上传文件的MIME内容类型,也就是文件类型,如返回"image/jpg"</span> <span style="font-size: 16px">/// FileUpload1.PostedFile.FileName 返回文件在客户端的完全路径(包括文件名全称)</span> <span style="font-size: 16px">/// FileUpload1.PostedFile.InputStream 返回一个指向上传文件的流对象</span> <span style="font-size: 16px">/// FileInfo对象表示磁盘或网络位置上的文件。提供文件的路径,就可以创建一个FileInfo对象:</span> <span style="font-size: 16px">/// 〈/summary></span> <span style="font-size: 16px">public partial class BackManagement_ImagesUpload : System.Web.UI.Page</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">public string treePath = "" ;</span> <span style="font-size: 16px">public int imageW = 100;</span> <span style="font-size: 16px">public int imageH = 100;</span> <span style="font-size: 16px">protected void Page_Load( object sender, EventArgs e)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">this .Button5.Attributes.Add( "Onclick" , "window.close();" ); //在本地关闭当前页,而不需要发送到服务器去关闭当前页时</span> <span style="font-size: 16px">if (!Page.IsPostBack)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">Label2.Text = Server.MapPath( "/" );</span> <span style="font-size: 16px">TextBox3.Text = "ImageUpload" ;</span> <span style="font-size: 16px">treePath = Server.MapPath( "/" ) + TextBox3.Text.Trim() + "/" ;</span> <span style="font-size: 16px">TextBox4.Text = imageW.ToString();</span> <span style="font-size: 16px">TextBox5.Text = imageH.ToString();</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">protected void btnload_Click( object sender, EventArgs e)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">//如果保存图片的目录不存在,由创建它</span> <span style="font-size: 16px">treePath = Server.MapPath( "/" ) + TextBox3.Text.Trim() + "/" ;</span> <span style="font-size: 16px">imageW = Convert.ToInt32(TextBox4.Text.ToString());</span> <span style="font-size: 16px">imageH = Convert.ToInt32(TextBox5.Text.ToString());</span> <span style="font-size: 16px">if (!File.Exists(treePath + "images" )) //如果/ImageUpload/images不存在,则创建/ImageUpload/images,用于存放源图片</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">System.IO.Directory.CreateDirectory(treePath + "images" );</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">if (!File.Exists(treePath + "thumbnails" )) //如果/ImageUpload/thumbnails不存在,则创建/ImageUpload/thumbnails,用于存放缩略图片</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">System.IO.Directory.CreateDirectory(treePath + "thumbnails" );</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">if (!File.Exists(treePath + "textImages" )) //如果/ImageUpload/textImages不存在,则创建/ImageUpload/textImages,用于存文字水印图片</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">System.IO.Directory.CreateDirectory(treePath + "textImages" );</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">if (!File.Exists(treePath + "waterImages" )) //如果/ImageUpload/waterImages不存在,则创建/ImageUpload/waterImages,用于存图形水印图片</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">System.IO.Directory.CreateDirectory(treePath + "waterImages" );</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">if (FileUpload1.HasFile) //如果是true,则表示该控件有文件要上传</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">string fileContentType = FileUpload1.PostedFile.ContentType;</span> <span style="font-size: 16px">if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg" )</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">string name = FileUpload1.PostedFile.FileName; //返回文件在客户端的完全路径(包括文件名全称)</span> <span style="font-size: 16px">FileInfo file = new FileInfo(name); //FileInfo对象表示磁盘或网络位置上的文件。提供文件的路径,就可以创建一个FileInfo对象:</span> <span style="font-size: 16px">string fileName = file.Name; // 文件名称</span> <span style="font-size: 16px">string fileName_s = "x_" + file.Name; // 缩略图文件名称</span> <span style="font-size: 16px">string fileName_sy = "text_" + file.Name; // 水印图文件名称(文字)</span> <span style="font-size: 16px">string fileName_syp = "water_" + file.Name; // 水印图文件名称(图片)</span> <span style="font-size: 16px">string webFilePath = treePath + "images/" + fileName; // 服务器端文件路径</span> <span style="font-size: 16px">string webFilePath_s = treePath + "thumbnails/" + fileName_s; // 服务器端缩略图路径</span> <span style="font-size: 16px">string webFilePath_sy = treePath + "textImages/" + fileName_sy; // 服务器端带水印图路径(文字)</span> <span style="font-size: 16px">string webFilePath_syp = treePath + "waterImages/" + fileName_syp; // 服务器端带水印图路径(图片)</span> <span style="font-size: 16px">string webFilePath_sypf = Server.MapPath( "../images/tzwhx.png" ); // 服务器端水印图路径(图片)</span> <span style="font-size: 16px">if (!File.Exists(webFilePath))</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">try</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件</span> <span style="font-size: 16px">if (CheckBox1.Checked) //是否生成文字水印图</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">AddWater(webFilePath, webFilePath_sy);</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">if (CheckBox2.Checked) //是否生成图形水印图</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf);</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">MakeThumbnail(webFilePath, webFilePath_s, imageW, imageH, "Cut" ); // 生成缩略图方法</span> <span style="font-size: 16px">Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B" ;</span> <span style="font-size: 16px">Image1.ImageUrl = "/" + TextBox3.Text.ToString() + "/images/" + fileName;</span> <span style="font-size: 16px">TextBox1.Text = webFilePath;</span> <span style="font-size: 16px">TextBox2.Text = "/" + TextBox3.Text.ToString() + "/images/" + fileName;</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">catch (Exception ex)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message;</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">else</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">Label1.Text = "提示:文件已经存在,请重命名后上传" ;</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">else</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">Label1.Text = "提示:文件类型不符" ;</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">/**/</span> <span style="font-size: 16px">/// 〈summary></span> <span style="font-size: 16px">/// 生成缩略图</span> <span style="font-size: 16px">/// 〈/summary></span> <span style="font-size: 16px">/// 〈param name="originalImagePath">源图路径(物理路径)〈/param></span> <span style="font-size: 16px">/// 〈param name="thumbnailPath">缩略图路径(物理路径)〈/param></span> <span style="font-size: 16px">/// 〈param name="width">缩略图宽度〈/param></span> <span style="font-size: 16px">/// 〈param name="height">缩略图高度〈/param></span> <span style="font-size: 16px">/// 〈param name="mode">生成缩略图的方式〈/param> </span> <span style="font-size: 16px">public static void MakeThumbnail( string originalImagePath, string thumbnailPath, int width, int height, string mode)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath);</span> <span style="font-size: 16px">int towidth = width;</span> <span style="font-size: 16px">int toheight = height;</span> <span style="font-size: 16px">int x = 0;</span> <span style="font-size: 16px">int y = 0;</span> <span style="font-size: 16px">int ow = originalImage.Width;</span> <span style="font-size: 16px">int oh = originalImage.Height;</span> <span style="font-size: 16px">switch (mode)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">case "HW" : //指定高宽缩放(可能变形) </span> <span style="font-size: 16px">break ;</span> <span style="font-size: 16px">case "W" : //指定宽,高按比例 </span> <span style="font-size: 16px">toheight = originalImage.Height * width / originalImage.Width;</span> <span style="font-size: 16px">break ;</span> <span style="font-size: 16px">case "H" : //指定高,宽按比例</span> <span style="font-size: 16px">towidth = originalImage.Width * height / originalImage.Height;</span> <span style="font-size: 16px">break ;</span> <span style="font-size: 16px">case "Cut" : //指定高宽裁减(不变形) </span> <span style="font-size: 16px">if (( double )originalImage.Width / ( double )originalImage.Height > ( double )towidth / ( double )toheight)</span> <span style="font-size: 16px">{</span> <p class="alt"><span style="font-size: 16px">oh = originalImage.Height;</span> <span style="font-size: 16px">ow = originalImage.Height * towidth / toheight;</span> <p class="alt"><span style="font-size: 16px"> y = 0;</span> <span style="font-size: 16px">x = (originalImage.Width - ow) / 2;</span> <p class="alt"><span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">else</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">ow = originalImage.Width;</span> <span style="font-size: 16px">oh = originalImage.Width * height / towidth;</span> <span style="font-size: 16px">x = 0;</span> <span style="font-size: 16px">y = (originalImage.Height - oh) / 2;</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">break ;</span> <span style="font-size: 16px">default :</span> <span style="font-size: 16px">break ;</span> <span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">//新建一个bmp图片</span> <span style="font-size: 16px">System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);</span> <p class="alt"><span style="font-size: 16px">//新建一个画板</span> <span style="font-size: 16px">System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);</span> <p class="alt"><span style="font-size: 16px">//设置高质量插值法</span> <span style="font-size: 16px">g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;</span> <p class="alt"><span style="font-size: 16px">//设置高质量,低速度呈现平滑程度</span> <span style="font-size: 16px">g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;</span> <p class="alt"><span style="font-size: 16px">//清空画布并以透明背景色填充</span> <span style="font-size: 16px">g.Clear(System.Drawing.Color.Transparent);</span> <p class="alt"><span style="font-size: 16px">//在指定位置并且按指定大小绘制原图片的指定部分</span> <span style="font-size: 16px">g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),</span> <span style="font-size: 16px">new System.Drawing.Rectangle(x, y, ow, oh),</span> <span style="font-size: 16px">System.Drawing.GraphicsUnit.Pixel);</span> <p class="alt"><span style="font-size: 16px">try</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">//以jpg格式保存缩略图</span> <span style="font-size: 16px">bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">catch (System.Exception e)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">throw e;</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">finally</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">originalImage.Dispose();</span> <span style="font-size: 16px">bitmap.Dispose();</span> <span style="font-size: 16px">g.Dispose();</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">/**/</span> <span style="font-size: 16px">/// 〈summary></span> <span style="font-size: 16px">/// 在图片上增加文字水印</span> <span style="font-size: 16px">/// 〈/summary></span> <span style="font-size: 16px">/// 〈param name="Path">原服务器图片路径〈/param></span> <span style="font-size: 16px">/// 〈param name="Path_sy">生成的带文字水印的图片路径〈/param></span> <span style="font-size: 16px">protected void AddWater( string Path, string Path_sy)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">string addText = "</span><a href="http://www.tzwhx.com/"><span style="font-size: 16px">www.tzwhx.com</span></a><span style="font-size: 16px">" ;</span> <span style="font-size: 16px">System.Drawing.Image image = System.Drawing.Image.FromFile(Path);</span> <span style="font-size: 16px">System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);</span> <span style="font-size: 16px">g.DrawImage(image, 0, 0, image.Width, image.Height);</span> <span style="font-size: 16px">System.Drawing.Font f = new System.Drawing.Font( "Verdana" , 10); //字体位置为左空10</span> <span style="font-size: 16px">System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green);</span> <p class="alt"><span style="font-size: 16px">g.DrawString(addText, f, b, 14, 14); //字体大小为14X14</span> <span style="font-size: 16px">g.Dispose();</span> <p class="alt"><span style="font-size: 16px">image.Save(Path_sy);</span> <span style="font-size: 16px">image.Dispose();</span> <span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">/**/</span> <span style="font-size: 16px">/// 〈summary></span> <span style="font-size: 16px">/// 在图片上生成图片水印</span> <span style="font-size: 16px">/// 〈/summary></span> <span style="font-size: 16px">/// 〈param name="Path">原服务器图片路径〈/param></span> <span style="font-size: 16px">/// 〈param name="Path_syp">生成的带图片水印的图片路径〈/param></span> <span style="font-size: 16px">/// 〈param name="Path_sypf">水印图片路径〈/param></span> <span style="font-size: 16px">protected void AddWaterPic( string Path, string Path_syp, string Path_sypf)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">System.Drawing.Image image = System.Drawing.Image.FromFile(Path);</span> <span style="font-size: 16px">System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf);</span> <span style="font-size: 16px">System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image);</span> <span style="font-size: 16px">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);</span> <span style="font-size: 16px">g.Dispose();</span> <p class="alt"><span style="font-size: 16px">image.Save(Path_syp);</span> <span style="font-size: 16px">image.Dispose();</span> <span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">protected void Button2_Click( object sender, EventArgs e)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">//自动保存远程图片</span> <span style="font-size: 16px">WebClient client = new WebClient();</span> <span style="font-size: 16px">//备用Reg:〈img.*?src=([\"\'])(http:\/\/.+\.(jpg|gif|bmp|bnp))\1.*?></span> <span style="font-size: 16px">Regex reg = new Regex( "IMG[^>]*?src\\s*=\\s*(?:\"(?〈1>[^\"]*)\"|'(?〈1>[^\']*)')" , RegexOptions.IgnoreCase);</span> <span style="font-size: 16px">MatchCollection m = reg.Matches(TextBox1.Text);</span> <p class="alt"><span style="font-size: 16px">foreach (Match math in m)</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">string imgUrl = math.Groups[1].Value;</span> <p class="alt"><span style="font-size: 16px">//在原图片名称前加YYMMDD重名名并上传</span> <p class="alt"><span style="font-size: 16px">Regex regName = new Regex(@ "\w+.(?:jpg|gif|bmp|png)" , RegexOptions.IgnoreCase);</span> <p class="alt"><span style="font-size: 16px">string strNewImgName = DateTime.Now.ToShortDateString().Replace( "-" , "" ) + regName.Match(imgUrl).ToString();</span> <p class="alt"><span style="font-size: 16px">try</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">//保存图片</span> <span style="font-size: 16px">//client.DownloadFile(imgUrl, Server.MapPath("../ImageUpload/Auto/" + strNewImgName));</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">catch</span> <span style="font-size: 16px">{</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">finally</span> <span style="font-size: 16px">{</span> <p class="alt"><span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">client.Dispose();</span> <span style="font-size: 16px">}</span> <p class="alt"><span style="font-size: 16px">Response.Write( "〈script>alert('远程图片保存成功,保存路径为ImageUpload/auto')〈/script>" );</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">}</span> <span style="font-size: 16px">另外,为解决大文件上传的限制,你必须在Web.config中加入以下代码。</span> <p class="alt"><span style="font-size: 16px">< system.web></span> <span style="font-size: 16px">< httpRuntime executionTimeout= "90" maxRequestLength= "20000" useFullyQualifiedRedirectUrl= "false" requestLengthDiskThreshold= "8192" /></span> <span style="font-size: 16px">< /system.web></span> </div>
试试其它关键字
批量上传图片
同语言下
.
gzip压缩
.
实现http多线程断点续传下载文件
.
实现多线程断点续传下载大文件
.
生成字符串的 CheckSum
.
根据 UserAgent 获取浏览器的类型和版本
.
根据 Agent 判断是否是智能手机
.
隐藏手机号中间四位为*方法
.
合并图片(二维码和其他图片合并)
.
ASP.NET CORE中判断是否移动端打开网页
.
ASP.NET(C#)实现页面计时(定时)自动跳转
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3