public async Task <IActionResult> AddPhoto(AddPhotoModel addPhotoModel)
        {
            if (addPhotoModel == null || !ModelState.IsValid)
            {
                return(View());
            }

            var file = addPhotoModel.Files.FirstOrDefault();

            if (file == null || file.Length == 0)
            {
                return(View());
            }

            var title   = addPhotoModel.Title;
            var ownerId = GetOwnerId();

            byte[] content;
            using (var fileStream = file.OpenReadStream())
            {
                using (var memoryStream = new MemoryStream())
                {
                    fileStream.CopyTo(memoryStream);
                    content = memoryStream.ToArray();
                }
            }

            if (!await photosRepository.AddPhotoAsync(title, ownerId, content))
            {
                return(StatusCode(StatusCodes.Status409Conflict));
            }

            return(RedirectToAction("Index"));
        }
        /// <summary>
        /// 添加附件
        /// </summary>
        /// <param name="Model">附件列表</param>
        /// <returns></returns>
        public string AddPhoto(AddPhotoModel Model)
        {
            string result = "";

            Dictionary <string, List <PhotoModel> > dic = new Dictionary <string, List <PhotoModel> >();

            dic.Add("Photos", Model.Photos);

            switch (Model.Type.ToUpper())
            {
            case "1":
                result = new FeeBill().PublicEditMethod <List <PhotoModel> >(Model.BillNo, dic);
                break;

            case "2":
                result = new NoticeBill().PublicEditMethod <List <PhotoModel> >(Model.BillNo, dic);
                break;

            case "3":
                result = new BorrowBill().PublicEditMethod <List <PhotoModel> >(Model.BillNo, dic);
                break;

            case "4":
                result = new RefundBill().PublicEditMethod <List <PhotoModel> >(Model.BillNo, dic);
                break;

            default:
                break;
            }

            return(result);
        }
Exemple #3
0
 private Photo GetPhotoEntity(AddPhotoModel model)
 {
     return(new Photo
     {
         User = LoggedUser,
         Category = model.Category,
         DisplayName = model.DisplayName,
         FileName = model.FileName,
         PhotoNum = DateTime.Now.Ticks.ToString(),
         Image = model.Image
     });
 }
Exemple #4
0
 public ActionResult <bool> AddPhoto(AddPhotoModel photoModel)
 {
     try
     {
         Photo photo = GetPhotoEntity(photoModel);
         _repository.AddPhoto(photo);
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }
Exemple #5
0
        public virtual ActionResult AddPhoto(AddPhotoModel model)
        {
            const int maxWidth = 840;

            //int PhotoId = 0;
            //byte[] resPhoto = null;

            if (model.Photo != null && model.Photo.ContentLength > 0)
            {
                //получаем фотку
                Bitmap src = Image.FromStream(model.Photo.InputStream) as Bitmap;

                //обрезаем и уменьшаем размер для превьюхи
                Rectangle cropRect = new Rectangle(model.x, model.y, model.w, model.h);
                Bitmap    bmpCrop  = src.Clone(cropRect, src.PixelFormat);

                Bitmap bmpCropPreview = new Bitmap(120, 90);
                using (Graphics g = Graphics.FromImage(bmpCropPreview))
                    g.DrawImage(bmpCrop, 0, 0, 120, 90);

                //если слишком здорова картинка, то уменьшаем по ширине
                if (src.Width > maxWidth)
                {
                    var ratio = (double)src.Width / maxWidth;
                    bmpCrop = new Bitmap(840, (int)(src.Height / ratio));
                    using (Graphics g = Graphics.FromImage(bmpCrop))
                        g.DrawImage(src, 0, 0, 840, (int)(src.Height / ratio));
                    src = bmpCrop;
                }
                //Bitmap target = new Bitmap(cropRect.Width, cropRect.Height);

                //using (Graphics g = Graphics.FromImage(target))
                //{
                //    g.DrawImage(src, new Rectangle(0, 0, target.Width, target.Height),
                //                     cropRect,
                //                     GraphicsUnit.Pixel);
                //}


                ImageConverter converter = new ImageConverter();
                if (db.addPhoto(model.AlbumId, (byte[])converter.ConvertTo(bmpCropPreview, typeof(byte[])), (byte[])converter.ConvertTo(src, typeof(byte[])), model.Description, Convert.ToInt32(User.Identity.Name)))
                {
                    return(RedirectToAction("ShowAlbum", new { id = model.AlbumId }));
                }
                else
                {
                    ViewData["Error"] = "Похоже, что не записалось :(";
                }
                //resPhoto = (byte[])converter.ConvertTo(bmpCropPreview, typeof(byte[]));

                //byte[] buffer = null;
                //using (var binaryReader = new BinaryReader(model.Photo.InputStream))
                //{
                //    buffer = binaryReader.ReadBytes(model.Photo.ContentLength);

                //}

                //сохраняем в базу
                //db.addImg(model.ThingId, buffer);
            }

            //ViewBag.Photo = resPhoto;
            return(View(model));
            //return RedirectToAction("EditPhoto", new { PhotoId = PhotoId });
        }