Example #1
0
        private Photo ApplyWaterMark(Photo photo, string waterMarkText)
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                memoryStream.Write(photo.Data, 0, photo.Data.Length);
                memoryStream.Position = 0;

                Bitmap sourceImage = (Bitmap)Image.FromStream(memoryStream);

                using (Font font = new Font("Arial", 14, FontStyle.Bold))
                {
                    using (Graphics destination = Graphics.FromImage(sourceImage))
                    {
                        float width  = sourceImage.Width / 2f;
                        float height = sourceImage.Height / 2f;

                        destination.TranslateTransform(width, height);
                        destination.RotateTransform(45);

                        SizeF  size      = destination.MeasureString(waterMarkText, font);
                        PointF drawPoint = new PointF(-size.Width / 2f, -size.Height / 2f);
                        destination.DrawString(waterMarkText, font, Brushes.LawnGreen, drawPoint);
                    }
                }

                using (MemoryStream waterMarkedMemoryStream = new MemoryStream())
                {
                    sourceImage.Save(waterMarkedMemoryStream, sourceImage.RawFormat);

                    if (photo.WaterMarked == null)
                    {
                        photo.WaterMarked = new File();
                    }

                    photo.WaterMarked.ContentContentType = photo.ContentContentType;
                    photo.WaterMarked.Data     = waterMarkedMemoryStream.ToArray();
                    photo.WaterMarked.FileName = photo.FileName;
                }
            }

            _photosRepository.Update(photo);

            return(photo);
        }
Example #2
0
        public async Task SetMain(int userId, int photoId)
        {
            var user = await _usersRepo.Get(userId);

            var photo = user.Photos.FirstOrDefault(p => p.Id == photoId);

            if (photo == null)
            {
                throw new RestException(HttpStatusCode.NotFound, new { Photo = "Not found" });
            }

            if (photo.IsMain)
            {
                throw new RestException(HttpStatusCode.BadRequest, new { Photo = "Photo is already main" });
            }

            var mainPhoto = user.Photos.FirstOrDefault(p => p.IsMain);

            mainPhoto.IsMain = false;
            photo.IsMain     = true;
            await _repo.Update(mainPhoto);

            await _repo.Update(photo);
        }
Example #3
0
        public async Task Update(PhotoView photoView)
        {
            var photo = _mapper.Map <Photo>(photoView);

            var photoDB = await _photosRepository.GetById(photo.Id);

            if (photoDB == null)
            {
                throw new ArgumentException("Photo not found");
            }

            if (photoDB.UserId != photo.UserId)
            {
                throw new ArgumentException("Photo doesn't belong to user");
            }

            photoDB.Description = photo.Description;

            await Task.WhenAll(_photosRepository.Update(photoDB),
                               _hashtagsService.AddNewHashtags(photoDB.Id, photoDB.Description));
        }
Example #4
0
 public void Update(Photo photo)
 {
     _repository.Update(photo);
 }