您目前尚未登陆,请选择【登陆】或【注册
首页->上传下载->图片上传(水印、缩略图、远程保存)源码>>Default.aspx.cs>>源码在线查看
温馨提示:代码在线浏览功能只能做为源码浏览参考,如果想更进一步了解该代码请下载:图片上传(水印、缩略图、远程保存)源码
当前文件:文件类型 ImageUpload/Default.aspx.cs打开代码结构图
普通视图
		            
1using System; 2using System.Data; 3using System.Configuration; 4using System.Collections; 5using System.Web; 6using System.Web.Security; 7using System.Web.UI; 8using System.Web.UI.WebControls; 9using System.Web.UI.WebControls.WebParts; 10using System.Web.UI.HtmlControls; 11using System.IO; 12using System.Net; 13using System.Text.RegularExpressions; 14 15namespace _51aspxImgUpload 16{ 17 /// <summary> 18 /// 上传图片生成缩略图及水印 (来自:http://www.51aspx.com/CV/ImageUpload) 19 /// </summary> 20 public partial class _Default : System.Web.UI.Page 21 { 22 protected void Page_Load(object sender, EventArgs e) 23 { 24 25 } 26 27 protected void Button1_Click(object sender, EventArgs e) 28 { 29 if (FileUpload1.HasFile) 30 { 31 string fileContentType = FileUpload1.PostedFile.ContentType; 32 if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg") 33 { 34 string name = FileUpload1.PostedFile.FileName; // 客户端文件路径 35 36 FileInfo file = new FileInfo(name); 37 string fileName = file.Name; // 文件名称 38 string fileName_s = "x_" + file.Name; // 缩略图文件名称 39 string fileName_sy = "text_" + file.Name; // 水印图文件名称(文字) 40 string fileName_syp = "water_" + file.Name; // 水印图文件名称(图片) 41 string webFilePath = Server.MapPath("ImgUpload/" + fileName); // 服务器端文件路径 42 string webFilePath_s = Server.MapPath("ImgUpload/" + fileName_s);  // 服务器端缩略图路径 43 string webFilePath_sy = Server.MapPath("ImgUpload/" + fileName_sy); // 服务器端带水印图路径(文字) 44 string webFilePath_syp = Server.MapPath("ImgUpload/" + fileName_syp); // 服务器端带水印图路径(图片) 45 string webFilePath_sypf = Server.MapPath("51aspx.png"); // 服务器端水印图路径(图片) 46 47 if (!File.Exists(webFilePath)) 48 { 49 try 50 { 51 FileUpload1.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件 52 AddWater(webFilePath, webFilePath_sy); 53 AddWaterPic(webFilePath, webFilePath_syp, webFilePath_sypf); 54 MakeThumbnail(webFilePath, webFilePath_s, 130, 130, "Cut"); // 生成缩略图方法 55 Label1.Text = "提示:文件“" + fileName + "”成功上传,并生成“" + fileName_s + "”缩略图,文件类型为:" + FileUpload1.PostedFile.ContentType + ",文件大小为:" + FileUpload1.PostedFile.ContentLength + "B"; 56 } 57 catch (Exception ex) 58 { 59 Label1.Text = "提示:文件上传失败,失败原因:" + ex.Message; 60 } 61 } 62 else 63 { 64 Label1.Text = "提示:文件已经存在,请重命名后上传"; 65 } 66 } 67 else 68 { 69 Label1.Text = "提示:文件类型不符"; 70 } 71 } 72 } 73 /**/ 74 /// <summary> 75 /// 生成缩略图 76 /// </summary> 77 /// <param name="originalImagePath">源图路径(物理路径)</param> 78 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 79 /// <param name="width">缩略图宽度</param> 80 /// <param name="height">缩略图高度</param> 81 /// <param name="mode">生成缩略图的方式</param> 82 public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) 83 { 84 System.Drawing.Image originalImage = System.Drawing.Image.FromFile(originalImagePath); 85 86 int towidth = width; 87 int toheight = height; 88 89 int x = 0; 90 int y = 0; 91 int ow = originalImage.Width; 92 int oh = originalImage.Height; 93 94 switch (mode) 95 { 96 case "HW"://指定高宽缩放(可能变形) 97 break; 98 case "W"://指定宽,高按比例 99 toheight = originalImage.Height * width / originalImage.Width; 100 break; 101 case "H"://指定高,宽按比例 102 towidth = originalImage.Width * height / originalImage.Height; 103 break; 104 case "Cut"://指定高宽裁减(不变形) 105 if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight) 106 { 107 oh = originalImage.Height; 108 ow = originalImage.Height * towidth / toheight; 109 y = 0; 110 x = (originalImage.Width - ow) / 2; 111 } 112 else 113 { 114 ow = originalImage.Width; 115 oh = originalImage.Width * height / towidth; 116 x = 0; 117 y = (originalImage.Height - oh) / 2; 118 } 119 break; 120 default: 121 break; 122 } 123 124 //新建一个bmp图片 125 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight); 126 127 //新建一个画板 128 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap); 129 130 //设置高质量插值法 131 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 132 133 //设置高质量,低速度呈现平滑程度 134 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 135 136 //清空画布并以透明背景色填充 137 g.Clear(System.Drawing.Color.Transparent); 138 139 //在指定位置并且按指定大小绘制原图片的指定部分 140 g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight), 141 new System.Drawing.Rectangle(x, y, ow, oh), 142 System.Drawing.GraphicsUnit.Pixel); 143 144 try 145 { 146 //以jpg格式保存缩略图 147 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 148 } 149 catch (System.Exception e) 150 { 151 throw e; 152 } 153 finally 154 { 155 originalImage.Dispose(); 156 bitmap.Dispose(); 157 g.Dispose(); 158 } 159 } 160 161 /**/ 162 /// <summary> 163 /// 在图片上增加文字水印 164 /// </summary> 165 /// <param name="Path">原服务器图片路径</param> 166 /// <param name="Path_sy">生成的带文字水印的图片路径</param> 167 protected void AddWater(string Path, string Path_sy) 168 { 169 string addText = "51aspx.com"; 170 System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 171 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); 172 g.DrawImage(image, 0, 0, image.Width, image.Height); 173 System.Drawing.Font f = new System.Drawing.Font("Verdana", 60); 174 System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green); 175 176 g.DrawString(addText, f, b, 35, 35); 177 g.Dispose(); 178 179 image.Save(Path_sy); 180 image.Dispose(); 181 } 182 183 /**/ 184 /// <summary> 185 /// 在图片上生成图片水印 186 /// </summary> 187 /// <param name="Path">原服务器图片路径</param> 188 /// <param name="Path_syp">生成的带图片水印的图片路径</param> 189 /// <param name="Path_sypf">水印图片路径</param> 190 protected void AddWaterPic(string Path, string Path_syp, string Path_sypf) 191 { 192 System.Drawing.Image image = System.Drawing.Image.FromFile(Path); 193 System.Drawing.Image copyImage = System.Drawing.Image.FromFile(Path_sypf); 194 System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(image); 195 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); 196 g.Dispose(); 197 198 image.Save(Path_syp); 199 image.Dispose(); 200 } 201 202 protected void Button2_Click(object sender, EventArgs e) 203 { 204 //自动保存远程图片 205 WebClient client = new WebClient(); 206 //备用Reg:<img.*?src=([\"\'])(http:\/\/.+\.(jpg|gif|bmp|bnp))\1.*?> 207 Regex reg = new Regex("IMG[^>]*?src\\s*=\\s*(?:\"(?<1>[^\"]*)\"|'(?<1>[^\']*)')", RegexOptions.IgnoreCase); 208 MatchCollection m = reg.Matches(TextBox1.Text); 209 210 foreach (Match math in m) 211 { 212 string imgUrl = math.Groups[1].Value; 213 214 //在原图片名称前加YYMMDD重名名并上传 215 216 Regex regName = new Regex(@"\w+.(?:jpg|gif|bmp|png)", RegexOptions.IgnoreCase); 217 218 string strNewImgName = DateTime.Now.ToShortDateString().Replace("-", "") + regName.Match(imgUrl).ToString(); 219 220 try 221 { 222 //保存图片 223 client.DownloadFile(imgUrl, Server.MapPath("ImgUpload/Auto/" + strNewImgName)); 224 225 } 226 catch 227 { 228 } 229 finally 230 { 231 232 } 233 234 client.Dispose(); 235 } 236 237 Response.Write("<script>alert('远程图片保存成功,保存路径为ImgUpload/auto')</script>"); 238 239 } 240 } 241} 242
还没有找到您心仪的内容?请用.net源码大搜捕
代码片断 打包下载该项目完整源码:图片上传(水印、缩略图、远程保存)源码
51Aspx.com 版权所有 CopyRight © 2000-2008. 京ICP备06046876号