代码如下
Byte[] oFileByte = new byte[this.File1.PostedFile.ContentLength]; 
System.IO.Stream oStream = this.File1.PostedFile.InputStream; 
System.Drawing.Image oImage = System.Drawing.Image.FromStream(oStream); 
int oWidth = oImage.Width; //原图宽度 
int oHeight = oImage.Height; //原图高度 
int tWidth = 100; //设置缩略图初始宽度 
int tHeight = 100; //设置缩略图初始高度 
if(oWidth >= oHeight) 

tHeight = (int)Math.Floor(Convert.ToDouble(oHeight) * (Convert.ToDouble(tWidth) / Convert.ToDouble(oWidth))); 

else 

tWidth = (int)Math.Floor(Convert.ToDouble(oWidth) * (Convert.ToDouble(tHeight) / Convert.ToDouble(oHeight))); 

Bitmap tImage = new Bitmap(tWidth,tHeight); 
Graphics g = Graphics.FromImage(tImage); 
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High; //设置高质量插值法 
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;//设置高质量,低速度呈现平滑程度 
g.Clear(Color.Transparent); //清空画布并以透明背景色填充 
g.DrawImage(oImage,new Rectangle(0,0,tWidth,tHeight),new Rectangle(0,0,oWidth,oHeight),GraphicsUnit.Pixel); 
string oFullName = Server.MapPath("txt/") + "/" + "o" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存原图的物理路径 
string tFullName = Server.MapPath("txt/") + "/" + "t" + DateTime.Now.ToShortDateString().Replace("-","") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + ".jpg"; //保存缩略图的物理路径 
try 

//以JPG格式保存图片 
oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg); 
tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg); 

catch(Exception ex) 

throw ex; 

finally 

//释放资源 
oImage.Dispose(); 
g.Dispose(); 
tImage.Dispose(); 

