照片列表里面的缩略照片都是有固定的,相同的宽度,和高度。但是呢,点开链接后,看到的大图都是有不同的宽和高,不同的比例。我知道小图的实际大小就是那么小。怎么做到将这些不同的大图,缩略成固定大小的小图,而又不失真!!!

解决方案 »

  1.   

    http://www.cnblogs.com/21dacia/articles/1335495.html
    http://www.cnblogs.com/rock_chen/archive/2006/08/05/468387.html
      

  2.   

    看一下这段代码就知道了private static Size NewSize( int maxWidth, 
    int maxHeight, int width, 
    int height )  
    {
        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 Size( Convert.ToInt32( w ), Convert.ToInt32( h ) );
    }public static void SendSmallImage( string fileName, 
    string newFile, 
    int maxHeight, 
    int maxWidth )  
    {
        System.Drawing.Image img = System.Drawing.Image.FromFile( fileName );
        System.Drawing.Imaging.ImageFormat 
        thisFormat = img.RawFormat;
        Size newSize = NewSize( maxWidth, maxHeight, img.Width, img.Height );
        Bitmap outBmp = new Bitmap( newSize.Width, newSize.Height );
        Graphics g = Graphics.FromImage( outBmp );
        // 设置画布的描绘质量
        g.CompositingQuality = CompositingQuality.HighQuality;
        g.SmoothingMode = SmoothingMode.HighQuality;
        g.InterpolationMode = InterpolationMode.HighQualityBicubic;
        g.DrawImage( img, new Rectangle( 0, 0, newSize.Width, newSize.Height ),
        0, 0, img.Width, img.Height, GraphicsUnit.Pixel );
        g.Dispose( );
        // 以下代码为保存图片时,设置压缩质量
        EncoderParameters encoderParams = new EncoderParameters( );
        long[] quality = new long[1];
        quality[0] = 100;
        EncoderParameter encoderParam = new EncoderParameter( System.Drawing.Imaging.Encoder.Quality, quality );
        encoderParams.Param[0] = encoderParam;
        //获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象.
        ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders( );
        ImageCodecInfo jpegICI = null;
        for ( int x = 0;
        x < arrayICI.Length;
        x++ )
        {
            if ( arrayICI[x].FormatDescription.Equals( "JPEG" ) )
            {
                jpegICI = arrayICI[x];
                //设置JPEG编码
                break;
            }
        }
        if ( jpegICI != null )
        {
            outBmp.Save( newFile, jpegICI, encoderParams );
        }
        else
        {
            outBmp.Save( newFile, 
            thisFormat );
        }
        img.Dispose( );
        outBmp.Dispose( );

    Graphics 设置为 InterpolationMode.HighQualityBicubic 这样保证缩略算法最优化,生成最高质量的缩略图。
    即使这样生成JPEG图片的文件,效果也不是很理想的。后来发现.net可以给编码器赋参数。也就是说可以设置JPEG的质量参数。效果对比:
    原图设置InterpolationMode.HighQualityBicubic,没有设置JPGE质量参数设置InterpolationMode.HighQualityBicubic,设置JPGE质量参数为100
    代码: 
    引用 
    using System;
    using System.Data;
    using System.Configuration;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Drawing;
    using System.IO;
    using System.Drawing.Imaging;
    using System.Net;
    using System.Drawing.Drawing2D;public partial class _Default : System.Web.UI.Page 
    {
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    }
    }/// <summary>
    /// 得到文件代码的二进制码流
    /// </summary>
    /// <param name="url"></param>
    /// <returns></returns>
    private byte[] getFileCodebyURL(string url)
    {
    byte[] page;
    try
    {
    WebClient client = new WebClient();
    page = client.DownloadData(url);
    }
    catch
    {
    page = null;
    }
    return page;
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
    MemoryStream ms = new MemoryStream(getFileCodebyURL(TextBox1.Text));
    System.Drawing.Image originalImage = System.Drawing.Image.FromStream(ms);
    int x, y, w, h;
    if (originalImage.Width > 160 && originalImage.Height > 120)
    {
    w = 160;
    h = 160 * originalImage.Height / originalImage.Width;
    if (h > 120)
    {
    h = 120;
    w = 120 * originalImage.Width / originalImage.Height;
    x = (160 - w) / 2;
    y = 0;
    }
    else
    {
    x = 0;
    y = (120 - h) / 2;
    }
    }
    else if (originalImage.Width > 160)
    {
    w = 160;
    h = 160 * originalImage.Height / originalImage.Width;
    x = 0;
    y = (120 - h) / 2;
    }
    else if (originalImage.Height > 120)
    {
    h = 120;
    w = 120 * originalImage.Width / originalImage.Height;
    x = (160 - w) / 2;
    y = 0;
    }
    else
    {
    w = originalImage.Width;
    h = originalImage.Height;
    x = (160 - w) / 2;
    y = (120 - h) / 2;
    }
    Bitmap bm = new Bitmap(160, 120);
    Graphics g = Graphics.FromImage(bm);// 指定高质量的双三次插值法。执行预筛选以确保高质量的收缩。此模式可产生质量最高的转换图像。
    //g.InterpolationMode = InterpolationMode.HighQualityBicubic;// 指定高质量、低速度呈现。
    //g.SmoothingMode = SmoothingMode.HighQuality;g.SmoothingMode = SmoothingMode.HighQuality;
    g.InterpolationMode = InterpolationMode.HighQualityBicubic;g.Clear(Color.White);
    g.DrawImage(originalImage, new Rectangle(x, y, w, h), 0, 0, originalImage.Width, originalImage.Height, GraphicsUnit.Pixel);long[] quality = new long[1];
    quality[0] = 100;System.Drawing.Imaging.EncoderParameters encoderParams = new System.Drawing.Imaging.EncoderParameters();
    System.Drawing.Imaging.EncoderParameter encoderParam = new System.Drawing.Imaging.EncoderParameter(System.Drawing.Imaging.Encoder.Quality,quality);
    encoderParams.Param[0] = encoderParam;
    ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();//获得包含有关内置图像编码解码器的信息的ImageCodecInfo 对象。
    ImageCodecInfo jpegICI = null;
    for (int i = 0; i < arrayICI.Length; i++)
    {
    if (arrayICI[i].FormatDescription.Equals("JPEG"))
    {
    jpegICI = arrayICI[i];//设置JPEG编码
    break;
    }
    }
    if (jpegICI != null)
    {
    bm.Save(Server.MapPath("upload/c.jpg"), jpegICI, encoderParams);
    }bm.Dispose();
    originalImage.Dispose();
    g.Dispose();
    Image1.ImageUrl = "http://localhost:7374/caipiao/upload/c.jpg";
    }
    }
      

  3.   

    方法就是,固定小图外层div的宽度和高度,div的overflow="hidden"