asp.net2.0 我就是把图片上传到 photo 文件夹
代码: 
try 
                { 
                        if   (FileUpload1.PostedFile.FileName   ==   " ") 
                        { 
                                this.lb_info.Text   =   "请选择文件! "; 
                        } 
                        else 
                        { 
                                string   filepath   =   FileUpload1.PostedFile.FileName; 
                                string   filename   =   filepath.Substring(filepath.LastIndexOf( "\\ ")   +   1); 
                                //string   serverpath1   =   Server.MapPath( "images/ ")   +   filename; 
                                string   serverpath2   =   Server.MapPath( "images/ ")   +   System.DateTime.Now.ToString( "yyy-MM-dd-hh-mm-ss ")   +   Session.SessionID   +   filename; 
                                //     FileUpload1.PostedFile.SaveAs(serverpath1); 
                                Response.Write(filepath); 
                                ImageThumbnail   img   =   new   ImageThumbnail(filepath); 
                                img.ReducedImage(200,   200,   serverpath2);//0.4表示缩小40% 
                                this.lb_info.Text   =   "上传成功! "; 
                        } 
                } 
                catch   (Exception   error) 
                { 
                        this.lb_info.Text   =   "上传发生错误!原因: "   +   error.ToString(); 
                } 
类代码: 
using   System; 
using   System.IO; 
using   System.Drawing; 
using   System.Drawing.Imaging; public   class   ImageThumbnail 

        public   Image   ResourceImage; 
        private   int   ImageWidth; 
        private   int   ImageHeight; 
        public   string   ErrorMessage;         public   ImageThumbnail(string   ImageFileName) 
        { 
                ResourceImage   =   Image.FromFile(ImageFileName); 
                ErrorMessage   =   " "; 
        }         public   bool   ThumbnailCallback() 
        { 
                return   false; 
        } 
        //   方法1,按大小 
        public   bool   ReducedImage(int   Width,   int   Height,   string   targetFilePath) 
        { 
                try 
                { 
                        Image   ReducedImage; 
                        Image.GetThumbnailImageAbort   callb   =   new   Image.GetThumbnailImageAbort(ThumbnailCallback); 
                        ReducedImage   =   ResourceImage.GetThumbnailImage(Width,   Height,   callb,   IntPtr.Zero); 
                        ReducedImage.Save(@targetFilePath,   ImageFormat.Jpeg); 
                        ReducedImage.Dispose(); 
                        return   true; 
                } 
                catch   (Exception   e) 
                { 
                        ErrorMessage   =   e.Message; 
                        return   false; 
                } 
        } 
        //   方法2,按百分比     缩小60%   Percent为0.6   targetFilePath为目标路径 
        public   bool   ReducedImage(double   Percent,   string   targetFilePath) 
        { 
                try 
                { 
                        Image   ReducedImage; 
                        Image.GetThumbnailImageAbort   callb   =   new   Image.GetThumbnailImageAbort(ThumbnailCallback); 
                        ImageWidth   =   Convert.ToInt32(ResourceImage.Width   *   Percent); 
                        ImageHeight   =   (ResourceImage.Height)   *   ImageWidth   /   ResourceImage.Width;//等比例缩放 
                        ReducedImage   =   ResourceImage.GetThumbnailImage(ImageWidth,   ImageHeight,   callb,   IntPtr.Zero); 
                        ReducedImage.Save(@targetFilePath,   ImageFormat.Jpeg); 
                        ReducedImage.Dispose(); 
                        return   true; 
                } 
                catch   (Exception   e) 
                { 
                        ErrorMessage   =   e.Message; 
                        return   false; 
                } 
        } 

错误信息: 
上传发生错误!原因:System.IO.FileNotFoundException:   C:\Documents   and   Settings\Administrator\桌面\猫\13.jpg   在   System.Drawing.Image.FromFile(String   filename,   Boolean   useEmbeddedColorManagement)   在   System.Drawing.Image.FromFile(String   filename)   在   ImageThumbnail..ctor(String   ImageFileName)   位置   e:\wwwroot\App_Code\ImageThumbnail.cs:行号   15   在   admin_cus_manage_invete_edit.Button4_Click(Object   sender,   EventArgs   e)   位置   e:\wwwroot\admin\cus_manage\invete_edit.aspx.cs:行号   197 
问高手怎么该~~~

解决方案 »

  1.   

    上传发生错误!原因:System.IO.FileNotFoundException: C:\Documents and Settings\new\桌面\肖像4.jpg 在 System.Drawing.Image.FromFile(String filename, Boolean useEmbeddedColorManagement) 在 System.Drawing.Image.FromFile(String filename) 在 Regedit.ImageThumbnail..ctor(String ImageFileName) 位置 d:\freehost\lianght7924\web\Regedit.aspx.vb:行号 171 在 Regedit.ButtonTJ_Click(Object sender, EventArgs e) 位置 d:\freehost\lianght7924\web\Regedit.aspx.vb:行号 100 
      

  2.   


    HttpContext.Current.Server.MapPath(@"photo\你的图片名")
      

  3.   

    System.IO.FileNotFoundException
    未找到文件
    路径错了 核对下吧
      

  4.   

    System.IO.FileNotFoundException: C:\Documents and Settings\new\桌面\肖像4.jpg从提示上来看 好象是说文件没找到,不要把图片放到桌面 上 ,放到 c盘或 D盘 根目录下 试一下。
      

  5.   

    我不是搞VB.Net的,但是我觉得你没必要忙着写路径来存。
    你可以先Response出你的路径看看是否正确再来做。
    在C#里用String serverpath1 = Server.MapPath("~/photo/") + filename;
      

  6.   

     try
            {
                if (FileUpload1.PostedFile.FileName == " ")
                {
                    this.lb_info.Text = "请选择文件! ";
                }
                else
                {
                    string filepath = FileUpload1.PostedFile.FileName;
                    //string  filename  =  filepath.Substring(filepath.LastIndexOf( "\\ ")  +  1);
                    string filename = filepath.Substring(filepath.LastIndexOf("\\") + 1);
                    //string  serverpath1  =  Server.MapPath( "images/ ")  +  filename; 
                    string serverpath2 = Server.MapPath("images/ ") + System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss ") + Session.SessionID + filename;
                    FileUpload1.PostedFile.SaveAs(serverpath2); 
                    Response.Write(filepath);
                    ImageThumbnail img = new ImageThumbnail(filepath);
                    img.ReducedImage(200, 200, serverpath2);//0.4表示缩小40% 
                    this.lb_info.Text = "上传成功! ";
                }
            }
            catch (Exception error)
            {
                this.lb_info.Text = "上传发生错误!原因: " + error.ToString();
            }
    ===========================
    楼主的这个地方好像多了一个空格,造成获取文件文件名字是错误。楼主可以看一下是不是这个问题。