void CutImage(HttpPostedFile post,string ppuid,out string imagename)
    {
        System.Drawing.Image SourceImg = System.Drawing.Image.FromStream(post.InputStream);
        if (SourceImg.Height > ConfigHelper.UserFaceMaxHeight)
        {
            this._lbl_upload_msg.Text = "最大高度不得大于 " + ConfigHelper.UserFaceMaxHeight;
            return;
        }
        if (SourceImg.Width > ConfigHelper.UserFaceMaxWidth)
        {
            this._lbl_upload_msg.Text = "最大宽度不得大于 " + ConfigHelper.UserFaceMaxWidth;
            return;
        }
        ImageFormat format = getImageformat(System.IO.Path.GetExtension(post.FileName));
        string filename =  ppuid+"."+format.ToString();
        imagename = filename;

        if (!UserFaceDir.EndsWith("\\")) 
            UserFaceDir = UserFaceDir+"\\";
        filename = UserFaceDir + filename;
        int SourceImgWidth = SourceImg.Width;
        int SourceImgHeight = SourceImg.Height;
        if ((SourceImgWidth != ConfigHelper.UserFaceWidth) && (SourceImgHeight != ConfigHelper.UserFaceHeight))
        {
            //如果宽高比例为1:1,则直接构成缩略图
            if (((Double)SourceImgWidth / SourceImgHeight) == 1)
            {
                System.Drawing.Image thumbimg = SourceImg.GetThumbnailImage(ConfigHelper.UserFaceWidth, ConfigHelper.UserFaceHeight, null, IntPtr.Zero);
                thumbimg.Save(filename, format);
                thumbimg.Dispose();
                SourceImg.Dispose();
                return;
            }
            Bitmap bit = new Bitmap(SourceImg);
            Rectangle rec = new Rectangle();   //构造一个Rectangle类,一个矩形
            rec.Width = ConfigHelper.UserFaceWidth;  
            rec.Height = ConfigHelper.UserFaceHeight;
            if (SourceImgWidth > rec.Width)
                rec.X = (SourceImgWidth - rec.Width) / 2;
            else
            {
                rec.X = 0;
                rec.Width = SourceImg.Width;
            }
            if (SourceImgHeight > rec.Height)
                rec.Y = (SourceImgHeight - rec.Height) / 2;
            else
            {
                rec.Y = 0;
                rec.Height = SourceImg.Height;
            }

            try
            {
                //这里就是把从上传过程中构造的bitmap克隆一份,并按定义好的矩形裁剪
                bit.Clone(rec, PixelFormat.DontCare).Save(filename, format);
            }
            catch (Exception ex)
            {
                this._lbl_upload_msg.Text = ex.Message;
                return;
            }
            finally
            {
                bit.Dispose();
                SourceImg.Dispose();
            }
        }
    }
posted on 2006-12-02 13:38  Eric Yao  阅读(1123)  评论(0编辑  收藏  举报