一个实用的文件上传类and生成图片缩略图(只提供上传)
2011-11-18 10:47
936 查看
///<summary> /// 文件上传类 ///</summary> public class UploadFile { ///<summary> /// 上传文件公共方法 ///</summary> ///<param name="PostedFile">PostedFile</param> ///<param name="sPath">示例(格式)为:D:/Upload/</param> ///<param name="sExtension">示例(格式)为:.jpg|.gif|.bmp|.png</param> ///<param name="iSize">上传的文件不能超过的大小(单位为KB)</iSize> ///<param name="flage">是否合格</param> ///<returns></returns> public static string UploadFileImg(HttpPostedFile PostedFile, string sPath, string sExtension, int iSize, out bool flage) { string fileName = null; flage = true; if (PostedFile != null) { fileName = System.IO.Path.GetFileName(PostedFile.FileName); if (fileName != "") { flage = false; //获得图片的后缀名 string imgExtension = System.IO.Path.GetExtension(fileName); //判断是否是图片 if (sExtension.ToLower().IndexOf(imgExtension.ToLower()) == -1) { Csharp5K.WebLib.MessageBox.Show("只能上传格式为[" + sExtension + "]的图片文件"); } else if (PostedFile.ContentLength / 1024 > iSize) // 上传文件不能大于 [iSize] KB { Csharp5K.WebLib.MessageBox.Show("上传图片不能超过" + iSize + "KB"); } else { flage = true; fileName = Guid.NewGuid().ToString("N") + imgExtension; //如果 sPath 中存在“:”则为物理路径 if (sPath.IndexOf(":") != -1) PostedFile.SaveAs(sPath + fileName); else PostedFile.SaveAs(HttpContext.Current.Server.MapPath(sPath + fileName)); } } } return fileName; } }
图片缩略图and简单水印方法
View Code
///<summary> /// 图形操作类 ///</summary> public class Drawing { #region 图片处理 ///<summary> /// 生成缩略图 ///</summary> ///<param name="originalImagePath">源图路径(物理路径)</param> ///<param name="thumbnailPath">缩略图路径(物理路径)</param> ///<param name="width">缩略图宽度</param> ///<param name="height">缩略图高度</param> ///<param name="mode">生成缩略图的方式【HW:指定高宽缩放(可能变形);W:指定宽,高按比例;H:指定高,宽按比例;Cut:指定高宽裁减(不变形)】</param> public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode) { //判断源图片是否存在,不存在就不执行 if (! File.Exists(originalImagePath)) return; //缩略图 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.ToUpper()) { 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; } //新建一个bmp图片 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 { //以jpg格式保存缩略图 bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg); } catch (System.Exception e) { throw e; } finally { originalImage.Dispose(); bitmap.Dispose(); g.Dispose(); } } ///<summary> /// 在图片上增加文字水印,好像只能是JPG格式才可能加水印呢 ///</summary> ///<param name="strText">文字</param> ///<param name="sPath">原服务器图片路径</param> ///<param name="sPath_sy">生成的带文字水印的图片路径</param> ///<param name="iSize">大小</param> public static void AddWater(string strText,string sPath, string sPath_sy,int iSize) { if (Path.GetExtension(sPath).ToUpper() != ".JPG") return; System.Drawing.Image image = System.Drawing.Image.FromFile(sPath); 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("Arial", iSize); System.Drawing.Brush b = new System.Drawing.SolidBrush(System.Drawing.Color.Green); g.DrawString(strText, f, b, 8, image.Height - 8 - iSize); g.Dispose(); image.Save(sPath_sy); image.Dispose(); } ///<summary> /// 在图片上生成图片水印 ///</summary> ///<param name="Path">原服务器图片路径</param> ///<param name="Path_syp">生成的带图片水印的图片路径</param> ///<param name="Path_sypf">水印图片路径</param> public static 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(); } #endregion }
相关文章推荐
- 赵祖辉 图片上传类;支持水印-日期文件夹-生成缩略图 ,支持多文件上传,
- 上传图片并生成缩略图的一个方法
- 一个上传图片生成缩略图的方法
- struts2上传文件、生成缩略图、添加文字和图片水印
- .net上传图片文件,生成缩略图,并写上版权信息
- C#文件上传生成缩略图及添加文字和图片水印
- DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
- 对给定的一个图片生成一个指定大小的缩略图(上传图片很大是可用)
- 上传图片文件,生成缩略图,并写上版权信息
- 上传图片文件,生成缩略图,并写上版权信息
- 图片文件上传, 生成高质量缩略图 (asp.net c#)
- dropzonejs中文翻译手册 DropzoneJS是一个提供文件拖拽上传并且提供图片预览的开源类库.
- php图片文件上传类 (附自动生成缩略图)
- struts2实现上传文件、生成缩略图、添加文字和图片水印
- 上传图片文件,生成缩略图,并写上版权信息
- .net上传图片文件,生成缩略图,并写上版权信息
- 提供下载:批量图片压缩(生成缩略图或指定大小图片)及文件批量改名器
- 简单图片文件上传并处理缩略图生成
- 使用FileUpload控件上传图片并自动生成缩略图、自动生成带文字和图片的水印图(原创)
- Net 上传图片并生成高清晰缩略图(网摘)