Example #1
0
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.QueryString["picId"] != null)
                {
                    int picId = Convert.ToInt32(Request.QueryString["picId"]);
                    string picName = Request.QueryString["picName"].ToString();//图片名称
                    string AlbumId = Request.QueryString["AlbumId"].ToString();//相册ID
                    string BigPath = Request.QueryString["BigPath"].ToString();//大图片地址

                    imgPic.Src = SiteUrl + BigPath;//显示的图片
                    lblPictureName.Text = imgPic.Alt = picName;

                    CY.UME.Core.Business.Picture picture = new CY.UME.Core.Business.Picture();
                    picture.Id = picId;
                    IList<PictureComment> pictureCommentList = PictureComment.GetPicCommentByPagesAndPicId(picture);

                    if (pictureCommentList.Count >= 1)
                    {
                        lblPictureComment.Text = "";
                        Repeater1.DataSourceID = "";
                        Repeater1.DataSource = pictureCommentList;
                        Repeater1.DataBind();
                    }
                    else
                    {
                        lblPictureComment.Text = "暂无评论。";
                    }
                }
            }
        }
        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "application/json";

            CY.UME.Core.Business.Account account;
            string bigImgPath, smallImgPath, imgExtention, imgName;
            string sitePath = CY.Utility.Common.SiteUtility.CurSitePath;

            String Id = context.Request.QueryString["accountId"].ToString();

            long accountId = CY.Utility.Common.ConvertUtility.ConvertToInt(Id, -1);

            #region -Validation and Get Basic Data-

            //account = CY.UME.Core.Global.GetCurrentAccount();
            account = CY.UME.Core.Business.Account.Load(accountId);
            if (account == null)
            {
                context.Response.Write("{success: false, msg: '用户登录超时,请重新登录!'}");
                return;
            }

            // post data
            bigImgPath = context.Request.Form["bigImgPath"];
            smallImgPath = context.Request.Form["smallImgPath"];
            imgExtention = context.Request.Form["imgExtention"];
            imgName = context.Request.Form["imgName"];
            if (string.IsNullOrEmpty(bigImgPath) ||
                string.IsNullOrEmpty(smallImgPath) ||
                string.IsNullOrEmpty(imgExtention))
            {
                context.Response.Write("{success: false, msg: '参数错误'}");
                return;
            }

            if (!File.Exists(sitePath + bigImgPath))
            {
                context.Response.Write("{success: false, msg: '照片不存在'}");
                return;
            }
            if (!File.Exists(sitePath + smallImgPath))
            {
                context.Response.Write("{success: false, msg: '小头像不存在'}");
                return;
            }
            #endregion

            CY.UME.Core.Business.Album avatarAlbum = account.GetMyAvatarAlbum();
            if (avatarAlbum == null)
            {
                avatarAlbum = new CY.UME.Core.Business.Album();
                avatarAlbum.AccountId = account.Id;
                avatarAlbum.DateCreated = DateTime.Now;
                avatarAlbum.IsAvatar = true;
                avatarAlbum.LastModifiedTime = avatarAlbum.DateCreated;
                avatarAlbum.Name = "头像相册";
                avatarAlbum.Save();
            }

            // 相册目录为: 网站根目录 + /Content/Avatar/ + 用户标识
            string avatarRelativePath = "/Content/Avatar/" + account.Id + "/";
            DirectoryInfo di = new DirectoryInfo(sitePath + avatarRelativePath); // 相册目录
            if (!di.Exists)
            {
                di.Create();
            }

            string uniqueId = Guid.NewGuid().ToString("N");

            string bImgName = uniqueId + "_big" + imgExtention;
            string mImgName = uniqueId + "_mid" + imgExtention;
            string sImgName = uniqueId + "_small" + imgExtention;

            try
            {
                Bitmap bmp = new Bitmap(sitePath + bigImgPath);
                int width = bmp.Width;
                int height = bmp.Height;

                bmp.Dispose();

                if (height <= width && width >= 200)
                {
                    height = Convert.ToInt32(width > 200 ? (200f / width) * height : height);
                    width = 200;
                }
                else if (width < height && height > 200)
                {
                    width = Convert.ToInt32(height > 200 ? (200f / height) * width : height);
                    height = 200;
                }

                // 将大图片压缩为中等缩略图
                using (StreamReader reader = new StreamReader(sitePath + bigImgPath))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, di.FullName + mImgName, width, height);
                }
                // 将小头像从120*120压缩到48*48
                using (StreamReader reader = new StreamReader(sitePath + smallImgPath))
                {
                    CY.Utility.Common.ImageUtility.ThumbAsJPG(reader.BaseStream, di.FullName + sImgName, 65, 65);//之前是48 x 48 2010.12.21 刘小平
                }

                // 将大图移动到头像目录下
                FileInfo fi = new FileInfo(sitePath + bigImgPath);
                fi.MoveTo(di.FullName + bImgName);

                try
                {
                    // File.Delete(sitePath + bigImgPath);
                    File.Delete(sitePath + smallImgPath);
                }
                catch
                {
                    ;
                }

                // 添加照片记录
                CY.UME.Core.Business.Picture picture = new CY.UME.Core.Business.Picture();
                picture.AlbumId = avatarAlbum.Id;
                picture.BigPath = avatarRelativePath + bImgName;
                picture.DateCreated = DateTime.Now;
                picture.MiddlePath = avatarRelativePath + mImgName;
                picture.Name = imgName;
                picture.Remark = string.Empty;
                picture.SmallPath = avatarRelativePath + sImgName;
                picture.Save();

                if (!account.HasAvatar)
                {
                    account.HasAvatar = true;

                    #region 积分

                    int uploadAvatarCredit;
                    if (CY.UME.Core.Business.SystemSetting.TryLoadInt32Setting("CreditUploadAvatar", out uploadAvatarCredit) &&
                        (uploadAvatarCredit != 0))
                    {
                        int orgCredit = account.Credit;
                        int modifiedCredit = orgCredit + uploadAvatarCredit;
                        account.Credit = modifiedCredit;

                        CreditHistory ch = new CreditHistory();
                        ch.AccountId = account.Id;
                        ch.DateCreated = DateTime.Now;
                        ch.Id = Guid.NewGuid();
                        ch.InstanceId = "";
                        ch.Original = orgCredit;
                        ch.Modified = modifiedCredit;
                        ch.Variation = uploadAvatarCredit;
                        ch.Type = "uploadavatar";
                        ch.Description = "上传头像";
                        ch.Save();
                    }

                    #endregion

                    account.Save();
                }

                //添加通知
                //account.SendNoticeToAllFriend("picture", "更新了头像", picture.Id.ToString());

                context.Response.Write("{success: true}");
                return;
            }
            catch
            {
                context.Response.Write("{success: false, msg: '保存头像失败'}");
                return;
            }
        }