在做web程序时候,客户上传了一张图片到服务器,我需要同时为这张图片制作一个缩略图,请问这个缩略图如何制作?谢谢大侠.急

解决方案 »

  1.   

    这是我以前用过的生成立体版权,与缩略图
    给几篇文章你参考
    http://dev.csdn.net/develop/article/26/26706.shtm
    http://dev.csdn.net/develop/article/21/21341.shtm
    public string UploadImage(System.Web.HttpPostedFile postFile)
    {
    try
    {
    if(postFile.ContentLength==0)
    {
    throw(new Exception("NotExistedPostFile"));
    }
    else 
    if(postFile.ContentLength>Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["MaxImageSize"]))
    {
    throw(new Exception("TooLargePostFile"));
    }
    String strExt=System.IO.Path.GetExtension(postFile.FileName);
    if(System.Configuration.ConfigurationSettings.AppSettings["ImageExtension"].IndexOf(";"+strExt+";")>-1)
    {
    throw(new Exception("ErrorImageExtension"));
    } String strFileName=@"/uploads/images/"+DateTime.Now.ToString("yyyyMMddHHmmssffff")+".jpg";
    String strAbsoluteFileName=System.Web.HttpContext.Current.Server.MapPath(strFileName);
    String Copyright=System.Configuration.ConfigurationSettings.AppSettings["CopyRight"]; System.Drawing.Image imgPhoto=System.Drawing.Image.FromStream(postFile.InputStream,true);
    //取高和宽
    int phWidth = imgPhoto.Width; 
    int phHeight =imgPhoto.Height;
    //建新图,指定格式为每像素 24 位;红色、绿色和蓝色分量各使用 8 位。
    Bitmap bmPhoto = new Bitmap(phWidth, phHeight,PixelFormat.Format24bppRgb);
    //设置分辨率
    bmPhoto.SetResolution(imgPhoto.HorizontalResolution,imgPhoto.VerticalResolution);
    //准备Graphics
    Graphics grPhoto = Graphics.FromImage(bmPhoto);
    try
    {
    //指定消除锯齿的呈现。
    grPhoto.SmoothingMode = SmoothingMode.AntiAlias;
    //拷贝原图到做图区
    grPhoto.DrawImage(
    imgPhoto,                               
    new Rectangle(0, 0, phWidth, phHeight), 
    0,                                      
    0,                                       
    phWidth,                                
    phHeight,                               
    GraphicsUnit.Pixel);
    //用指定Sizes绘制图片时,测量用指定的字串宽度
    //取可能的最大宽度
    int[] sizes = new int[]{64,48,32,16,8,6,4};
    Font crFont = null; 
    SizeF crSize = new SizeF(); 
    for (int i=0 ;i<7; i++)

    crFont = new Font("Verdana", sizes[i],
    FontStyle.Bold);
    crSize = grPhoto.MeasureString(Copyright,
    crFont);
    if((ushort)crSize.Width < (ushort)phWidth)
    break;
    }
    //指定做图点
    int yPixlesFromBottom = (int)(phHeight *.05);
    float yPosFromBottom = ((phHeight - 
    yPixlesFromBottom)-(crSize.Height/2));
    float xCenterOfImg = (phWidth/2);
    StringFormat StrFormat = new StringFormat();
    StrFormat.Alignment = StringAlignment.Center;
    //绘制copyright
    SolidBrush semiTransBrush2 = 
    new SolidBrush(Color.FromArgb(100, 0, 0,0)); 
    grPhoto.DrawString(Copyright,                    
    crFont,                                      
    semiTransBrush2,                             
    new PointF(xCenterOfImg+1,yPosFromBottom+1), 
    StrFormat);
    SolidBrush semiTransBrush = new SolidBrush(
    Color.FromArgb(100, 255, 255, 255));
    grPhoto.DrawString(Copyright,                 
    crFont,                                   
    semiTransBrush,                           
    new PointF(xCenterOfImg,yPosFromBottom),  
    StrFormat);
    bmPhoto.Save(strAbsoluteFileName,ImageFormat.Jpeg);
    }
    finally
    {
    grPhoto.Dispose();
    bmPhoto.Dispose();
    imgPhoto.Dispose();
    }
    return strFileName;
    }
    catch(Exception excep)
    {
    throw(excep);
    }
    }
      

  2.   

    我是以前遇到这样的问题过,我用的是visual studio,当时没有生成缩略图,直接在页面上拉了个IMAGE控件,然后把控件的固定成一个合适的大小,然后用户上传上图片以后在IMAGE控件里显示就行了,和缩略图效果一样的,就是把图片另存下来可以看到实际上图片没有变小,还是原图,但是显示是小了。
      

  3.   

    公司有个NB的人用JS写个程序,在页面上显示大图,在右下角显示小图,小图就是大图的缩略图(只有大图的1/10),还可以拖来拖去!只不过我看不到代码!UP
      

  4.   

    谢谢 LaoDai_Net(老代.Net『学无止境』) ,果然是高人.根据你提供的文章搞定了