public ServiceResult <UserPhotoInfo> GetUserPhotoByAccount(string account, string format) { var userPhoto = UserAdapter.GetUserPhotoByAccount(account); if (userPhoto == null) { userPhoto = new UserPhotoInfo() { Account = account, PhotoExt = ".jpg" }; string imagePath = this.Context.Server.MapPath("..\\static\\defaultuser.jpg"); System.IO.FileStream stream = new System.IO.FileStream(imagePath, System.IO.FileMode.Open); userPhoto.PhotoBinary = new byte[stream.Length]; stream.Read(userPhoto.PhotoBinary, 0, (int)stream.Length); stream.Close(); } if (format.ToUpper() == "BASE64") { return(new ServiceResult <UserPhotoInfo>(userPhoto)); } else { byte[] image_bytes = userPhoto.PhotoBinary; string image_ext = userPhoto.PhotoExt; if (format.IndexOf("*") > -1) { int newW = Convert.ToInt32(format.Split('*')[0]); int newH = Convert.ToInt32(format.Split('*')[1]); System.IO.MemoryStream msSource = new System.IO.MemoryStream(image_bytes); System.Drawing.Bitmap bmp = (System.Drawing.Bitmap)System.Drawing.Image.FromStream(msSource); msSource.Close(); System.Drawing.Bitmap bpmTarget = new System.Drawing.Bitmap(newW, newH); System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bpmTarget); g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic; g.DrawImage(bmp, new System.Drawing.Rectangle(0, 0, newW, newH), new System.Drawing.Rectangle(0, 0, bmp.Width, bmp.Height), System.Drawing.GraphicsUnit.Pixel); g.Dispose(); System.IO.MemoryStream ms = new System.IO.MemoryStream(); bpmTarget.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp); image_bytes = ms.ToArray(); ms.Close(); } this.Context.Response.ContentType = image_ext == ".jpg" ? "image/jpeg" : image_ext == "gif" ? "image/gif" : "image/x-png"; this.Context.Response.BinaryWrite(image_bytes); this.Context.Response.End(); } return(new ServiceResult <UserPhotoInfo>(null)); }
public JsonResult upFile() { AjaxMsgResult reuslt = new AjaxMsgResult(); if (!isInTime()) { reuslt.Msg = "活动已过期"; return(Json(reuslt)); } HttpPostedFileBase file = Request.Files[0]; string skey = "x_photo_up"; UpFileTypeInfo uftype = new UpFileTypeInfo(); if (Session[skey] != null) { uftype = Session[skey] as UpFileTypeInfo; } else { Session[skey] = uftype; } if (file != null) { string oripath = "/uploads/" + CurrentUser.ID + "/"; string path = Server.MapPath(oripath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } string filename = DateTime.Now.ToString("yyMMddhhmmssfff") + file.FileName; file.SaveAs(path + filename); ThumbnailHelper.MakeThumbnailImage(path + filename, path + filename + "_1.jpg", 400, 400); string dbpath = oripath + filename; if (string.IsNullOrEmpty(uftype.fatherp)) { uftype.fatherp = dbpath; } else if (string.IsNullOrEmpty(uftype.childp)) { uftype.childp = dbpath; } if (!string.IsNullOrEmpty(uftype.fatherp) && !string.IsNullOrEmpty(uftype.childp)) { UserPhotoService x_upService = new UserPhotoService(); UserPhotoInfo uinfo = new UserPhotoInfo() { UserId = CurrentUser.ID, FatherPhoto = uftype.fatherp, ChildPhoto = uftype.childp, CreateTime = DateTime.Now, IsValid = 1, PerValueTime = DateTime.Now.AddYears(-100) }; x_upService.Insert(uinfo); uftype.fatherp = null; uftype.childp = null; } reuslt.Success = true; reuslt.Source = dbpath + "_1.jpg"; } return(Json(reuslt)); }