就是用户在上传一张图片,比如一张400*300大小的图片test.gif,服务器在接收完后同时创建另外一张test.gif的40*30的缩小图view.gif。应该可以实现吧,现求代码~~80分马上奉献上(心痛啊,自己太懒了-_-)

解决方案 »

  1.   

    http://yefei520.cnblogs.com/archive/2006/01/05/311343.html
      

  2.   

    #region 生成缩略图
    //生成缩略图函数
    public static void MakeSmallImg(System.Web.HttpPostedFile postFile,string saveImg,System.Double Width,System.Double Height)
    {
    //原始图片名称
    string originalFilename = postFile.FileName;
    //生成的高质量图片名称
    string strGoodFile = saveImg; //从文件取得图片对象
    System.Drawing.Image image = System.Drawing.Image.FromStream(postFile.InputStream,true); System.Double NewWidth,NewHeight;
    if(image.Width>image.Height)
    {
    NewWidth=Width;
    NewHeight=image.Height*(NewWidth/image.Width);
    }
    else
    {
    NewHeight=Height;
    NewWidth=(NewHeight/image.Height)*image.Width;
    } if (NewWidth>Width)
    {
    NewWidth=Width;
    }
    if (NewHeight>Height)
    {
    NewHeight=Height;
    } //取得图片大小
    System.Drawing.Size size = new Size((int)NewWidth,(int)NewHeight);
    //新建一个bmp图片
    System.Drawing.Image bitmap = new System.Drawing.Bitmap(size.Width,size.Height);
    //新建一个画板
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
    //设置高质量插值法
    g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
    //设置高质量,低速度呈现平滑程度
    g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
    //清空一下画布
    g.Clear(Color.White);
    //在指定位置画图
    g.DrawImage(image, new System.Drawing.Rectangle(0, 0, bitmap.Width, bitmap.Height), 
    new System.Drawing.Rectangle(0, 0, image.Width,image.Height),
    System.Drawing.GraphicsUnit.Pixel); ///文字水印
    //System.Drawing.Graphics G=System.Drawing.Graphics.FromImage(bitmap);
    //System.Drawing.Font f=new Font("宋体",10);
    //System.Drawing.Brush b=new SolidBrush(Color.Black);
    //G.DrawString("myohmine",f,b,10,10);
    //G.Dispose(); ///图片水印
    //System.Drawing.Image copyImage = System.Drawing.Image.FromFile(System.Web.HttpContext.Current.Server.MapPath("pic/1.gif"));
    //Graphics a = Graphics.FromImage(bitmap);
    //a.DrawImage(copyImage, new Rectangle(bitmap.Width-copyImage.Width,bitmap.Height-copyImage.Height,copyImage.Width, copyImage.Height),0,0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel); //copyImage.Dispose();
    //a.Dispose();
    //copyImage.Dispose(); //保存高清晰度的缩略图
    bitmap.Save(strGoodFile, System.Drawing.Imaging.ImageFormat.Jpeg); g.Dispose();
    image.Dispose();
    bitmap.Dispose();
    }
    #endregion
      

  3.   

    上传大图,并根据大图长宽按比例生成小图:
    switch(this.selectfile.PostedFile.ContentType.ToString())
    {
    case "image/pjpeg":
    string filename = Hmg.DBAccess.Tools.GetGuidString();
    string filename_small = filename+"_";
    filename += ".JPG";
    filename_small += ".JPG";
    this.selectfile.PostedFile.SaveAs(HttpRuntime.AppDomainAppPath+@"UploadFile\UserMaterial\1\"+filename);//*
    System.Drawing.Image myImage = System.Drawing.Image.FromFile(HttpRuntime.AppDomainAppPath+@"UploadFile\UserMaterial\1\"+filename);//*
    string  su = System.Configuration.ConfigurationSettings.AppSettings["WebUrl"]+"uploadfile/usermaterial/1/"+filename;//*;
    //横图
    if(myImage.Width > myImage.Height || myImage.Width == myImage.Height)
    {
    if(myImage.Width > 124)
    {
    su = System.Configuration.ConfigurationSettings.AppSettings["WebUrl"]+"uploadfile/usermaterial/1/"+filename_small;//*
    double percent = Math.Round(124 / (double)myImage.Width , 2);//缩放百分比
    int new_width = 124;
    int new_height = Convert.ToInt32(Math.Round(myImage.Height * percent));
    Hmg.DBAccess.Tools.MakeSmallImg(this.selectfile.PostedFile,HttpRuntime.AppDomainAppPath+@"UploadFile\UserMaterial\1\"+filename_small,new_width,new_height);//*
    }
    }
    //竖图
    if(myImage.Height > myImage.Width)
    {
    if(myImage.Height > 124)
    {
    su = System.Configuration.ConfigurationSettings.AppSettings["WebUrl"]+"uploadfile/usermaterial/1/"+filename_small;//*
    double percent = Math.Round(124 / (double)myImage.Height , 2);//缩放百分比
    int new_width = Convert.ToInt32(Math.Round(myImage.Height * percent));
    int new_height = 124;
    Hmg.DBAccess.Tools.MakeSmallImg(this.selectfile.PostedFile,HttpRuntime.AppDomainAppPath+@"UploadFile\UserMaterial\1\"+filename_small,new_width,new_height);//*
    }
    }
    //写数据库
    UserMaterialInfo myU = new Hmg.Model.UserMaterialInfo();
    myU.ImageSmallUrl = su;
    myU.UserNumber = Convert.ToInt32(Session["UserNumber"]);
    myU.Name = Hmg.DBAccess.Tools.CleanInputText(this.txtName.Text,255);
    myU.Url = System.Configuration.ConfigurationSettings.AppSettings["WebUrl"]+"uploadfile/usermaterial/1/"+filename;//*
    myU.Size = this.selectfile.PostedFile.ContentLength;
    myU.ImageWidth = myImage.Width;
    myU.ImageHeight = myImage.Height;
    myU.FileTypes = 1;
    myU.Types = 1;//1封面 2封二 3内页 4封底 5其它图片 6背景音乐 //*
    myImage.Dispose();
    if(myMat.AddUserMaterial(myU))
    {
    Response.Write(Hmg.DBAccess.Tools.Alert("记录添加成功!"));
    //绑定
    this.binddlst(Convert.ToInt32(ViewState["pagenow"]));
    }
    break;
    ......
      

  4.   

    可以給你一個以前寫的轉門處理圖片的類給你;
    MSN:[email protected]