网站中需要上传图片,而且要把图片缩略后方便显示,上传照片一切ok就是在生成缩略图的时候出错,各位看看代码,帮忙解决下
     //上传代码    string imageurl = "cpimage/" + FileUpload1.FileName;
        
        string imagetype = FileUpload1.FileName.Split('.')[1];
        
        
        if (imagetype.ToLower() == "jpg" || imagetype.ToLower() == "gif" || imagetype.ToLower() == "png")
        {
            FileUpload1.SaveAs(Server.MapPath(imageurl));
        }
        //生成缩略图        ImageBll.ImageUtility.MakeThumbImage(Server.MapPath(imageurl),"~/scpimage", 100, 100, "W");
           //Server.MapPath(imageurl)原图是存放路径
           //"~/scpimage"   缩略图存放路径     (我想应该是错在路径上,但是我改了好几中都不行;Server.MapPath(scpimage))这种不行,"../scpimage"也不行//保存到数据库
        string sql = "INSERT INTO chanpin (cnm, nm, content, imagee) VALUES ('" + this.DropDownList1.Text + "', '" + this.TextBox1.Text + "', '" + this.TextBox2.Text + "', '" + imageurl + "')";
        if (int.Parse(DB.bb(sql).ToString()) > 0)
        {
            Response.Write("<script>alert('添加成功!');location.href='cpxx.aspx';</script> ");            //Page.ClientScript.RegisterStartupScript(this.GetType(), "zz", "alert( '新增成功!')", true);
            //Response.Redirect("cpxx.aspx");
        }
///生成缩略图类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.IO;namespace ImageBll
{
    public class ImageUtility
    {
        /// <summary>
        /// 生成缩略图的方法
        /// </summary>
        /// <param name="Path">原始图片路径</param>
        /// <param name="StPath">生成图片存放的路径</param>
        /// <param name="width">生成图片的宽度</param>
        /// <param name="height">生成图片的高度</param>
        /// <param name="mode">生成的模式( "HW":指定高宽缩放;"W"://指定宽度,计算缩略图;"H":指定高度,计算缩略图;"CUT":)</param>
        /// <returns></returns>
        public static string MakeThumbImage(string Path, string StPath, int width, int height, string mode)
        {
            string messages = string.Empty;
            System.Drawing.Image image = System.Drawing.Image.FromFile(Path);
            int tw = width;
            int th = height;
            //原始图片的宽度和高度
             int sw = image.Width;
            int sh = image.Height;
            int x = 0, y = 0;
            switch (mode)
            {
                case "HW"://指定高宽缩放
                    break;
                case "W"://按比例缩放,指定宽度,计算缩略图的高度
                    th = image.Height * width / image.Width;
                    break;
                case "H"://按比例缩放,指定高度,计算缩略图的高度
                    tw = image.Width * height / image.Height;
                    break;
                case "CUT":
                    if ((double)tw / (double)th < (double)width / (double)height)
                    {
                        sw = image.Width;
                        sh = image.Width * height / tw;
                        x = 0;
                        y = (image.Height - sh) / 2;
                    }
                    else
                    {
                        sh = image.Height;
                        sw = image.Height * width / th;
                        y = 0;
                        x = (image.Width - sw) / 2;
                    }
                    break;
                default: break;
            }
            System.Drawing.Image bitmap = new System.Drawing.Bitmap(tw, th);
            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(System.Drawing.Color.Transparent);
            g.DrawImage(image,
                new System.Drawing.Rectangle(x, y, tw, th),
                new System.Drawing.Rectangle(x, y, sw, sh),
                System.Drawing.GraphicsUnit.Pixel);
            try
            {
                //以jpg格式保存缩略图
                bitmap.Save(StPath, System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch (System.Exception e)
            {
                throw e;
            }
            finally
            {                image.Dispose();
                bitmap.Dispose();
                g.Dispose();
                if (messages == string.Empty)
                {
                    messages = "成功生成!";
                }
            }
                
               
            
                                 return messages;
        }
    }
}
麻烦帮我看看是不是路径错误???正确的该怎么写?
       

解决方案 »

  1.   

    Server.MapPath("~/");
    单步跟踪数据
    public static void MakeThumbnail(string originalImagePath, string thumbnailPath, int width, int height, string mode)
      {
      Image originalImage = Image.FromFile(originalImagePath);
      int towidth = width;
      int toheight = height;
      int x = 0;
      int y = 0;
      int ow = originalImage.Width;
      int oh = originalImage.Height;   
      switch (mode)
      {   
      case "HW":   
      break;
      case "W":   
      toheight = originalImage.Height * width/originalImage.Width;
      break;
      case "H":
      towidth = originalImage.Width * height/originalImage.Height;   
      break;   
      case "Cut":   
      if((double)originalImage.Width/(double)originalImage.Height > (double)towidth/(double)toheight)
      {
      oh = originalImage.Height;
      ow = originalImage.Height*towidth/toheight;
      y = 0;
      x = (originalImage.Width - ow)/2;
      }
      else
      {
      ow = originalImage.Width;
      oh = originalImage.Width*height/towidth;
      x = 0;
      y = (originalImage.Height - oh)/2;
      }
      break;   
      default :
      break;
      }   
      Image bitmap = new System.Drawing.Bitmap(towidth,toheight);
      Graphics g = System.Drawing.Graphics.FromImage(bitmap);
      g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
      g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
      g.Clear(Color.Transparent);   
      g.DrawImage(originalImage, new Rectangle(0, 0, towidth, toheight),   
      new Rectangle(x, y, ow,oh),
      GraphicsUnit.Pixel);  try
      {   
      bitmap.Save(thumbnailPath, System.Drawing.Imaging.ImageFormat.Jpeg);
      }
      catch(System.Exception e)
      {
      throw e;
      }
      finally
      {
      originalImage.Dispose();
      bitmap.Dispose();   
      g.Dispose();
      }
      }
      

  2.   

    错在这句
    ImageBll.ImageUtility.MakeThumbImage(Server.MapPath(imageurl),"~/scpimage", 100, 100, "W");

    原始图片路径你有进行了server.MapPath转换,但在生成缩略图的保存路径没有进行绝对路径转换
    改成这样试
    ImageBll.ImageUtility.MakeThumbImage(Server.MapPath(imageurl),Server.MapPath("~/scpimage"+FileUpload1.FileName), 100, 100, "W");
      

  3.   


    ImageBll.ImageUtility.MakeThumbImage(Server.MapPath(imageurl),"~/scpimage", 100, 100, "W");这句的写法并没有向生成缩略图的函数提供完整的保存缩略图的路径,只给了一个相对路径的文件夹,而且没有包含所要保存的文件的文件名