代码下载:http://pan.baidu.com/share/link?shareid=393581&uk=3627406265
1、图片上传:在UploadFile.htm选择文件,以method="post" enctype="multipart/form-data"提交,会将图片传递到一般处理程序UploadFile.ashx,后台用HttpPostedFile file=context.Request.Files["img"];接收,并根据扩展名,设置文件保存路径(项目用添加UploadFiles文件夹),用file.SaveAs(FileFullPath);保存文件。
UploadFile.htm:
前台用Jquery对扩展名进行判断:
<script type="text/javascript"> $(function () { //前台处理图片安全性 $('#btn').click(function () { var str = $('#img').val(); //获取文件扩展名 var ext = str.substr(str.lastIndexOf('.')); //如果扩展名是.jpg、png时才提交表单 return ext == '.jpg' || ext == '.png'; }); }); </script>
body:
<!--上传文件一定是Post enctype="multipart/form-data" 将数据上传的时候进行分割--> <form action="UploadFile.ashx" method="post" enctype="multipart/form-data"> <table> <tr><td><input type="file" name="img" id="img"/></td></tr> <tr><td><input type="submit" value="上传" id="btn"/></td></tr> </table> </form>
UploadFile.ashx:
context.Response.ContentType = "text/html"; //获取请求报文传递过来的文件 HttpPostedFile file=context.Request.Files["img"]; //获取文件扩展名 string ext = Path.GetExtension(file.FileName); //如果是图片文件则执行存储操作 if (ext==".jpg" || ext==".png") { //为保证每次存储的文件名不一样,避免重名,使用guid对文件名进行处理 string clientFileName = Guid.NewGuid().ToString() + file.FileName; string FileFullPath = context.Request.MapPath("/UploadFiles/" + clientFileName); file.SaveAs(FileFullPath);}
这样子,图片就会保存到UploadFiles下。
2、提交后,我们可以读取保存好的文件,对其进行水印处理,放到内存流中,并以BinaryWrite方式输出,注意改变报文的输出类型:context.Response.ContentType = "image/jpeg";
#region 加水印显示刚上传的图片 Bitmap bitmap = new Bitmap(FileFullPath);//把图片转成一个位图图片对象。 Graphics g = Graphics.FromImage(bitmap); //往图片写文字 g.DrawString("水印", new Font("黑体", 20), new HatchBrush(HatchStyle.Cross, Color.Blue), 20, 20); MemoryStream ms = new MemoryStream(); //将图片以jpeg格式保存到内存流中 bitmap.Save(ms, ImageFormat.Jpeg); context.Response.BinaryWrite(ms.ToArray()); #endregion
3、生成简单缩略图:很多时候,我们需要把图片保存为缩略图。
#region 简单缩略图 Bitmap bitmapMin1 = new Bitmap(40, 40);//放最终那个缩略图 Graphics gMin = Graphics.FromImage(bitmapMin1); //把原始图片拿到 System.Drawing.Image image = System.Drawing.Image.FromFile(FileFullPath); //将原始图片画到位图也就是缩略图上去 gMin.DrawImage(image, 0, 0, 40, 40); string smallPic = "/UploadFiles/min_" + clientFileName; //保存缩略图 bitmapMin1.Save(context.Request.MapPath(smallPic), ImageFormat.Jpeg); StringBuilder sb = new StringBuilder(); sb.AppendFormat("<html><head><title>显示缩略图</title></head>"); sb.AppendFormat("<body><img src='{0}'/></body></html>", smallPic); context.Response.Write(sb.ToString()); #endregion
效果图:
4、这样生成的缩略图非常简单。
博客园有写好的缩略图生成类,地址是http://www.cnblogs.com/BoKeRen/
1 #region 生成缩略图 2 ///<summary> 3 /// 生成缩略图 4 /// </summary> 5 /// <param name="originalImagePath">源图路径(物理路径)</param> 6 /// <param name="thumbnailPath">缩略图路径(物理路径)</param> 7 /// <param name="width">缩略图宽度</param> 8 /// <param name="height">缩略图高度</param> 9 /// <param name="mode">生成缩略图的方式</param> 10 public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode out string outthumbnailPath) 11 { 12 System.Drawing.Image originalImage =System.Drawing.Image.FromFile(originalImagePath); 13 int towidth = width; 14 int toheight = height; 15 int x = 0; 16 int y = 0; 17 int ow = originalImage.Width; 18 int oh = originalImage.Height; 19 20 switch (mode) 21 { 22 case "HW"://指定高宽缩放(可能变形) 23 break; 24 case "W"://指定宽,高按比例 25 toheight = originalImage.Height * width/originalImage.Width; 26 break; 27 case "H"://指定高,宽按比例 28 towidth = originalImage.Width * height/originalImage.Height; 29 break; 30 case "Cut"://指定高宽裁减(不变形) 31 if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight) 32 { 33 oh = originalImage.Height; 34 ow = originalImage.Height*towidth/toheight; 35 y = 0; 36 x = (originalImage.Width - ow)/2; 37 } 38 else 39 { 40 ow = originalImage.Width; 41 oh = originalImage.Width*height/towidth; 42 x = 0; 43 y = (originalImage.Height - oh)/2; 44 } 45 break; 46 default : 47 break; 48 } 49 //新建一个bmp图片 50 System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth,toheight); 51 52 //新建一个画板 53 Graphics g = System.Drawing.Graphics.FromImage(bitmap); 54 55 //设置高质量插值法 56 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; 57 58 //设置高质量,低速度呈现平滑程度 59 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; 60 61 //清空画布并以透明背景色填充 62 g.Clear(Color.Transparent); 63 64 //在指定位置并且按指定大小绘制原图片的指定部分 65 g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight), 66 new Rectangle(x, y, ow,oh), 67 GraphicsUnit.Pixel); 68 69 try 70 { 71 //以jpg格式保存缩略图 72 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); 73 outthumbnailPath=thumbnailPath; 74 } 75 catch(System.Exception e) 76 { 77 throw e; 78 } 79 finally 80 { 81 originalImage.Dispose(); 82 bitmap.Dispose(); 83 g.Dispose(); 84 } 85 }
#region 用网上下载的类生成较高级的缩略图 string smallPic2 = "/UploadFiles/min_" + clientFileName; string smallPicFullPath = context.Request.MapPath(smallPic2); MakeThumbnail(FileFullPath, smallPicFullPath, 100, 100, "W"); StringBuilder sb2 = new StringBuilder(); sb2.Append("<html><head></head><body>"); sb2.AppendFormat("<img src='{0}' />", smallPic2); sb2.Append("</body></html>"); context.Response.Write(sb2.ToString()); #endregion