asp.net生成的缩略图不够清晰,望高手解决...
public static bool CreateMiniPic(string url, string newPath, int width) { double w = 0.0; double h = 0.0; bool result = false; System.Drawing.Image image = null; System.Drawing.Graphics g = null; string newMapPath = System.Web.HttpContext.Current.Server.MapPath(newPath); try { //从文件取得图片对象 System.Net.WebRequest rq = System.Net.WebRequest.Create(url); rq.Timeout = 10000; System.Net.HttpWebResponse rs = (System.Net.HttpWebResponse)rq.GetResponse(); Stream st = rs.GetResponseStream(); image = System.Drawing.Image.FromStream(st); w = Convert.ToDouble(width); h = (w * Convert.ToDouble(image.Height)) / Convert.ToDouble(image.Width); //取得图片大小 System.Drawing.Size size = new System.Drawing.Size(Convert.ToInt32(w), Convert.ToInt32(h)); //新建一个bmp图片 System.Drawing.Image bmp = new System.Drawing.Bitmap(size.Width, size.Height); //新建一个画板 g = System.Drawing.Graphics.FromImage(bmp); //设置高质量插值法 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; //设置高质量,低速度呈现平滑程度 g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality; //清空一下画布 g.Clear(System.Drawing.Color.Transparent); //在指定位置画图 g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), new System.Drawing.Rectangle(0, 0, image.Width, image.Height), System.Drawing.GraphicsUnit.Pixel); //保存高清晰度的缩略图 bmp.Save(newMapPath, System.Drawing.Imaging.ImageFormat.Jpeg); result = true; } catch { result = false; } finally { g.Dispose(); image.Dispose(); } return result; }
MSDN中有示例,你可以看看这方法的示例代码
Image.Save(String, ImageCodecInfo, EncoderParameters)
能给个网站吗
本人英语不是很好,MSDN的都是英文
缩略图本来就是不清晰的
可以实现清楚的。只要想到的都可以
怎么的,你只想尺寸改变?
尺寸改变,清楚度不变
@Ewin: Image.Save(String, ImageCodecInfo, EncoderParameters);
EncoderParameters ep = new EncoderParameters(1); ep.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, (long)100); ImageCodecInfo[] icis = ImageCodecInfo.GetImageEncoders(); ImageCodecInfo ici = null; string switch_on = url.Substring(url.LastIndexOf('.')+1); switch (switch_on.ToLower()) { case "jpg": ici=icis[1]; break; case "gif": ici=icis[2]; break; case "bmp": ici=icis[0]; break; case "png": ici=icis[4]; break; default: ici=icis[1]; break; } bmp.Save(newMapPath,ici,ep);
ImageFormat.Jpeg 图片格式应该用 5个左右吧,我记录有一个是很清楚的,具体是哪个目前想不起来了,你可以都试一下
Jpeg 最清晰吧