日期:2014-05-18  浏览次数:20634 次

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.Im