在C#20里fileupload控件上传图片后怎么获取图片的长和宽啊  
 List<Album> album = AlbumBiz.SelectAlbumByUserId(user.User_id);
            //得到完整的文件名
            string filename = this.FileUpload1.PostedFile.FileName;
            //得到扩展名
            string extname = System.IO.Path.GetExtension(filename);
            //3.重新设置一个文件名
            string newname = DateTime.Now.ToString("yyyyMMddHHmmss");
            //随机数
            Random rd = new Random();
            newname = newname + rd.Next(1, 10000).ToString();
            //将相对路径转换为绝对路径(重要)
            string path = Server.MapPath("~/images/");
            //4.真正的上传(必须是绝对路径)
            this.FileUpload1.PostedFile.SaveAs(path + newname + extname);   我想让我上传的 图片 按我想要的大小来  高手帮忙解决一下

解决方案 »

  1.   

    建议楼主可以实现一个等比例缩放,参考using System;
    using System.Collections;
    using System.Configuration;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.IO;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Drawing.Imaging;public partial class UCUploadFiles : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            PicturePath = string.IsNullOrEmpty(Request["picpath"]) ? "" : Request.QueryString["picpath"].ToString();
            PicHeight = string.IsNullOrEmpty(Request.QueryString["height"]) ? 108 : int.Parse(Request.QueryString["height"].ToString());
            PicWidth = string.IsNullOrEmpty(Request.QueryString["width"]) ? 196 : int.Parse(Request.QueryString["width"].ToString());
        }    /// <summary>
        /// 源控件名称
        /// </summary>
        private string picturePath;
        public string PicturePath
        {
            get { return picturePath; }
            set { picturePath = value; }
        }    /// <summary>
        /// 高度
        /// </summary>
        private int picHeight;
        public int PicHeight
        {
            get { return picHeight; }
            set { picHeight = value; }
        }    /// <summary>
        /// 宽度
        /// </summary>
        private int picWidth;
        public int PicWidth
        {
            get { return picWidth; }
            set { picWidth = value; }
        }    protected void Button1_Click(object sender, EventArgs e)
        {
            #region 上传图片到指定路径
            HttpPostedFile postFile = this.FileUpload1.PostedFile;
            string fileType = Path.GetExtension(this.FileUpload1.PostedFile.FileName);
            string uploadPath = System.Configuration.ConfigurationManager.AppSettings["uploadfiles"];// "uploadfiles";//上传图片存放文件夹        string path = System.Configuration.ConfigurationManager.AppSettings["uploadpath"]; //Server.MapPath("~/files/");
            string sServerPath = path;
            path = path + uploadPath;        if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }        string fileName = "U";
            fileName += "_";     //分割符;
            fileName += System.DateTime.Now.Year.ToString();
            fileName += System.DateTime.Now.Month.ToString();
            fileName += System.DateTime.Now.Day.ToString();
            fileName += System.DateTime.Now.Hour.ToString();
            fileName += System.DateTime.Now.Minute.ToString();
            fileName += System.DateTime.Now.Second.ToString();
            fileName += fileType;        string sThumbnailFileName = fileName.Substring(0, fileName.IndexOf(fileType) - 1) + "_Thumbnail" + fileType;
            string smallFileName = fileName.Substring(0, fileName.IndexOf(fileType) - 1) + "_small" + fileType;        //model.PhotoPath = "ImageFileUpLoad/" + smallFileName;设置最终保存等比例图片路径到数据库中对应的图片路径字段        string sThumbnailFilePath = path + "\\" + sThumbnailFileName;
            string smallFilePath = path + "\\" + smallFileName;
            string bigFilePath = path + "\\" + fileName;        postFile.SaveAs(bigFilePath);        //按比例制作所略图
            MakeZeroImage(bigFilePath, sThumbnailFilePath);
            //将缩略图与背景图合并
            UnitSmallImageAndBackGround(sThumbnailFilePath, smallFilePath);
            #endregion        setFilePath(smallFileName, picturePath);
        }
        protected void setFilePath(string file_name, string picturePath)
        {
            string strScript;
            string strKey;
            int i;
            //脚本块的内容 
            strScript = "<script language=javascript>\n";
            strScript += "window.alert('上传成功!');";
            strScript += "window.opener.document.form1." + picturePath + ".value='" + file_name + "';";
            strScript += " window.opener.location='default.aspx?picpath=" + file_name + "';";
            strScript += "window.close();";
            strScript += "</script>";
            //reload();
            //注册脚本块的Key 
            strKey = System.DateTime.Now.ToString();
            Page.RegisterClientScriptBlock(strKey, strScript);
        }
        //图片等比缩放处理
        //int PicWidth = 196;
        //int PicHeight = 108;
        private void MakeZeroImage(string sbigFilePath, string sThumbnailFilePath)
        {
            System.Drawing.Image image = System.Drawing.Image.FromFile(sbigFilePath);
            decimal width = Decimal.Parse(image.Width.ToString());
            decimal heght = Decimal.Parse(image.Height.ToString());
            int newwidth, newheight;
            if (width > heght)
            {
                newwidth = picWidth;
                decimal tempwidth = heght / width;
                newheight = Int32.Parse(Math.Round((tempwidth * newwidth), 0).ToString());
            }
            else
            {
                newheight = picHeight;
                decimal tempheight = width / heght;
                newwidth = Int32.Parse(Math.Round((tempheight * newheight), 0).ToString());
            }
            System.Drawing.Image oNewImage = image.GetThumbnailImage(newwidth, newheight, null, IntPtr.Zero);
            image.Dispose();
            oNewImage.Save(sThumbnailFilePath);
            oNewImage.Dispose();
        }
        /// <summary>
        /// 将缩略图与背景图合并
        /// </summary>
        private void UnitSmallImageAndBackGround(string sThumbnailFilePath, string smallFilePath)
        {
            //背景图片
            string bgFileName = Server.MapPath("~/files/uploadfiles/background.jpg");//背景图片地址
            System.Drawing.Image image = System.Drawing.Image.FromFile(bgFileName);
            System.Drawing.Image copyImage = System.Drawing.Image.FromFile(sThumbnailFilePath);        System.Drawing.Image aNewImage;
            Graphics g = Graphics.FromImage(image);
            g.SmoothingMode = SmoothingMode.HighQuality;
            g.InterpolationMode = InterpolationMode.High;
            g.CompositingQuality = CompositingQuality.HighQuality;        g.DrawImage(copyImage, new Rectangle((picWidth - copyImage.Width) / 2, (picHeight - copyImage.Height) / 2, copyImage.Width, copyImage.Height), 0, 0, copyImage.Width, copyImage.Height, GraphicsUnit.Pixel);
            g.Dispose();
            //string finalpicture = GetPicName();
            //string newPath = MapPath(sServerPath) + sFileName;// +finalpicture + ".jpg";
            copyImage.Dispose();        ImageCodecInfo myImageCodecInfo;
            System.Drawing.Imaging.Encoder myEncoder;
            EncoderParameter myEncoderParameter;
            EncoderParameters myEncoderParameters;
            myImageCodecInfo = ImageCodecInfo.GetImageEncoders()[0];
            myEncoder = System.Drawing.Imaging.Encoder.Quality;
            myEncoderParameters = new EncoderParameters(1);
            myEncoderParameter = new EncoderParameter(myEncoder, 100L);   //   0-100   
            myEncoderParameters.Param[0] = myEncoderParameter;
            image.Save(sThumbnailFilePath, myImageCodecInfo, myEncoderParameters);
            myEncoderParameter.Dispose();
            myEncoderParameters.Dispose();
            image.Dispose();        System.Drawing.Image finalImage = System.Drawing.Image.FromFile(sThumbnailFilePath);
            //string smallfile_name = finalpicture + "final.jpg";
            aNewImage = finalImage.GetThumbnailImage(picWidth, picHeight, null, IntPtr.Zero);
            finalImage.Dispose();
            aNewImage.Save(smallFilePath);//, System.Drawing.Imaging.ImageFormat.Jpeg
            aNewImage.Dispose();
            //return smallfile_name;
        }
    }
      

  2.   

    我是把他做成用户控件了,添加了/// <summary>
        /// 高度
        /// </summary>
        private int picHeight;
        public int PicHeight
        {
            get { return picHeight; }
            set { picHeight = value; }
        }    /// <summary>
        /// 宽度
        /// </summary>
        private int picWidth;
        public int PicWidth
        {
            get { return picWidth; }
            set { picWidth = value; }
        }
    两个属性值,可自行设定上传图片的高度和宽度
      

  3.   


    如上传控件为 myup
    System.Drawing.Image upimage=System.Drawing.Image.FromStream(Request.Files["myup"].InputStream);
    int width = upimage.Width;
    int height = upimage.Height;