public ActionResult AddImage(int UserId) { UsersImageViewModel model = new UsersImageViewModel(); model.UserId = UserId; return(PartialView("~/Views/Shared/Partials/_Images.cshtml", model)); }
public ActionResult AddUserImage(int EmployeeId) { UsersImageViewModel model = new UsersImageViewModel(); model.UserId = EmployeeId; if (EmployeeId > 1) { foreach (string upload in Request.Files) { if (Request.Files[upload].FileName != "") { string fileName = Request.Files[upload].FileName; string contentType = Request.Files[upload].ContentType; long fileLength = Request.Files[upload].InputStream.Length; byte[] fileContent = new byte[fileLength]; using (MemoryStream memoryStream = new MemoryStream()) { Request.Files[upload].InputStream.CopyTo(memoryStream); fileContent = memoryStream.ToArray(); var image = Image.FromStream(memoryStream); var ratioX = (double)350 / image.Width; var ratioY = (double)200 / image.Height; var ratio = Math.Min(ratioX, ratioY); var width = (int)(image.Width * ratio); var height = (int)(image.Height * ratio); var newImage = new Bitmap(width, height); Graphics.FromImage(newImage).DrawImage(image, 0, 0, width, height); Bitmap bmp = new Bitmap(newImage); ImageConverter converter = new ImageConverter(); fileContent = (byte[])converter.ConvertTo(bmp, typeof(byte[])); } //string directory = Server.MapPath("/Image/"); HttpPostedFileBase photo = Request.Files["ImageFile"]; string ImagePath = ""; if (photo != null && photo.ContentLength > 0) { var PhfileName = Path.GetFileName(photo.FileName); //var path = Path.Combine(directory, PhfileName); //photo.SaveAs(path); ImagePath = "~/Image/" + PhfileName; } UserImageDTO dto = new UserImageDTO(); dto.EmployeeId = EmployeeId; dto.file_stream = fileContent; dto.ContentType = contentType; dto.FileNameWithExtension = fileName; int saved = _UsersBusinessLogic.AddImages(dto); } TempData["Success"] = MStart.Common.App_LocalResources.Resource.Success; } return(RedirectToAction("Index")); } else { TempData["Success"] = MStart.Common.App_LocalResources.Resource.Error; return(RedirectToAction("Index")); } }