Exemplo n.º 1
0
        public async Task <bool> SaveImage(ImageInputDto input)
        {
            try
            {
                var tempProfilePicturePath = Path.Combine(_appFolders.TempFileDownloadFolder, input.FileName);

                byte[] byteArray;

                using (var fsTempProfilePicture = new FileStream(tempProfilePicturePath, FileMode.Open))
                {
                    using (var bmpImage = new Bitmap(fsTempProfilePicture))
                    {
                        var width  = input.Width == 0 ? bmpImage.Width : input.Width;
                        var height = input.Height == 0 ? bmpImage.Height : input.Height;
                        var bmCrop = bmpImage.Clone(new Rectangle(input.X, input.Y, width, height), bmpImage.PixelFormat);

                        using (var stream = new MemoryStream())
                        {
                            bmCrop.Save(stream, bmpImage.RawFormat);
                            stream.Close();
                            byteArray = stream.ToArray();
                        }
                    }
                }

                if (byteArray.LongLength > 10002400) //100 MB
                {
                    throw new UserFriendlyException(L("ResizedProfilePicture_Warn_SizeLimit"));
                }

                var user = await UserManager.GetUserByIdAsync(AbpSession.GetUserId());

                var storedFile = new BinaryObject(AbpSession.TenantId, byteArray);
                await _binaryObjectManager.SaveAsync(storedFile);


                FileHelper.DeleteIfExists(tempProfilePicturePath);


                //if (input.ImageId == 0)
                //{
                _imageRepository.InsertOrUpdateAndGetId(new Image()
                {
                    FileLocation = tempProfilePicturePath,
                    FileFormat   = input.FileExtension,
                    FileName     = input.FileName,
                    ImageByte    = byteArray,
                    ImageString  = Convert.ToBase64String(byteArray),
                    UserId       = user.Id,
                    Id           = input.ImageId
                });
                return(true);
                //}
            }
            catch (Exception e)
            {
                throw new UserFriendlyException(e.Message);
            }
        }
Exemplo n.º 2
0
 public async Task <bool> EditImage(ImageInputDto input)
 {
     throw new NotImplementedException();
 }