A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 夏华成 中级黑马   /  2012-5-19 09:45  /  2124 人查看  /  1 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

以下是本人QQ空间早期日记. 都是自己实际工作中用到的(分享一下)
  1. public static string UploadFile_wh(HttpPostedFile postedFile, string path, out string err,int w,int h)
  2. {
  3. err = "";
  4. string fileUrlPath = "", pathfilename = "";
  5. fileUrlPath = "/保存图片文件夹/" + path + "/" + DateTime.Now.ToString("yyyyMM") + "/";//生成的文件夹要保存的位置
  6. if (!Directory.Exists(System.Web.HttpContext.Current.Server.MapPath(fileUrlPath)))
  7. {
  8. Directory.CreateDirectory(System.Web.HttpContext.Current.Server.MapPath(fileUrlPath)); //创建文件夹
  9. }
  10. try
  11. {
  12. string fileName, fileExtension, FileType;
  13. int FileLength;
  14. fileName = System.IO.Path.GetFileName(postedFile.FileName).ToLower();
  15. fileExtension = System.IO.Path.GetExtension(fileName);
  16. FileType = postedFile.ContentType.ToLower();

  17. FileLength = postedFile.ContentLength;
  18. Random rd = new Random();
  19. string newName = "";
  20. string str = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(fileName.Replace(fileExtension, ""), "MD5");

  21. if (str.Length > 10)
  22. {
  23. str = str.Substring(0, 10);
  24. }
  25. if (System.IO.File.Exists(System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + str + fileExtension))
  26. {
  27. newName = str + (DateTime.Now.Date.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Millisecond + Convert.ToInt32(rd.NextDouble() * 10000)).ToString();
  28. }
  29. else
  30. {
  31. newName = str;
  32. }
  33. //string newName = (DateTime.Now.Date.ToString("yyyyMMdd") + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Millisecond + Convert.ToInt32(rd.NextDouble() * 10000)).ToString() ;
  34. string newNamex = newName + "x" + fileExtension;
  35. newName = newName + fileExtension;
  36. pathfilename = fileUrlPath + newName;
  37. if (fileName != "")
  38. {
  39. if (FileType == "image/bmp" || FileType == "image/jpg" || FileType == "image/gif" || FileType == "image/pjpeg" || FileType == "image/jpeg" || FileType == "image/x-png" || FileType == "application/x-shockwave-flash")
  40. {
  41. //if (FileLength / 1024 < 300) //上传文件不能大于300kb
  42. //{
  43. string fullfilename = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + newName;
  44. string fullfilenamex = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + newNamex;
  45. string fullfilename1="", fullfilenamex1="";
  46. postedFile.SaveAs(fullfilename);
  47. System.Drawing.Image image100, image60,image30, syImage;

  48. //三种不同尺寸的水印图(根据实际情况,定义水印图的尺寸)
  49. image100 = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("~/100.png"));
  50. image60 = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("~/60.png"));
  51. image30 = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Request.MapPath("~/30.png"));
  52. syImage = System.Drawing.Image.FromFile(fullfilename);

  53. if (syImage.Height < 50 || syImage.Width < 50)
  54. {
  55. fullfilename1 = fullfilename;
  56. fullfilenamex1 = fullfilenamex;
  57. }
  58. else if (syImage.Height < 100 || syImage.Width < 100)
  59. {
  60. fullfilename1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newName;
  61. fullfilenamex1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newNamex;
  62. Graphics g = Graphics.FromImage(syImage); // 绘制水印的 位置坐标
  63. g.DrawImage(image30, syImage.Width - image30.Width, syImage.Height - image30.Height, image30.Width, image30.Height);
  64. g.Dispose();

  65. syImage.Save(fullfilename1);
  66. syImage.Dispose();
  67. image30.Dispose();
  68. image100.Dispose();
  69. image60.Dispose();
  70. }
  71. else if (syImage.Height < 250 || syImage.Width < 250)
  72. {
  73. fullfilename1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newName;
  74. fullfilenamex1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newNamex;
  75. Graphics g = Graphics.FromImage(syImage); // 绘制水印的 位置坐标
  76. g.DrawImage(image60, syImage.Width - image60.Width, syImage.Height - image60.Height, image60.Width, image60.Height);
  77. g.Dispose();

  78. syImage.Save(fullfilename1);
  79. syImage.Dispose();
  80. image30.Dispose();
  81. image100.Dispose();
  82. image60.Dispose();
  83. }
  84. else
  85. {
  86. fullfilename1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newName;
  87. fullfilenamex1 = System.Web.HttpContext.Current.Request.MapPath(fileUrlPath) + "M" + newNamex;
  88. Graphics g = Graphics.FromImage(syImage); // 绘制水印的 位置坐标
  89. g.DrawImage(image100, syImage.Width - image100.Width, syImage.Height - image100.Height, image100.Width, image100.Height);
  90. g.Dispose();

  91. syImage.Save(fullfilename1);
  92. syImage.Dispose();
  93. image30.Dispose();
  94. image100.Dispose();
  95. image60.Dispose();
  96. }
  97. if (FileType != "application/x-shockwave-flash")
  98. {
  99. try
  100. {

  101. //此处是生成缩微图类

  102. WebCommon.ImageClass ic = new ImageClass(fullfilename1);
  103. ic.MakeThumbnail(fullfilename1, fullfilenamex1, w, h);

  104. //ic = null;
  105. }
  106. catch(Exception ex)
  107. { }
  108. if (File.Exists(fullfilename))
  109. {
  110. File.Delete(fullfilename);
  111. }
  112. if (File.Exists(fullfilenamex))
  113. {
  114. File.Delete(fullfilenamex);
  115. }
  116. File.Move(fullfilename1, fullfilename);
  117. File.Move(fullfilenamex1, fullfilenamex);
  118. }
  119. //}
  120. //else
  121. //{
  122. // err = "文件过大!";
  123. //}
  124. }
  125. else
  126. {
  127. err = "上传文件格式不正确!";
  128. }
  129. }
  130. else
  131. {
  132. err = "请选择上传文件!";
  133. }
  134. }
  135. catch (Exception e)
  136. {
  137. err = e.Message;
  138. pathfilename = "";
  139. }
  140. return pathfilename;
  141. }



  142. //生成缩微图类

  143. public class ImageClass
  144. {
  145. public System.Drawing.Image ResourceImage;
  146. private int ImageWidth;
  147. private int ImageHeight;

  148. public string ErrMessage;

  149. /// <summary>
  150. /// 类的构造函数
  151. /// </summary>
  152. /// <param name="ImageFileName">图片文件的全路径名称</param>
  153. public ImageClass(string ImageFileName)
  154. {
  155. // ResourceImage = System.Drawing.Image.FromFile(ImageFileName);
  156. ErrMessage = "";
  157. }

  158. public bool ThumbnailCallback()
  159. {
  160. return false;
  161. }

  162. public void MakeThumbnail(string originalImagePath, string thumbnailPath, int width1, int height1)
  163. {
  164. System.Drawing.Image original_image = System.Drawing.Image.FromFile(originalImagePath);



  165. // Calculate the new width and height
  166. int width = original_image.Width;
  167. int height = original_image.Height;
  168. int target_width = width1;
  169. int target_height = height1;
  170. int new_width, new_height;
  171. if (width == target_width && height == target_height)
  172. {
  173. new_height = height;
  174. new_width = width;
  175. }
  176. else
  177. {
  178. float target_ratio = (float)target_width / (float)target_height;
  179. float image_ratio = (float)width / (float)height;

  180. if (target_ratio > image_ratio)
  181. {
  182. new_height = target_height;
  183. new_width = (int)Math.Floor(image_ratio * (float)target_height);
  184. }
  185. else
  186. {
  187. new_height = (int)Math.Floor((float)target_width / image_ratio);
  188. new_width = target_width;
  189. }

  190. new_width = new_width > target_width ? target_width : new_width;
  191. new_height = new_height > target_height ? target_height : new_height;
  192. }

  193. int paste_x = 0; //(target_width - new_width) / 2;
  194. int paste_y =0; //(target_height - new_height) / 2;


  195. //新建一个bmp图片
  196. System.Drawing.Image bitmap = new System.Drawing.Bitmap(new_width, new_height);
  197. //System.Drawing.Image bitmap = System.Drawing.Image.FromFile(originalImagePath);

  198. //新建一个画板
  199. Graphics g = System.Drawing.Graphics.FromImage(bitmap);

  200. //设置高质量插值法
  201. g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;

  202. //设置高质量,低速度呈现平滑程度
  203. g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;

  204. //清空画布并以透明背景色填充
  205. g.Clear(System.Drawing.Color.Transparent);

  206. //在指定位置并且按指定大小绘制原图片的指定部分
  207. // g.DrawImage(original_image, paste_x, paste_y, new_width, new_height);
  208. g.DrawImage(original_image, 0, 0, new_width, new_height);
  209. try
  210. {
  211. //以jpg格式保存缩略图
  212. //bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
  213. bitmap.Save(thumbnailPath);
  214. }
  215. catch (System.Exception e)
  216. {
  217. throw e;
  218. }
  219. finally
  220. {
  221. original_image.Dispose();
  222. bitmap.Dispose();
  223. g.Dispose();
  224. }
  225. }

  226. }
复制代码

评分

参与人数 1技术分 +3 收起 理由
宋天琪 + 3

查看全部评分

1 个回复

倒序浏览
值得学习ing!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马