Asp.net 快速生成清晰缩略图解决方案
做站的时候经常会遇到要生成缩略图的功能,因为可能不同的情况需要用来不同大小的缩略图。
本文生成的图片都为正方形,只有正方形的缩略图才是保证图片足够清晰。
当我我这里说的正方形 是先按比例压缩,然后加一个固定的白底 然后居中显示。
好了 话不多说 上代码:
新建outputimg.ashx
1 //调整图片大小 2 private static Size NewSize(int maxWidth, int maxHeight, int Width, int Height) 3 { 4 double w = 0.0; 5 double h = 0.0; 6 double sw = Convert.ToDouble(Width); 7 double sh = Convert.ToDouble(Height); 8 double mw = Convert.ToDouble(maxWidth); 9 double mh = Convert.ToDouble(maxHeight); 10 11 if (sw < mw && sh < mh)//如果maxWidth和maxHeight大于源图像,则缩略图的长和高不变 12 { 13 w = sw; 14 h = sh; 15 } 16 else if ((sw / sh) > (mw / mh)) 17 { 18 w = maxWidth; 19 h = (w * sh) / sw; 20 } 21 else 22 { 23 h = maxHeight; 24 w = (h * sw) / sh; 25 } 26 return new Size(Convert.ToInt32(w), Convert.ToInt32(h)); 27 }
1 //生成缩略图 2 public static void SendSmallImage(string filename, string newfile, int maxHeight, int maxWidth, string mode) 3 { 4 System.Drawing.Image img = System.Drawing.Image.FromFile(filename);//源图像的信息 5 System.Drawing.Imaging.ImageFormat thisformat = img.RawFormat; //源图像的格式 6 Size newSize = NewSize(maxWidth, maxHeight, img.Width, img.Height); //返回调整后的图像Width与Height 7 Bitmap outBmp = new Bitmap(maxWidth, maxHeight); 8 Graphics g = Graphics.FromImage(outBmp); 9 //设置画布的描绘质量 10 g.CompositingQuality = CompositingQuality.HighQuality; 11 g.SmoothingMode = SmoothingMode.HighQuality; 12 g.InterpolationMode = InterpolationMode.HighQualityBicubic; 13 g.Clear(Color.White); 14 g.DrawImage(img, new Rectangle(((maxWidth - newSize.Width) / 2), ((maxHeight - newSize.Height) / 2), newSize.Width, newSize.Height), 0, 0, img.Width, img.Height, GraphicsUnit.Pixel); 15 g.Dispose(); 16 //以下代码为保存图片时,设置压缩质量 17 EncoderParameters encoderParams = new EncoderParameters(); 18 long[] quality = new long[1]; 19 quality[0] = 100; 20 EncoderParameter encoderParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, quality); 21 encoderParams.Param[0] = encoderParam; 22 //获取包含有关内置图像编码解码器的信息的ImageCodecInfo对象。 23 ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders(); 24 ImageCodecInfo jpegICI = null; 25 for (int x = 0; x < arrayICI.Length; x++) 26 { 27 if (arrayICI[x].FormatDescription.Equals("JPEG")) 28 { 29 jpegICI = arrayICI[x];//设置jpeg编码 30 break; 31 } 32 } 33 if (jpegICI != null) 34 { 35 outBmp.Save(newfile, jpegICI, encoderParams); 36 } 37 else 38 { 39 outBmp.Save(newfile, thisformat); 40 } 41 img.Dispose(); 42 outBmp.Dispose(); 43 }
输出图片:
//输出图片 public static void OutPutImg(string imgFilePath) { FileStream fs = new FileStream(HttpContext.Current.Server.MapPath(imgFilePath), FileMode.Open, FileAccess.Read); DateTime contentModified = System.IO.File.GetLastWriteTime(HttpContext.Current.Server.MapPath(imgFilePath)); if (IsClientCached(contentModified)) { HttpContext.Current.Response.StatusCode = 304; HttpContext.Current.Response.SuppressContent = true; } else { byte[] mydata = new byte[fs.Length]; int Length = Convert.ToInt32(fs.Length); fs.Read(mydata, 0, Length); fs.Close(); HttpContext.Current.Response.OutputStream.Write(mydata, 0, Length); HttpContext.Current.Response.ContentType = "image/jpeg"; HttpContext.Current.Response.End(); HttpContext.Current.Response.Cache.SetETagFromFileDependencies(); HttpContext.Current.Response.Cache.SetAllowResponseInBrowserHistory(true); HttpContext.Current.Response.Cache.SetLastModified(contentModified); } }
//outpuimg.ashx?src=/images/weimeidesc/8af30049-797e-4eb4-8a54-cc4de47c1694.jpg!100x100.jpg public void ProcessRequest(HttpContext context) { //获取图片 string imgUrl = context.Request.QueryString["src"]; string trueFilePath = imgUrl.Split('!')[0]; //获取图片大小 int width = Convert.ToInt32(imgUrl.Split('!')[1].Replace(".jpg", "").Split('x')[0]); int height = Convert.ToInt32(imgUrl.Split('!')[1].Replace(".jpg", "").Split('x')[1]); //图片已经存在直接输出 if (File.Exists(context.Server.MapPath("~/" + imgUrl))) { OutPutImg("~/"+imgUrl); } else { if (!string.IsNullOrEmpty(imgUrl) && File.Exists(context.Server.MapPath("~/" + trueFilePath))) { Image originalImage = System.Drawing.Image.FromFile(context.Server.MapPath("~/" + trueFilePath)); var newBitmap = new Bitmap(originalImage); //生成相应的小图并保存 SendSmallImage(context.Server.MapPath("~/" + trueFilePath),context.Server.MapPath("~/" + imgUrl), width, height, "meiyouyisi"); //输出 OutPutImg("~/" + imgUrl); } else//图片如果不存在 输出默认图片 { //OutPutImg(imgUrl); } } }
以上就是全部代码。效果请查看:
http://meiyouyisi.com/ 右键在新标签中打开图片 后面的100x100 可以修改成相应的图片大小。
我没加判断别生成过多的缩略图 谢谢!