解决方案 »

  1.   

    protected void btnUpdaload_Click(object sender, EventArgs e)
        {
            if (imageUpload.HasFile)
            {
                string extension = Path.GetExtension(imageUpload.FileName);
                if (SafeTool.CheckAllowFile(extension))
                {
                    string path = Server.MapPath("~/Images/FlashImages");
                    string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + extension;
                    imageUpload.PostedFile.SaveAs(path + "\\" + fileName);                Uri uri = Request.Url;
                    //注意,这里数据库里保存的是相对地址,显示给用户的是绝对URL,这样即使域名或者路径发生相对更改,也不会影响使用
                    //txtLinkUrl.Text = string.Format("{0}://{1}/download/{2}", uri.Scheme, uri.Authority, fileName);//显示给用户的地址
                    //hfPath.Value = string.Format("download/{0}", fileName);//数据库中保存的地址
                    txtImagePath.Text = string.Format("Images/FlashImages/{0}", fileName);
                }
                else
                {
                    JScript.Alert("你上传的文件格式不符合要求!");
                }
            }
            else
            {
                JScript.Alert("请选择一个文件上传!");
            }
        }
      

  2.   

    oImage.Save(oFullName,System.Drawing.Imaging.ImageFormat.Jpeg); 
    tImage.Save(tFullName,System.Drawing.Imaging.ImageFormat.Jpeg); 
    这里不是有吗?
    oFullName="d:\asdfasdf.jpg";
    你自己定义下就可以了~
      

  3.   

    public static UploadMsg UploadFile(HttpPostedFile file, int maxSize, int space, FileType fileType, string stillPath,int preWidth,int preHeight, ref string path, string[] , params string[] name)
            {
                if (file.ContentLength == 0)
                {
                    return UploadMsg.NoFile;
                }
                if (file.ContentLength > maxSize && maxSize != -1)
                {
                    return UploadMsg.SizeOut;
                }
                if (file.ContentLength > space && space != -1)
                {
                    return UploadMsg.NoSpace;
                }
                string[] type = new string[0];
                string fileName = file.FileName.Substring(file.FileName.LastIndexOf(".") + 1).ToLower();
                switch (fileType)
                {
                    case FileType.Image:
                        type = "jpg,gif,png,bmp".Split(',');
                        fileName = FileExt(file.ContentType);
                        break;
                    case FileType.Music:
                        type = "mp3,wma".Split(',');
                        break;
                    case FileType.Other:
                        type = name;
                        break;
                }
                bool chkExt = false;
                foreach (string ext in type)
                {
                    if (ext == fileName)
                    {
                        chkExt = true;
                        break;
                    }
                }
                if (type.Length == 0)
                {
                    chkExt = true;
                }
                if (!chkExt || fileName == "")
                {
                    return UploadMsg.TypeError;
                }            if (fileType == FileType.Other)
                {
                    if (stillPath != "")
                    {
                        file.SaveAs(stillPath);
                        return UploadMsg.Success;
                    }
                    return UploadMsg.Other;
                }
                string newPath = DateTime.Now.ToString("yyyy\\/MM\\/dd\\/");// "\"为转义字符
                if (!Directory.Exists(path + "\\" + newPath))
                {
                    Directory.CreateDirectory(path + "\\" + newPath);
                }
                string newFileName = CreateFileName();
                while (File.Exists(path + @"\" + newFileName + "." + fileName))
                {
                    newFileName = CreateFileName();
                }
                if (fileType == FileType.Image)
                {
                    file.SaveAs(path + "\\" + newPath + @"\" + newFileName + "." + fileName);
                    //MakeThumbnail(path + "\\" + newPath + @"\" + newFileName + "." + fileName,
                    //              path + "\\" + newPath + @"\" + newFileName + "_pre." + fileName,
                    //              preWidth, preHeight, "Cut");
                    Hashtable ht = GetPhotoExif(path + "\\" + newPath + @"\" + newFileName + "." + fileName);
                    MakeMark(path + "\\" + newPath + @"\" + newFileName + "." + fileName, );                path = string.Format("Size={0}&FileName={1}&Width={2}&Height={3}&DC={4}&Pic={5}", file.ContentLength, file.FileName.Substring(file.FileName.LastIndexOf(@"\") + 1),
                                            (int)ht["Width"], (int)ht["Height"],
                                            (string)ht["Company"] + " " + (string)ht["DC"],
                                            newPath + newFileName + "." + fileName);            }
                else
                {
                    file.SaveAs(path + "\\" + newPath + @"\" + newFileName + "." + fileName);
                    path = string.Format("Size={0}&LocalPath={1}&Path={2}", file.ContentLength,
                                            file.FileName,
                                            newPath + newFileName + "." + fileName);
                }
                return UploadMsg.Success;
            }
      

  4.   

    忘记写注释了:/// <summary>
            /// 上传文件的基类方法
            /// </summary>
            /// <param name="file">文件流</param>
            /// <param name="maxSize">允许的最大尺寸</param>
            /// <param name="space">剩余空间</param>
            /// <param name="fileType">文件类型</param>
            /// <param name="stillPath">静止路径</param>
            /// <param name="preWidth">缩略图宽度</param>
            /// <param name="preHeight">缩略图高度</param>
            /// <param name="path">输入路径 返回结果</param>
            /// <param name="">水印</param>
            /// <param name="name">可选参数 其他后缀名的文件</param>
            /// <returns></returns>
      

  5.   

    string newPath = DateTime.Now.ToString("yyyy\\/MM\\/dd\\/");// "\"为转义字符 
       file.SaveAs(path + "\\" + newPath + @"\" + newFileName + "." + fileName
      

  6.   

    OK,谢谢大家了,那么再一个简单的问题,有什么办法可以让文件保存在项目以外的目录下?
    因为我现在("txt/")
    是在这个项目的目录,有什么办法可以将文件保存在上级的目录中
      

  7.   

    通过取得项目的物理路径然后再通过SubString直接取得最后一个\前的路径就可以了