首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

C#生成图片缩略图的方法 提高图片清晰度,该如何解决

2012-01-07 
C#生成图片缩略图的方法 提高图片清晰度这里是我的源码C# codeusing System.Drawing.Imagingusing System

C#生成图片缩略图的方法 提高图片清晰度
这里是我的源码 

C# code
using System.Drawing.Imaging;using System.Drawing;public partial class A_T_userpicture : System.Web.UI.Page{    protected void Page_Load(object sender, EventArgs e)    {        if (!Page.IsPostBack)        {            string fileName = "";            string filePath = "";            //清除输出            Response.Clear();            System.Drawing.Image img = null;            if (Request.QueryString["fsrc"] != null)            {                                fileName = Request.QueryString["fsrc"].ToString();                                if (fileName.StartsWith("http://"))                 {                     filePath =fileName;                     //获取URL流                     System.Net.WebClient client = new System.Net.WebClient();                     System.IO.Stream stream = new System.IO.MemoryStream();                     stream = client.OpenRead(filePath);                     //从流中得到img对象                     img = System.Drawing.Image.FromStream(stream, true);                 }                 else                 {                     filePath = Server.MapPath(fileName);                     img = System.Drawing.Image.FromFile(filePath);                 }                             }            else            {                try                {                    filePath = Server.MapPath("mumu.JPG");                    img = System.Drawing.Image.FromFile(filePath);                        }                catch(Exception se)                {                    throw se;                    //return;                 }            }            //初始化图片的宽和高            int fw =120;            int fh =120;            if (Request.QueryString["fw"] != null && Request.QueryString["fh"]!=null)            {                fw = int.Parse(Request.QueryString["fw"].ToString());                fh = int.Parse(Request.QueryString["fh"].ToString());            }                        System.Drawing.Bitmap outBmp = new System.Drawing.Bitmap(fw,fh);            System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(outBmp);            try            {                ImageFormat thisformat = img.RawFormat;                System.Drawing.Size newSize = this.getNewSize(fw, fh, img.Width, img.Height);                //设置画布质量                //线性质量设置 ( AssumeLinear ) 高速度呈现。                g.CompositingQuality = System.Drawing.Drawing2D.CompositingQuality.HighSpeed;                //指定高速度呈现                g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighSpeed;                //指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。                 g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;                //清除绘图并指定背景色                g.Clear(System.Drawing.Color.Transparent);                //在指定位置并且按指定大小绘制指定的 Image 的指定部分。                g.DrawImage(img,new System.Drawing.Rectangle((fw-newSize.Width)/2,(fh-newSize.Height)/2,newSize.Width,newSize.Height),0,0,img.Width, img.Height, System.Drawing.GraphicsUnit.Pixel);                               System.IO.MemoryStream ms = new System.IO.MemoryStream();                //特定图像编码器,编码方式                EncoderParameters parameters = new EncoderParameters(2);                parameters.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, ((long)90));                parameters.Param[1] = new EncoderParameter(Encoder.Compression, ((long)1024));                                System.Drawing.Imaging.ImageCodecInfo jpegICI = null;                //检索有关已安装的图像编码解码器的所有相关信息                System.Drawing.Imaging.ImageCodecInfo[] arrayICI = System.Drawing.Imaging.ImageCodecInfo.GetImageEncoders();                for (int i = 0; i < arrayICI.Length - 1; i++)                {                    if (arrayICI[i].FormatDescription.Equals("JPEG"))                    {                        jpegICI = arrayICI[i];                        outBmp.Save(ms, jpegICI, parameters);                    }                    else                    {                        outBmp.Save(ms, thisformat);                    }                }                Response.ClearContent();                #region 响应类型设置                if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Gif))                {                    Response.ContentType = "image/Gif";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Wmf))                {                    Response.ContentType = "image/Wmf";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Bmp))                {                    Response.ContentType = "image/bmp";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Emf))                {                    Response.ContentType = "image/Emf";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Exif))                {                    Response.ContentType = "image/Exif";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Icon))                {                    Response.ContentType = "image/Icon";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Png))                {                    Response.ContentType = "image/Png";                }                else if (thisformat.Equals(System.Drawing.Imaging.ImageFormat.Tiff))                {                    Response.ContentType = "image/Tiff";                }                else                {                    Response.ContentType = "image/jpeg";                }                #endregion                //写入流                Response.BinaryWrite(ms.ToArray());                                  parameters.Dispose();                outBmp.Dispose();                g.Dispose();            }            catch(Exception se)            {                outBmp.Dispose();                g.Dispose();                throw se;            }        }    }    //自适应图片大小    private System.Drawing.Size getNewSize(int maxwidth,int maxheight,int width,int height)    {        try        {            double W = 0.0;            double H = 0.0;            double SW = Convert.ToDouble(width);            double SH = Convert.ToDouble(height);            double MW = Convert.ToDouble(maxwidth);            double MH = Convert.ToDouble(maxheight);            if (SW < MW && SH < MH)            {                W = SW;                H = SH;            }            else if ((SW / SH) > (MW / MH))            {                W = maxwidth;                H = (W * SH) / SW;            }            else            {                H = maxheight;                W = (H * SW) / SH;            }            return new System.Drawing.Size(Convert.ToInt32(W),Convert.ToInt32(H));        }        catch(Exception se)        {            throw se;        }    }} 


原始图片地址:http://www.huadublog.cn/images/huadublog_com/308557038/38901/r_2006121923255632812.jpg
生成后的图片效果:
http://localhost:1197/noble/Admin/userpicture.aspx?fsrc=http://www.huadublog.cn/images/huadublog_com/308557038/38901/r_2006121923255632812.jpg
大家随便建立一个aspx页面 然后我的代码贴了看效果

我觉得我这个方法有两点不好:
 (1):生成速度慢,在绑定的时候如果一个页面上多次使用我的这个缩小图的绑定 就显示不了图片
 (2):清晰度不高 比较模糊
 希望能人智者给我点建议
  


[解决办法]
生成速度慢 估计是慢在获取远程图片的时候吧

热点排行