标签:des style blog http color os io 文件
生成缩略图核心代码:
/// <summary> /// 图片上传 生成缩略图 /// </summary> /// <param name="files">文件上传控件</param> /// <param name="path">文件夹名称</param> /// <param name="fname">文件名称</param> /// <param name="w">缩略图宽度</param> /// <param name="h">缩略图高度</param> public static void UploadImageThumbs(HttpPostedFile files, string filePath, string fname, int w, int h) { if (UtilsFile.IsOrNoFileUp(files) && fname.Equals(String.Empty) == false) { UtilsFile.DirectorysCreate(filePath); Image image = Image.FromStream(files.InputStream, true); //等比例缩放 if (w > 0 && h > 0) { if (image.Width > image.Height) { if (image.Width > w) h = (int)(image.Height * ((decimal)w / image.Width)); else { h = image.Height; w = image.Width; } } else { if (image.Height > h) w = (int)(image.Width * ((decimal)h / image.Height)); else { h = image.Height; w = image.Width; } } } else if (w > 0 && h == 0) { if (image.Width < w) w = image.Width; h = (int)(image.Height * ((decimal)w / image.Width)); } else if (w == 0 && h > 0) { if (image.Height < h) h = image.Height; w = (int)(image.Width * ((decimal)h / image.Height)); } else { w = image.Width; h = image.Height; } Bitmap ret = new Bitmap(w, h); using (Graphics g = Graphics.FromImage(ret)) { g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.FillRectangle(Brushes.White, 0, 0, w, h); g.DrawImage(image, 0, 0, w, h); EncoderParameters parms = new EncoderParameters(); long[] quality = new long[1]; quality[0] = 80; EncoderParameter parm = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); parms.Param[0] = parm; ImageCodecInfo[] arr = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ar = null; for (int x = 0; x < arr.Length; x++) { if (arr[x].FormatDescription.Equals("JPEG")) { ar = arr[x]; break; } } ret.Save(filePath + fname, ar, parms); ret.Dispose(); image.Dispose(); } } }
总结:以上生成缩略图代码,效果是比较好的,就是有时候红色的点会失真,比较无解。不过这问题基本可以忽略不计。较真的朋友,如有较好的生成代码,可以共享。
原文转载: http://www.dyxue.com/tech/id2498.html
Asp.Net 高清图片缩略图生成,布布扣,bubuko.com
标签:des style blog http color os io 文件
原文地址:http://www.cnblogs.com/btnan/p/3928250.html